<commit_before>'use strict';

var WebSocketClient = require('websocket').client;
var EventEmitter = require('events').EventEmitter;

var parse = require('./util.js').parse;

var connect = function(opts, callback) {
  var client = new WebSocketClient();

  client.on('connect', function(conn) {
    if (opts.verbose) { console.log('Connected'); }
    var events = new EventEmitter(opts);

    conn.on('message', function(msg) {
      var parsed;
      try {
        parsed = parse(msg);
      } catch (parseError) {
        if (opts.verbose) { console.log('Trouble parsing JSON from message on websocket: ' + parseError.message); }
        return;
      }
      if (opts.verbose) { console.log('Got ' + parsed[0] + ' message'); }
      events.emit.apply(events, parsed);
    });

    var send = function(type) {
      if (typeof type !== 'string') {
        throw new Error('Message type must be a string');
      }
      if (opts.verbose) { console.log('Sending ' + type + ' message'); }
      conn.send(JSON.stringify(Array.prototype.slice.call(arguments)));
    };

    client.messages = events;
    client.send = send;
    client.disconnect = conn.close;

    callback(undefined, client);
  });

  var url = [ 'ws://', opts.host || 'localhost', ':', opts.port || 80, opts.path || '/' ].join('');
  client.connect(url, opts.protocol || 'json-socket');
};

module.exports = connect;
<commit_msg>Added some more logging, TODOs<commit_after>'use strict';

var WebSocketClient = require('websocket').client;
var EventEmitter = require('events').EventEmitter;

var parse = require('./util.js').parse;

var connect = function(opts, callback) {
  var client = new WebSocketClient();

  client.on('connect', function(conn) {
    if (opts.verbose) { console.log('Connected'); }
    var events = new EventEmitter(opts);

    conn.on('message', function(msg) {
      var parsed;
      try {
        parsed = parse(msg);
      } catch (parseError) {
        if (opts.verbose) { console.log('Trouble parsing JSON from message on websocket: ' + parseError.message); }
        return;
      }
      if (opts.verbose) { console.log('Got ' + parsed[0] + ' message'); }
      events.emit.apply(events, parsed);
    });

    var send = function(type) {
      if (typeof type !== 'string') {
        throw new Error('Message type must be a string');
      }
      if (opts.verbose) { console.log('Sending ' + type + ' message'); }
      conn.send(JSON.stringify(Array.prototype.slice.call(arguments)));
    };

    if (opts.verbose) { conn.on('close', function() { console.log('Disconnected'); }); }

    client.messages = events;
    client.send = send;
    client.disconnect = conn.close;

    callback(undefined, client); //TODO: give back the conn as well?
    //Nah, lets hide all kinds of stuff, fuck getting access to the raw sockets
  });

  var url = [ 'ws://', opts.host || 'localhost', ':', opts.port || 80, opts.path || '/' ].join('');
  client.connect(url, opts.protocol || 'json-socket');
};

module.exports = connect;

{'fraction_non_alphanumeric': 0.16749585406301823, 'fraction_numerical': 0.001990049751243781, 'mean_word_length': 3.103401360544218, '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': 1, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4761362', 'n_tokens_mistral': 923, 'n_tokens_neox': 902, 'n_words': 226}