<commit_before>// A client websocket connect which attempts to reconnect after disconnection and parse JSON message

// #Modules
var EventEmitter = require( 'events' ).EventEmitter;
var util = require( 'util' );

// #NPM 
var WS = require( 'ws' );

// #JSONSocketConnection
// Options
//  * socketUrl <String>
//  * attemptReconnectionAfterMS <Number> - wait until attempting reconnection. Default 1000.
// Events
//  * 'open'
//  * 'closed'
//  * 'reconnecting'
//  * 'closed successfully'
//  * 'json'
//  * 'nonjson'
//  * 'message'
function JSONSocketConnection ( options ) {
    this.socketUrl = options.socketUrl;
    this._connectionDesired = false;
    this.attemptReconnectionAfterMS = (typeof options.attemptReconnectionAfterMS !== 'undefined') ? options.attemptReconnectionAfterMS : 10000;
}

// Add event emitter
util.inherits(JSONSocketConnection, EventEmitter);

JSONSocketConnection.prototype.openSocketConnection = function () {
    this._connectionDesired = true;
    var self = this;
    console.log( 'openSocketConnection' );
    // if ( this.socket && ( this.socket.readyState === WebSocket.CONNECTING || this.socket.readyState === WebSocket.OPEN ||  this.socket.readyState === WebSocket.CLOSING ) ) {
    //     // Already connecting
    //     var state  = '';
    //     setTimeout( function () { 
    //         self.openSocketConnection.call( self );
    //     }, self.attemptReconnectionAfterMS );
    //     return;
    // } else if ( this.socket ) {
    //     // self.socket.onclose = undefined;
    //     // self.socket.onmessage = undefined;
    //     // self.socket.onerror = undefined;
    //     // delete self.socket;
    // }
    console.log( 'Connecting 123', self.socketUrl );
    //this.socket = null;
    try {
        self.socket = new WS( self.socketUrl ); 
    } catch (err) {
        console.log( 'Failed to create WS' );
        return self.emit( 'error', err );
    }
    self.socket.on( 'open', function () {
        self.emit('open');
    });

    self.socket.on( 'close', function () {
        shouldReconnect();
        if ( !self._connectionDesired ) {
            self.emit( 'closed successfully' );
        }
    });

    self.socket.on( 'error', function ( err ) {
        if ( err.code && err.code === 'ECONNREFUSED' ) {
            console.log( 'connnection refused', 'will close' );
            shouldReconnect();
        } else {
            console.log( 'ws error', err );
        }
    });

    self.socket.on( 'message', function ( message ) {<commit_msg>Cleaned up comments and logs<commit_after>// A client websocket connect which attempts to reconnect after disconnection and parse JSON message

// #Modules
var EventEmitter = require( 'events' ).EventEmitter;
var util = require( 'util' );

// #NPM 
var WS = require( 'ws' );

// #JSONSocketConnection
// Options
//  * socketUrl <String>
//  * attemptReconnectionAfterMS <Number> - wait until attempting reconnection. Default 1000.
// Events
//  * 'open'
//  * 'closed'
//  * 'reconnecting'
//  * 'closed successfully'
//  * 'json'
//  * 'nonjson'
//  * 'message'
function JSONSocketConnection ( options ) {
    this.socketUrl = options.socketUrl;
    this._connectionDesired = false;
    this.attemptReconnectionAfterMS = (typeof options.attemptReconnectionAfterMS !== 'undefined') ? options.attemptReconnectionAfterMS : 10000;
}

// Add event emitter
util.inherits(JSONSocketConnection, EventEmitter);

JSONSocketConnection.prototype.openSocketConnection = function () {
    this._connectionDesired = true;
    var self = this;
    try {
        self.socket = new WS( self.socketUrl ); 
    } catch (err) {
        console.log( 'Failed to create WS' );
        return self.emit( 'error', err );
    }
    self.socket.on( 'open', function () {
        self.emit('open');
    });

    self.socket.on( 'close', function () {
        shouldReconnect();
        if ( !self._connectionDesired ) {
            self.emit( 'closed successfully' );
        }
    });

    self.socket.on( 'error', function ( err ) {
        if ( err.code && err.code === 'ECONNREFUSED' ) {
            console.log( 'connnection refused', 'will close' );
            shouldReconnect();
        } else {
            console.log( 'ws error', err );
        }
    });

    self.socket.on( 'message', function ( message ) {
{'fraction_non_alphanumeric': 0.14613778705636743, 'fraction_numerical': 0.0048712595685455815, 'mean_word_length': 2.5257563368765332, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, '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': '3603406', 'n_tokens_mistral': 1296, 'n_tokens_neox': 1260, 'n_words': 297}