<commit_before># encoding: utf-8

require_relative "./raw_websocket"

module SockJS
  module Transports
    class WebSocket < RawWebSocket
      extend Forwardable

      # Settings.
      self.prefix = /[^.]+\/([^.]+)\/websocket$/
      self.method = "GET"

      # Handlers.
      def handle_open(request)
        puts "~ Opening WS connection."
        match = request.path_info.match(self.class.prefix)
        session = self.create_session(request.path_info)
        session.buffer = Buffer.new # This is a hack for the bloody API. Rethinking and refactoring required!
        session.transport = self

        # Send the opening frame.
        session.open!

        session.process_buffer # Run the app (connection.session_open hook).
      end

      def handle_message(request, event)
        message = event.data

        # Unlike other transports, the WS one is supposed to ignore empty messages.
        unless message.empty?
          message = "[#{message}]" unless message.start_with?("[")
          puts "<~ WS message received: #{message.inspect}"
          session = self.get_session(request.path_info)
          session.receive_message(request, message)

          # Send encoded messages in an array frame.
          messages = session.process_buffer
          if messages.start_with?("a[") # a[] frames are sent immediatelly! FIXME!
            puts "~ Messages to be sent: #{messages.inspect}"
            @ws.send(messages)<commit_msg>Assign @ws to the session object.<commit_after># encoding: utf-8

require_relative "./raw_websocket"

module SockJS
  module Transports
    class WebSocket < RawWebSocket
      extend Forwardable

      # Settings.
      self.prefix = /[^.]+\/([^.]+)\/websocket$/
      self.method = "GET"

      # Handlers.
      def handle_open(request)
        puts "~ Opening WS connection."
        match = request.path_info.match(self.class.prefix)
        session = self.create_session(request.path_info)
        session.ws = @ws
        session.buffer = Buffer.new # This is a hack for the bloody API. Rethinking and refactoring required!
        session.transport = self

        # Send the opening frame.
        session.open!

        session.process_buffer # Run the app (connection.session_open hook).
      end

      def handle_message(request, event)
        message = event.data

        # Unlike other transports, the WS one is supposed to ignore empty messages.
        unless message.empty?
          message = "[#{message}]" unless message.start_with?("[")
          puts "<~ WS message received: #{message.inspect}"
          session = self.get_session(request.path_info)
          session.receive_message(request, message)

          # Send encoded messages in an array frame.
          messages = session.process_buffer
          if messages.start_with?("a[") # a[] frames are sent immediatelly! FIXME!
            puts "~ Messages to be sent: #{messages.inspect}"
            @ws.send(messages)
{'fraction_non_alphanumeric': 0.10097939885173927, 'fraction_numerical': 0.0006754474839581223, 'mean_word_length': 2.4163783160322954, 'pattern_counts': {'":': 0, '<': 7, '<?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': '4872539', 'n_tokens_mistral': 825, 'n_tokens_neox': 795, 'n_words': 234}