Class: FrCable::HttpHandler

Inherits:
Object
  • Object
show all
Defined in:
lib/fr_cable/http_handler.rb

Instance Method Summary collapse

Instance Method Details

#channel_class(room) ⇒ Object



4
5
6
7
8
9
10
11
# File 'lib/fr_cable/http_handler.rb', line 4

def channel_class(room)
  @channel_class ||= {}
  @channel_class[room] ||= begin
    splited_room_name = room.split(":")
    splited_room_name[0].constantize.new({room: splited_room_name[1]})
  end
  @channel_class[room]
end

#connect(connection_uuid) ⇒ Object



47
48
49
50
51
52
53
54
# File 'lib/fr_cable/http_handler.rb', line 47

def connect connection_uuid
  result = connection_class.connected(connection_uuid)
  if result
    ::FrCable::HTTPClient.post(::FrCable::Rack.config[:socket_server_url], body: {type: 'accept_connection', payload: connection_uuid}.to_json)
  else
    ::FrCable::HTTPClient.post(::FrCable::Rack.config[:socket_server_url], body: {type: 'deny_connection', payload: connection_uuid}.to_json)
  end
end

#connection_classObject



13
14
15
# File 'lib/fr_cable/http_handler.rb', line 13

def connection_class
  @connection_class ||= ::ApplicationCable::Connection.new
end

#disconnect(connection_uuid) ⇒ Object



56
57
58
# File 'lib/fr_cable/http_handler.rb', line 56

def disconnect connection_uuid
  connection_class.disconnected(connection_uuid)
end

#message(payload) ⇒ Object



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

def message payload
  connection_uuid = payload["connection_uuid"]
  room = payload["room"]
  message = payload["message"]
  
  channel_class(room).receive(message)
end

#subscribe(payload) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/fr_cable/http_handler.rb', line 25

def subscribe payload
  connection_uuid = payload["connection"]
  room = payload["room"]
  uuid = payload["subscription"]

  result = channel_class(room).subscribed(connection_uuid)
  if result
    ::FrCable::HTTPClient.post(::FrCable::Rack.config[:socket_server_url], body: {type: 'accept_subscription', payload: uuid}.to_json)
  else
    ::FrCable::HTTPClient.post(::FrCable::Rack.config[:socket_server_url], body: {type: 'deny_subscription', payload: uuid}.to_json)
  end
end

#unsubscribe(payload) ⇒ Object



38
39
40
41
42
43
44
45
# File 'lib/fr_cable/http_handler.rb', line 38

def unsubscribe payload
  connection_uuid = payload[:connection_uuid]
  room = payload[:room]
  uuid = payload[:uuid]

  channel_class(room).unsubscribed(connection_uuid)

end