<commit_before>module EventMachine
  module WebSocketIO
    class Client
      include EventEmitter
      attr_reader :url, :session, :running
      attr_accessor :timeout

      def initialize(url)
        raise ArgumentError, "invalid websocket URL \"#{url}\"" unless url =~ /^ws:\/\/.+/
        @url = url
        @running = false
        @__reconnect_timer_id = nil
        @ws = nil
        self.on :__session_id do |session_id|
          @session = session_id
          self.emit :connect, session_id
        end
      end

      def connect
        url = @session ? "#{@url}/session=#{@session}" : @url
        @ws = EventMachine::WebSocketClient.connect url
        @running = true

        @ws.stream do |msg|
          data = JSON.parse msg
          self.emit data['type'], data['data']
        end

        @ws.callback do
          self.push :__session_id
        end

        @ws.errback do |err|
          self.emit :error, err
        end

        @ws.disconnect do
          self.emit :disconnect
          if @running
            @__reconnect_timer_id = EM::add_timer 10 do
              connect
            end
          end
        end

        return self
      end

      def close
        @running = false
        @ws.close_connection if @ws
        EM::cancel_timer @__reconnect_timer_id
      end

      def push(type, data={})
        @ws.send_msg({:type => type, :data => data, :session => self.session}.to_json)
      end

    end
  end
end
<commit_msg>fix for em-websocket-client v0.1.2  #2<commit_after>module EventMachine
  module WebSocketIO
    class Client
      include EventEmitter
      attr_reader :url, :session, :running
      attr_accessor :timeout

      def initialize(url)
        raise ArgumentError, "invalid websocket URL \"#{url}\"" unless url =~ /^ws:\/\/.+/
        @url = url
        @running = false
        @__reconnect_timer_id = nil
        @ws = nil
        self.on :__session_id do |session_id|
          @session = session_id
          self.emit :connect, session_id
        end
      end

      def connect
        url = @session ? "#{@url}/session=#{@session}" : @url
        @ws = EventMachine::WebSocketClient.connect url
        @running = true

        @ws.stream do |msg|
          data = JSON.parse msg.data
          self.emit data['type'], data['data']
        end

        @ws.callback do
          self.push :__session_id
        end

        @ws.errback do |err|
          self.emit :error, err
        end

        @ws.disconnect do
          self.emit :disconnect
          if @running
            @__reconnect_timer_id = EM::add_timer 10 do
              connect
            end
          end
        end

        return self
      end

      def close
        @running = false
        @ws.close_connection if @ws
        EM::cancel_timer @__reconnect_timer_id
      end

      def push(type, data={})
        @ws.send_msg({:type => type, :data => data, :session => self.session}.to_json)
      end

    end
  end
end

{'fraction_non_alphanumeric': 0.09759358288770054, 'fraction_numerical': 0.00267379679144385, 'mean_word_length': 1.6463306808134395, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 9, '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': '911506', 'n_tokens_mistral': 892, 'n_tokens_neox': 868, 'n_words': 238}