Class: Slanger::Handler

Inherits:
Object
  • Object
show all
Defined in:
lib/slanger/handler.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(socket, handshake) ⇒ Handler

Returns a new instance of Handler.



17
18
19
20
21
22
23
# File 'lib/slanger/handler.rb', line 17

def initialize(socket, handshake)
  @socket        = socket
  @handshake     = handshake
  @connection    = Connection.new(@socket)
  @subscriptions = {}
  authenticate
end

Instance Attribute Details

#connectionObject

Returns the value of attribute connection.



14
15
16
# File 'lib/slanger/handler.rb', line 14

def connection
  @connection
end

Instance Method Details

#authenticateObject



58
59
60
61
62
63
64
65
66
67
68
# 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

#oncloseObject



47
48
49
50
51
52
53
54
55
56
# 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) ⇒ Object

Dispatches message handling to method with same name as the event name



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# 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) ⇒ Object



74
75
76
# File 'lib/slanger/handler.rb', line 74

def pusher_ping(msg)
  send_payload nil, 'pusher:pong'
end

#pusher_pong(msg) ⇒ Object



78
# File 'lib/slanger/handler.rb', line 78

def pusher_pong msg; end

#pusher_subscribe(msg) ⇒ Object



80
81
82
83
84
85
86
87
88
89
# 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) ⇒ Object



91
92
93
94
95
96
# 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?Boolean

Returns:

  • (Boolean)


70
71
72
# File 'lib/slanger/handler.rb', line 70

def valid_protocol_version?
  protocol_version.between?(3, 7)
end