Sha256: 50ced5b0c74de9200b2751136457dc2151bc794d39cab4ed260dc723782875bf

Contents?: true

Size: 1.15 KB

Versions: 27

Compression:

Stored size: 1.15 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
    
    puts "GOT: #{message.inspect}"
    @@dispatcher.dispatch(self, message)
  end
  
  def send_message(*args)
    str = JSON.dump([*args])
    
    send(str)
  end
  
  def closed
    puts "CHANNEL CLOSED"
    # Remove ourself from the available channels
    @@channels.delete(self)
    
    # Remove any listening channels
    ChannelTasks.new(self).close!
  end

end

Version data entries

27 entries across 27 versions & 1 rubygems

Version Path
volt-0.4.15 lib/volt/server/socket_connection_handler.rb
volt-0.4.14 lib/volt/server/socket_connection_handler.rb
volt-0.4.12 lib/volt/server/socket_connection_handler.rb
volt-0.4.11 lib/volt/server/socket_connection_handler.rb
volt-0.4.10 lib/volt/server/socket_connection_handler.rb
volt-0.4.9 lib/volt/server/socket_connection_handler.rb
volt-0.4.8 lib/volt/server/socket_connection_handler.rb