Sha256: d6290eda53b8a24385a88ce8db302d904252ff123500194812bb4177d92d80f4

Contents?: true

Size: 1.61 KB

Versions: 15

Compression:

Stored size: 1.61 KB

Contents

require 'json'
require 'sockjs/session'
require File.join(File.dirname(__FILE__), '../../../app/volt/tasks/query_tasks')

module Volt
  class SocketConnectionHandler < SockJS::Session
    # Create one instance of the dispatcher

    # We track the connected user_id with the channel for use with permissions.
    # This may be changed as new listeners connect, which is fine.
    attr_accessor :user_id

    def self.dispatcher=(val)
      @@dispatcher = val
    end

    def self.dispatcher
      @@dispatcher
    end

    # Sends a message to all, optionally skipping a users channel
    def self.send_message_all(skip_channel = nil, *args)
      @@channels.each do |channel|
        if skip_channel && channel == skip_channel
          next
        end
        channel.send_message(*args)
      end
    end

    def initialize(session, *args)
      @session = session

      @@channels ||= []
      @@channels << self

      super
    end

    def process_message(message)
      # self.class.message_all(message)
      # Messages are json and wrapped in an array
      message = JSON.parse(message).first

      @@dispatcher.dispatch(self, message)
    end

    def send_message(*args)
      str = JSON.dump([*args])

      begin
        send(str)
      rescue MetaState::WrongStateError => e
        puts "Tried to send to closed connection: #{e.inspect}"

        # Mark this channel as closed
        closed
      end
    end

    def closed
      # Remove ourself from the available channels
      @@channels.delete(self)

      QueryTasks.new(self).close!
    end

    def inspect
      "<#{self.class}:#{object_id}>"
    end
  end
end

Version data entries

15 entries across 15 versions & 1 rubygems

Version Path
volt-0.9.1.pre1 lib/volt/server/socket_connection_handler.rb
volt-0.9.0 lib/volt/server/socket_connection_handler.rb
volt-0.9.0.pre7 lib/volt/server/socket_connection_handler.rb
volt-0.9.0.pre6 lib/volt/server/socket_connection_handler.rb
volt-0.9.0.pre5 lib/volt/server/socket_connection_handler.rb
volt-0.9.0.pre4 lib/volt/server/socket_connection_handler.rb
volt-0.9.0.pre3 lib/volt/server/socket_connection_handler.rb
volt-0.9.0.pre2 lib/volt/server/socket_connection_handler.rb
volt-0.9.0.pre1 lib/volt/server/socket_connection_handler.rb
volt-0.8.27.beta9 lib/volt/server/socket_connection_handler.rb
volt-0.8.27.beta8 lib/volt/server/socket_connection_handler.rb
volt-0.8.27.beta7 lib/volt/server/socket_connection_handler.rb
volt-0.8.27.beta6 lib/volt/server/socket_connection_handler.rb
volt-0.8.27.beta5 lib/volt/server/socket_connection_handler.rb
volt-0.8.27.beta4 lib/volt/server/socket_connection_handler.rb