Class: PusherFake::Connection
- Inherits:
-
Object
- Object
- PusherFake::Connection
- Defined in:
- lib/pusher-fake/connection.rb
Constant Summary collapse
- CLIENT_EVENT_MATCHER =
Name matcher for client events.
/\Aclient-(.+)\Z/.freeze
Instance Attribute Summary collapse
-
#socket ⇒ EventMachine::WebSocket::Connection
readonly
The socket object for this connection.
Instance Method Summary collapse
-
#emit(event, data = {}, channel = nil) ⇒ Object
Emit an event to the connection.
-
#establish ⇒ Object
Notify the Pusher client that a connection has been established.
-
#id ⇒ Integer
The ID of the connection.
-
#initialize(socket) ⇒ Connection
constructor
Create a new Connection object.
-
#process(data) ⇒ Object
Process an event.
Constructor Details
#initialize(socket) ⇒ Connection
Create a new PusherFake::Connection object.
12 13 14 |
# File 'lib/pusher-fake/connection.rb', line 12 def initialize(socket) @socket = socket end |
Instance Attribute Details
#socket ⇒ EventMachine::WebSocket::Connection (readonly)
Returns The socket object for this connection.
7 8 9 |
# File 'lib/pusher-fake/connection.rb', line 7 def socket @socket end |
Instance Method Details
#emit(event, data = {}, channel = nil) ⇒ Object
Emit an event to the connection.
31 32 33 34 35 36 37 38 |
# File 'lib/pusher-fake/connection.rb', line 31 def emit(event, data = {}, channel = nil) = { event: event, data: MultiJson.dump(data) } [:channel] = channel if channel PusherFake.log("SEND #{id}: #{}") socket.send(MultiJson.dump()) end |
#establish ⇒ Object
Notify the Pusher client that a connection has been established.
41 42 43 |
# File 'lib/pusher-fake/connection.rb', line 41 def establish emit("pusher:connection_established", socket_id: id, activity_timeout: 120) end |
#id ⇒ Integer
The ID of the connection.
19 20 21 22 23 24 |
# File 'lib/pusher-fake/connection.rb', line 19 def id parts = socket.object_id.to_s.split("") parts = parts.each_slice(parts.length / 2).to_a [parts.first.join(""), parts.last.join("")].join(".") end |
#process(data) ⇒ Object
Process an event.
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/pusher-fake/connection.rb', line 48 def process(data) = MultiJson.load(data, symbolize_keys: true) PusherFake.log("RECV #{id}: #{}") data = [:data] event = [:event] name = [:channel] || data[:channel] channel = Channel.factory(name) if name case event when "pusher:subscribe" channel.add(self, data) when "pusher:unsubscribe" channel.remove(self) when "pusher:ping" emit("pusher:pong") when CLIENT_EVENT_MATCHER if channel.is_a?(Channel::Private) && channel.includes?(self) channel.emit(event, data, socket_id: id) trigger(channel, id, event, data) end end end |