Class: PusherFake::Connection
- Inherits:
-
Object
- Object
- PusherFake::Connection
- Defined in:
- lib/pusher-fake/connection.rb
Overview
A client connection.
Constant Summary collapse
- CLIENT_EVENT_PREFIX =
Prefix for client events.
"client-"
Instance Attribute Summary collapse
-
#socket ⇒ EventMachine::WebSocket::Connection
readonly
Socket for the 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.
15 16 17 |
# File 'lib/pusher-fake/connection.rb', line 15 def initialize(socket) @socket = socket end |
Instance Attribute Details
#socket ⇒ EventMachine::WebSocket::Connection (readonly)
Returns Socket for the connection.
10 11 12 |
# File 'lib/pusher-fake/connection.rb', line 10 def socket @socket end |
Instance Method Details
#emit(event, data = {}, channel = nil) ⇒ Object
Emit an event to the connection.
34 35 36 37 38 39 40 41 |
# File 'lib/pusher-fake/connection.rb', line 34 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.
44 45 46 47 |
# File 'lib/pusher-fake/connection.rb', line 44 def establish emit("pusher:connection_established", socket_id: id, activity_timeout: 120) end |
#id ⇒ Integer
The ID of the connection.
22 23 24 25 26 27 |
# File 'lib/pusher-fake/connection.rb', line 22 def id parts = socket.object_id.to_s.chars parts = parts.each_slice((parts.length / 2.0).ceil).to_a [parts.first.join, parts.last.join].join(".") end |
#process(data) ⇒ Object
Process an event.
52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/pusher-fake/connection.rb', line 52 def process(data) = MultiJson.load(data, symbolize_keys: true) event = [:event] PusherFake.log("RECV #{id}: #{}") if event.start_with?(CLIENT_EVENT_PREFIX) process_trigger(event, ) else process_event(event, ) end end |