<commit_before>var net = require("net")
  , protocol = require("./protocol")
  , EventEmitter = require("events")
  , inherits = require("util").inherits

var HOST = "livecmt.bilibili.com"
  , PORT = "88"

function Conn(channelID) {
  this.channelID = channelID
  this.socket = this.hbInterval = null
}
inherits(Conn, EventEmitter)

Conn.prototype.connect = function() {
  this.socket = net.connect(PORT, HOST)
  this.handleEvents()
  return this
}

Conn.prototype.handleEvents = function() {
  this.handleConnection()
  this.handleData()
  this.handleDisconnect()
}

Conn.prototype.handleConnection = function() {
  this.socket.on('connect', function() {
    this.joinChannel()
    this.setupHeartbeat()
  }.bind(this))
}

Conn.prototype.joinChannel = function() {
  this.socket.write(protocol.composeJoinChannel(this.channelID))
}

Conn.prototype.setupHeartbeat = function() {
  this.hbInterval = setInterval(function() {
    this.sendHeartbeat()
  }.bind(this), 55000)
}

Conn.prototype.sendHeartbeat = function() {
  this.socket.write(protocol.composeHeartbeat())
}

Conn.prototype.handleData = function() {
  this.socket.on("data", function(buf) {
    var messages = protocol.parse(buf)
    messages.forEach(function(m) {
      this.emit(m.type, m)
    }, this)
  }.bind(this))
}

Conn.prototype.handleDisconnect = function() {
  this.socket.on("error", this.reconnect.bind(this))
}

Conn.prototype.reconnect = function() {
  this.stopHeartbeat()
  this.connect()
}

Conn.prototype.stopHeartbeat = function() {
  clearInterval(this.hbInterval)
  this.hbInterval = null
}

module.exports = Conn
Conn.Conn = Conn

function newDumbConnection(channelID) {
  var conn = new Conn(channelID)
  conn.on("comment", function(c) {
    console.log("%s : %s", c.nick, c.text)
  }).on("unknown", function(m) {
    console.log("unknown message: %j", m)
  }).on("onlineNumber", function(m) {
    console.log("# Online: %d", m.number)
  })
  conn.connect()
}

if (module.id === '.') {
  newDumbConnection(process.argv[2] || 5446)
}<commit_msg>Remove scaffolding in conn.js<commit_after>var net = require("net")
  , protocol = require("./protocol")
  , EventEmitter = require("events")
  , inherits = require("util").inherits

var HOST = "livecmt.bilibili.com"
  , PORT = "88"

function Conn(channelID) {
  this.channelID = channelID
  this.socket = this.hbInterval = null
}
inherits(Conn, EventEmitter)

Conn.prototype.connect = function() {
  this.socket = net.connect(PORT, HOST)
  this.handleEvents()
  return this
}

Conn.prototype.handleEvents = function() {
  this.handleConnection()
  this.handleData()
  this.handleDisconnect()
}

Conn.prototype.handleConnection = function() {
  this.socket.on('connect', function() {
    this.joinChannel()
    this.setupHeartbeat()
  }.bind(this))
}

Conn.prototype.joinChannel = function() {
  this.socket.write(protocol.composeJoinChannel(this.channelID))
}

Conn.prototype.setupHeartbeat = function() {
  this.hbInterval = setInterval(function() {
    this.sendHeartbeat()
  }.bind(this), 55000)
}

Conn.prototype.sendHeartbeat = function() {
  this.socket.write(protocol.composeHeartbeat())
}

Conn.prototype.handleData = function() {
  this.socket.on("data", function(buf) {
    var messages = protocol.parse(buf)
    messages.forEach(function(m) {
      this.emit(m.type, m)
    }, this)
  }.bind(this))
}

Conn.prototype.handleDisconnect = function() {
  this.socket.on("error", this.reconnect.bind(this))
}

Conn.prototype.reconnect = function() {
  this.stopHeartbeat()
  this.connect()
}

Conn.prototype.stopHeartbeat = function() {
  clearInterval(this.hbInterval)
  this.hbInterval = null
}

module.exports = Conn
Conn.Conn = Conn
{'fraction_non_alphanumeric': 0.15813826891671204, 'fraction_numerical': 0.005171475231355471, 'mean_word_length': 4.357142857142857, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5458584', 'n_tokens_mistral': 1266, 'n_tokens_neox': 1220, 'n_words': 202}