<commit_before>var WS = require('ws')
  , EventEmitter = require('events').EventEmitter
  , inherits = require('inherits')
  , protocol = require('nydus-protocol')

module.exports = Socket

function Socket(host, wsOptions) {
  EventEmitter.call(this)
  this._host = host
  this._wsOptions = wsOptions
}
inherits(Socket, EventEmitter)

Socket.prototype.open = function() {
  if (!WS) {
    throw new Error('WebSockets are not supported')
  }

  if (!this._ws || this._ws.readyState >= WebSocket.CLOSING) {
    this._ws = new WS(this._host, this._wsOptions)
    this._ws.onopen = this._onOpen.bind(this)
    this._ws.onclose = this._onClose.bind(this)
    this._ws.onerror = this._onError.bind(this)
    this._ws.onmessage = this._onMessage.bind(this)
  }

  return this
}

Socket.prototype.close = function() {
  if (this._ws && this._ws.readyState < WebSocket.CLOSING) {
    this._ws.close()
  }

  return this
}

Socket.prototype._onOpen = function() {
  this.emit('connect')
}

Socket.prototype._onClose = function(event) {
  this.emit('disconnect', event)
}

Socket.prototype._onError = function(event) {
  this.emit('error', event)
}

Socket.prototype._onMessage = function(event) {
  try {
    var message = protocol.decode(event.data)
  } catch (err) {
    this._ws.close(1002, 'Invalid nydus message')
    this.emit('error', err)
    return
  }

  this.emit('message', message)
  switch (message.type) {
    case protocol.WELCOME:
      this.emit('message:welcome', message)
      break
    case protocol.CALL:
      this.emit('message:call', message)
      break
    case protocol.RESULT:
      this.emit('message:result', message)
      break
    case protocol.ERROR:
      this.emit('message:error', message)
      break
    case protocol.SUBSCRIBE:
      this.emit('message:subscribe', message)
      break
    case protocol.UNSUBSCRIBE:
      this.emit('message:unsubscribe', message)
      break
    case protocol.PUBLISH:
      this.emit('message:publish', message)
      break
    case protocol.EVENT:
      this.emit('message:event', message)
      break
    case protocol.REVOKE:
      this.emit('message:revoke', message)
      break
  }
}

Socket.prototype.sendMessage = function(message) {
  var encoded = protocol.encode(message)
  this.emit('send', message)
  this._ws.send(encoded)
}
<commit_msg>Use WS rather than WebSocket so that the connection check works in node and the browser.<commit_after>var WS = require('ws')
  , EventEmitter = require('events').EventEmitter
  , inherits = require('inherits')
  , protocol = require('nydus-protocol')

module.exports = Socket

function Socket(host, wsOptions) {
  EventEmitter.call(this)
  this._host = host
  this._wsOptions = wsOptions
}
inherits(Socket, EventEmitter)

Socket.prototype.open = function() {
  if (!WS) {
    throw new Error('WebSockets are not supported')
  }

  if (!this._ws || this._ws.readyState >= WS.CLOSING) {
    this._ws = new WS(this._host, this._wsOptions)
    this._ws.onopen = this._onOpen.bind(this)
    this._ws.onclose = this._onClose.bind(this)
    this._ws.onerror = this._onError.bind(this)
    this._ws.onmessage = this._onMessage.bind(this)
  }

  return this
}

Socket.prototype.close = function() {
  if (this._ws && this._ws.readyState < WS.CLOSING) {
    this._ws.close()
  }

  return this
}

Socket.prototype._onOpen = function() {
  this.emit('connect')
}

Socket.prototype._onClose = function(event) {
  this.emit('disconnect', event)
}

Socket.prototype._onError = function(event) {
  this.emit('error', event)
}

Socket.prototype._onMessage = function(event) {
  try {
    var message = protocol.decode(event.data)
  } catch (err) {
    this._ws.close(1002, 'Invalid nydus message')
    this.emit('error', err)
    return
  }

  this.emit('message', message)
  switch (message.type) {
    case protocol.WELCOME:
      this.emit('message:welcome', message)
      break
    case protocol.CALL:
      this.emit('message:call', message)
      break
    case protocol.RESULT:
      this.emit('message:result', message)
      break
    case protocol.ERROR:
      this.emit('message:error', message)
      break
    case protocol.SUBSCRIBE:
      this.emit('message:subscribe', message)
      break
    case protocol.UNSUBSCRIBE:
      this.emit('message:unsubscribe', message)
      break
    case protocol.PUBLISH:
      this.emit('message:publish', message)
      break
    case protocol.EVENT:
      this.emit('message:event', message)
      break
    case protocol.REVOKE:
      this.emit('message:revoke', message)
      break
  }
}

Socket.prototype.sendMessage = function(message) {
  var encoded = protocol.encode(message)
  this.emit('send', message)
  this._ws.send(encoded)
}

{'fraction_non_alphanumeric': 0.13343264524366888, 'fraction_numerical': 0.0017024898914662695, 'mean_word_length': 3.355885078776645, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '4682167', 'n_tokens_mistral': 1538, 'n_tokens_neox': 1520, 'n_words': 297}