class Slanger::Handler
Attributes
connection[RW]
Public Class Methods
new(socket, handshake)
click to toggle source
# File lib/slanger/handler.rb, line 17 def initialize(socket, handshake) @socket = socket @handshake = handshake @connection = Connection.new(@socket) @subscriptions = {} authenticate end
Public Instance Methods
authenticate()
click to toggle source
# File lib/slanger/handler.rb, line 58 def authenticate if !valid_app_key? app_key error({ code: 4001, message: "Could not find app by key #{app_key}" }) @socket.close_websocket elsif !valid_protocol_version? error({ code: 4007, message: "Unsupported protocol version" }) @socket.close_websocket else return connection.establish end end
onclose()
click to toggle source
# File lib/slanger/handler.rb, line 47 def onclose subscriptions = @subscriptions.select { |k,v| k && v } subscriptions.each_key do |channel_id| subscription_id = subscriptions[channel_id] Channel.unsubscribe channel_id, subscription_id end end
onmessage(msg)
click to toggle source
Dispatches message handling to method with same name as the event name
# File lib/slanger/handler.rb, line 27 def onmessage(msg) msg = Oj.strict_load(msg) msg['data'] = Oj.strict_load(msg['data']) if msg['data'].is_a? String event = msg['event'].gsub(/\Apusher:/, 'pusher_') if event =~ /\Aclient-/ msg['socket_id'] = connection.socket_id Channel.send_client_message msg elsif respond_to? event, true send event, msg end rescue JSON::ParserError error({ code: 5001, message: "Invalid JSON" }) rescue Exception => e error({ code: 500, message: "#{e.message}\n #{e.backtrace.join "\n"}" }) end
pusher_ping(msg)
click to toggle source
# File lib/slanger/handler.rb, line 74 def pusher_ping(msg) send_payload nil, 'pusher:pong' end
pusher_pong(msg;)
click to toggle source
# File lib/slanger/handler.rb, line 78 def pusher_pong msg; end
pusher_subscribe(msg)
click to toggle source
# File lib/slanger/handler.rb, line 80 def pusher_subscribe(msg) channel_id = msg['data']['channel'] klass = subscription_klass channel_id if @subscriptions[channel_id] error({ code: nil, message: "Existing subscription to #{channel_id}" }) else @subscriptions[channel_id] = klass.new(connection.socket, connection.socket_id, msg).subscribe end end
pusher_unsubscribe(msg)
click to toggle source
# File lib/slanger/handler.rb, line 91 def pusher_unsubscribe(msg) channel_id = msg['data']['channel'] subscription_id = @subscriptions.delete(channel_id) Channel.unsubscribe channel_id, subscription_id end
valid_protocol_version?()
click to toggle source
# File lib/slanger/handler.rb, line 70 def valid_protocol_version? protocol_version.between?(3, 7) end
Private Instance Methods
app_key()
click to toggle source
# File lib/slanger/handler.rb, line 100 def app_key @handshake.path.split(/\W/)[2] end
protocol_version()
click to toggle source
# File lib/slanger/handler.rb, line 104 def protocol_version @query_string ||= Rack::Utils.parse_nested_query(@handshake.query_string) @query_string["protocol"].to_i || -1 end
subscription_klass(channel_id)
click to toggle source
# File lib/slanger/handler.rb, line 113 def subscription_klass channel_id klass = channel_id.match(/\A(private|presence)-/) do |match| Slanger.const_get "#{match[1]}_subscription".classify end klass || Slanger::Subscription end
valid_app_key?(app_key)
click to toggle source
# File lib/slanger/handler.rb, line 109 def valid_app_key? app_key Slanger::Config.app_key == app_key end