<commit_before># encoding: utf-8

require "sockjs/adapter"

module SockJS
  module Adapters
    class WebSocket < Adapter
      # Settings.
      self.prefix  = /[^.]+\/([^.]+)\/websocket$/
      self.method  = "GET"

      def invalid_request_or_disabled_websocket?(request)
        if self.disabled?
          status, body = 404, "WebSockets Are Disabled"
        elsif request.env["HTTP_UPGRADE"] != "WebSocket"
          status, body = 400, 'Can "Upgrade" only to "WebSocket".'
        elsif request.env["HTTP_CONNECTION"] != "Upgrade"
          status, body = 400, '"Connection" must be "Upgrade".'
        else
          return false
        end

        self.write_response(request, status, Hash.new, body)
      end

      # Handlers.
      def handle(request)
        unless invalid_request_or_disabled_websocket?(request)
          puts "~ Upgrading to WebSockets ..."

          ws = Faye::WebSocket.new(request.env)
          handler = ::SockJS::Adapters::WebSocket.new(@connection, @options)

          handler.handle_open(request, ws)

          ws.onmessage = lambda do |event|
            debug "~ WS data received: #{event.data.inspect}"
            handler.handle_message(request, event, ws)
          end

          ws.onclose = lambda do |event|
            debug "~ Closing WebSocket connection (#{event.code}, #{event.reason})"
            handler.handle_close(request, ws)
          end

          # Thin async response
          ::SockJS::Thin::DUMMY_RESPONSE
        end
      end

      def handle_open(request, ws)
        p [:ws_open]
        ws.send("o")
      end

      def handle_close(request, ws)
        p [:ws_close]
        ws.send("c")
      end

      def handle_message(request, event, ws)
        p [:ws_message, event.data]
        ws.send(event.data) # not this way, it's an app responsibility
      end
    end
  end
end<commit_msg>WebSocket transport logic.<commit_after># encoding: utf-8

require "sockjs/adapter"

module SockJS
  module Adapters
    class WebSocket < Adapter
      # Settings.
      self.prefix  = /[^.]+\/([^.]+)\/websocket$/
      self.method  = "GET"

      def invalid_request_or_disabled_websocket?(request)
        if self.disabled?
          status, body = 404, "WebSockets Are Disabled"
        elsif request.env["HTTP_UPGRADE"] != "WebSocket"
          status, body = 400, 'Can "Upgrade" only to "WebSocket".'
        elsif request.env["HTTP_CONNECTION"] != "Upgrade"
          status, body = 400, '"Connection" must be "Upgrade".'
        else
          return false
        end

        self.write_response(request, status, Hash.new, body)
      end

      # Handlers.
      def handle(request)
        unless invalid_request_or_disabled_websocket?(request)
          puts "~ Upgrading to WebSockets ..."

          ws = Faye::WebSocket.new(request.env)
          handler = ::SockJS::Adapters::WebSocket.new(@connection, @options)

          handler.handle_open(request, ws)

          ws.onmessage = lambda do |event|
            debug "~ WS data received: #{event.data.inspect}"
            handler.handle_message(request, event, ws)
          end

          ws.onclose = lambda do |event|
            debug "~ Closing WebSocket connection (#{event.code}, #{event.reason})"
            handler.handle_close(request, ws)
          end

          # Thin async response
          ::SockJS::Thin::DUMMY_RESPONSE
        end
      end

      def handle_open(request, ws)
        match = request.path_info.match(self.class.prefix)
        session = self.connection.create_session(match[1])
        body = self.format_frame(session.open!)
        ws.send(body)
      end

      def handle_close(request, ws)
        session = self.connection.sessions[match[1]]
        session.close
      end

      def handle_message(request, event, ws)
        session.receive_message(event.data)
        session.process_buffer
      end
    end
  end
end
{'fraction_non_alphanumeric': 0.11173612886729736, 'fraction_numerical': 0.005625159805676298, 'mean_word_length': 2.139646869983949, 'pattern_counts': {'":': 0, '<': 5, '<?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': '2639795', 'n_tokens_mistral': 1238, 'n_tokens_neox': 1147, 'n_words': 272}