Sha256: 1948677dee39fc48d524ef7ff000927d2e11dc99a548decde896832af8165fda

Contents?: true

Size: 1.24 KB

Versions: 9

Compression:

Stored size: 1.24 KB

Contents

require 'json'
require 'sockjs/session'

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

  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}"
    end
  end

  def closed
    puts "CHANNEL CLOSED: #{self.inspect}"
    # Remove ourself from the available channels
    @@channels.delete(self)

    QueryTasks.new(self).close!
  end

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

end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
volt-0.7.10 lib/volt/server/socket_connection_handler.rb
volt-0.7.9 lib/volt/server/socket_connection_handler.rb
volt-0.7.8 lib/volt/server/socket_connection_handler.rb
volt-0.7.7 lib/volt/server/socket_connection_handler.rb
volt-0.7.6 lib/volt/server/socket_connection_handler.rb
volt-0.7.5 lib/volt/server/socket_connection_handler.rb
volt-0.7.4 lib/volt/server/socket_connection_handler.rb
volt-0.7.3 lib/volt/server/socket_connection_handler.rb
volt-0.7.2 lib/volt/server/socket_connection_handler.rb