class WebsocketRails::Event
Contains all of the relevant information for incoming and outgoing events. All events except for channel events will have a connection object associated.
Events
require an event name and hash of options:
:data => The data object will be passed to any callback functions bound on the client side.
You can also pass a Hash of options to specify:
:connection => Connection that will be receiving or that sent this event.
:namespace => The namespace this event is under. Will default to :global If the namespace is nested under multiple levels pass them as an array. For instance, if the namespace route looks like the following:
namespace :products do namespace :hats do # events end end
Then you would pass the namespace argument as [:products,:hats]
:channel => The name of the channel that this event is destined for.
Constants
- FAILED
- FINISHED_WITHOUT_RESULT
- SUCCEEDED
Attributes
Public Class Methods
# File lib/websocket_rails/event.rb, line 70 def self.log_header "Event" end
# File lib/websocket_rails/event.rb, line 109 def initialize(event_name, options={}) case event_name when String namespace = event_name.split('.') @name = namespace.pop.to_sym when Symbol @name = event_name namespace = [:global] end @id = options[:id] @data = options[:data].is_a?(Hash) ? options[:data].with_indifferent_access : options[:data] @channel = options[:channel].to_sym rescue options[:channel].to_s.to_sym if options[:channel] @token = options[:token] if options[:token] @connection = options[:connection] @server_token = options[:server_token] @user_id = options[:user_id] @propagate = options[:propagate].nil? ? true : options[:propagate] @namespace = validate_namespace( options[:namespace] || namespace ) end
# File lib/websocket_rails/event.rb, line 74 def self.new_from_json(encoded_data, connection) case encoded_data when String event_name, data = JSON.parse encoded_data unless event_name.is_a?(String) && data.is_a?(Hash) raise UnknownDataType end data = data.merge(:connection => connection).with_indifferent_access Event.new event_name, data # when Array # TODO: Handle file #File.open("/tmp/test#{rand(100)}.jpg", "wb") do |file| # encoded_data.each do |byte| # file << byte.chr # end #end else raise UnknownDataType end rescue JSON::ParserError, UnknownDataType => ex warn "Invalid Event Received: #{ex}" debug "Event Data: #{encoded_data}" log_exception(ex) Event.new_on_invalid_event_received(connection, nil) end
Public Instance Methods
# File lib/websocket_rails/event.rb, line 129 def as_json [ encoded_name, { :id => id, :channel => channel, :user_id => user_id, :data => data, :success => success, :result => result, :token => token, :server_token => server_token } ] end
# File lib/websocket_rails/event.rb, line 173 def encoded_name if namespace.size > 1 child_namespace = namespace.dup[1..-1] child_namespace << name combined_name = child_namespace.join('.') else combined_name = name end combined_name end
# File lib/websocket_rails/event.rb, line 149 def is_channel? !@channel.nil? end
# File lib/websocket_rails/event.rb, line 161 def is_internal? namespace.include?(:websocket_rails) end
# File lib/websocket_rails/event.rb, line 157 def is_invalid? name == :invalid_event end
# File lib/websocket_rails/event.rb, line 153 def is_user? !@user_id.nil? && !is_channel? end
# File lib/websocket_rails/event.rb, line 145 def serialize as_json.to_json end
# File lib/websocket_rails/event.rb, line 165 def should_propagate? @propagate end
# File lib/websocket_rails/event.rb, line 169 def trigger connection.trigger self if connection end
Private Instance Methods
# File lib/websocket_rails/event.rb, line 186 def validate_namespace(namespace) namespace = [namespace] unless namespace.is_a?(Array) namespace.unshift :global unless namespace.first == :global namespace.map(&:to_sym) rescue [:global] end