Class: Webmate::Websockets
- Inherits:
-
Object
- Object
- Webmate::Websockets
- Defined in:
- lib/webmate/websockets.rb
Class Method Summary collapse
- .subscribe(session_id, request, &block) ⇒ Object
- .subscribe_to_personal_channel(user_id, websocket) ⇒ Object
Class Method Details
.subscribe(session_id, request, &block) ⇒ Object
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/webmate/websockets.rb', line 4 def subscribe(session_id, request, &block) user_id = request.env['warden'].try(:user).try(:id) request.websocket do |websocket| # subscribe user to redis channel subscribe_to_personal_channel(user_id, websocket) websocket.onopen do websocket.send(Webmate::SocketIO::Packets::Connect.new.to_packet) warn("Socket connection opened for session_id: #{session_id}") end websocket. do || request = Webmate::SocketIO::Packets::Base.parse() response = begin block.call(request) || Responders::Response.build_not_found("Action not found: #{request.path}") rescue Exception => e Responders::Response.build_error("Error while processing request: #{request.path}, #{e.}") end websocket.send(response.to_packet) end websocket.onclose do warn("Socket connection closed '#{session_id}'") end end end |
.subscribe_to_personal_channel(user_id, websocket) ⇒ Object
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/webmate/websockets.rb', line 32 def subscribe_to_personal_channel(user_id, websocket) channel_name = Webmate::Application.get_channel_name_for(user_id) subscriber = EM::Hiredis.connect.pubsub subscriber.subscribe(channel_name) warn("user has been subscribed to channel '#{channel_name}'") subscriber.on(:message) do |channel, | response_data = Webmate::JSON.parse() packet = Webmate::SocketIO::Packets::Message.new(response_data) websocket.send(packet.to_packet) end subscriber end |