Class: Slanger::Handler
- Inherits:
-
Object
- Object
- Slanger::Handler
- Defined in:
- lib/slanger/handler.rb
Instance Attribute Summary collapse
-
#connection ⇒ Object
Returns the value of attribute connection.
Instance Method Summary collapse
- #authenticate ⇒ Object
-
#initialize(socket, handshake) ⇒ Handler
constructor
A new instance of Handler.
- #onclose ⇒ Object
-
#onmessage(msg) ⇒ Object
Dispatches message handling to method with same name as the event name.
- #pusher_ping(msg) ⇒ Object
- #pusher_pong(msg) ⇒ Object
- #pusher_subscribe(msg) ⇒ Object
- #pusher_unsubscribe(msg) ⇒ Object
- #valid_protocol_version? ⇒ Boolean
Constructor Details
#initialize(socket, handshake) ⇒ Handler
Returns a new instance of Handler.
16 17 18 19 20 21 22 |
# File 'lib/slanger/handler.rb', line 16 def initialize(socket, handshake) @socket = socket @handshake = handshake @connection = Connection.new(@socket) @subscriptions = {} authenticate end |
Instance Attribute Details
#connection ⇒ Object
Returns the value of attribute connection.
13 14 15 |
# File 'lib/slanger/handler.rb', line 13 def connection @connection end |
Instance Method Details
#authenticate ⇒ Object
54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/slanger/handler.rb', line 54 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 connection.establish end end |
#onclose ⇒ Object
45 46 47 48 49 50 51 52 |
# File 'lib/slanger/handler.rb', line 45 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) ⇒ Object
Dispatches message handling to method with same name as the event name
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/slanger/handler.rb', line 26 def (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. 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.}\n #{e.backtrace.join "\n"}" }) end |
#pusher_ping(msg) ⇒ Object
70 71 72 |
# File 'lib/slanger/handler.rb', line 70 def pusher_ping(msg) send_payload nil, "pusher:pong" end |
#pusher_pong(msg) ⇒ Object
74 |
# File 'lib/slanger/handler.rb', line 74 def pusher_pong(msg); end |
#pusher_subscribe(msg) ⇒ Object
76 77 78 79 80 81 82 83 84 85 |
# File 'lib/slanger/handler.rb', line 76 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) ⇒ Object
87 88 89 90 91 92 |
# File 'lib/slanger/handler.rb', line 87 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? ⇒ Boolean
66 67 68 |
# File 'lib/slanger/handler.rb', line 66 def valid_protocol_version? protocol_version.between?(3, 7) end |