<commit_before>function createWs(options) {

    var WebSocketClass = window.WebSocket || window.MozWebSocket,
        socket,
        connected = false,
        closedByMe = false,
        connectTimer, reconnectTimer, idleTimer,
        reconnectTime = 1000,
        initialReconnectTime = 1000,
        maxReconnectTime = 60000,
        messageQueue = []

    options = _.extend({
        url: null,
        connectionTimeout: 1000,
        connectTime: 10000,
        idleTimeout: 5 * 60000
    }, options)

    var ws = emitter({
        connect: connect,

        send: function(message) {
          var strMessage = JSON.stringify(message)
          messageQueue.push(strMessage)
          dequeue()
        },

        close: function() {
            closedByMe = true
            log('explicit close requested')
            try {
                socket && socket.close()
            } catch (e) {
                logError("socket closing bug", e.stack || e)
            }
            connected = false
        },

        reconnect: function() {
          ws.close()
          ws.connect()
        },

        release: function() {
            clearTimeout(reconnectTimer)
        }
    })

    return ws

    // private stuff

    function connect(re) {
        log("connect to url: " + options.url)

        socket = new WebSocketClass(options.url)
        closedByMe = false
        connected = false

        connectTimer = setTimeout(onerror, options.connectTime)

        socket.onopen = function(){  
          //log('Socket Status: ', socket.readyState, ' (open)')
          clearTimeout(connectTimer)
          reconnectTime = initialReconnectTime
          connected = true
          dequeue()
          ws.emit('connect')
          re && ws.emit('reconnect')
        }  

        socket.onmessage = function(msg){  
          console.log('%c Received: %s', "color: #46af91;", msg.data)
          handleMessage(msg.data)
        }  

        socket.onclose = function(){  
          //log('Socket Status: ', socket.readyState, ' (Closed)')<commit_msg>when socket is not yet open it was not allowed to close it (fired exception)<commit_after>function createWs(options) {

    var WebSocketClass = window.WebSocket || window.MozWebSocket,
        socket,
        connected = false,
        closedByMe = false,
        closeWhenConnected = false,
        connectTimer, reconnectTimer, idleTimer,
        reconnectTime = 1000,
        initialReconnectTime = 1000,
        maxReconnectTime = 60000,
        messageQueue = []

    options = _.extend({
        url: null,
        connectionTimeout: 1000,
        connectTime: 10000,
        idleTimeout: 5 * 60000
    }, options)

    var ws = emitter({
        connect: connect,

        send: function(message) {
          var strMessage = JSON.stringify(message)
          messageQueue.push(strMessage)
          dequeue()
        },

        close: function() {
            closedByMe = true
            log('explicit close requested')
            if(!connected) { return closeWhenConnected = true  }//when closing before connection established
            try {
              socket && socket.close()
            } catch (e) {
              logError("socket closing bug", e.stack || e)
            }
            connected = false
        },

        reconnect: function() {
          ws.close()
          ws.connect()
        },

        release: function() {
            clearTimeout(reconnectTimer)
        }
    })

    return ws

    // private stuff

    function connect(re) {
        log("connect to url: " + options.url)

        socket = new WebSocketClass(options.url)
        closedByMe = false
        connected = false

        connectTimer = setTimeout(onerror, options.connectTime)

        socket.onopen = function(){  
          //log('Socket Status: ', socket.readyState, ' (open)')
          clearTimeout(connectTimer)
          reconnectTime = initialReconnectTime
          connected = true
          if(closeWhenConnected) {
            closeWhenConnected = false
            ws.close()
          }
          dequeue()
          ws.emit('connect')
          re && ws.emit('reconnect')
        }  

        socket.onmessage = function(msg){  
          console.log('%c Received: %s', "color: #46af91;", msg.data)
          handleMessage(msg.data)
        }  

        socket.onclose = function(){  
          //log('Socket Status: ', socket.readyState, ' (Closed)')
{'fraction_non_alphanumeric': 0.10375028070963395, 'fraction_numerical': 0.014372333258477431, 'mean_word_length': 1.712545676004872, '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': '708244', 'n_tokens_mistral': 1255, 'n_tokens_neox': 1149, 'n_words': 270}