Sha256: 957757bbdd430aecdd3bb6893335008d31ac5c431197eefde80aa1bcc2652372

Contents?: true

Size: 1023 Bytes

Versions: 3

Compression:

Stored size: 1023 Bytes

Contents

require 'json'
require 'sockjs/session'

class ChannelHandler < SockJS::Session
  # Create one instance of the dispatcher
  
  def self.dispatcher=(val)
    @@dispatcher = val
  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    
    # Remove ourself from the available channels
    @@channels.delete(self)
  end

end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
volt-0.4.0 lib/volt/server/channel_handler.rb
volt-0.3.9 lib/volt/server/channel_handler.rb
volt-0.3.8 lib/volt/server/channel_handler.rb