Class: Sarb::Connection

Inherits:
Object
  • Object
show all
Defined in:
lib/sarb/connection.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app, ws) ⇒ Connection

Returns a new instance of Connection.



3
4
5
6
# File 'lib/sarb/connection.rb', line 3

def initialize(app, ws)
  @app = app
  @ws = ws
end

Class Method Details

.setup(app, ws) ⇒ Object



24
25
26
27
28
29
30
# File 'lib/sarb/connection.rb', line 24

def self.setup(app, ws)
  connection = Connection.new(app, ws)
  ws.onopen &connection.method(:onopen)
  ws.onmessage &connection.method(:onmessage)
  ws.onclose &connection.method(:onclose)
  connection
end

Instance Method Details

#message(message) ⇒ Object



20
21
22
# File 'lib/sarb/connection.rb', line 20

def message(message)
  @ws.send(message.to_json)
end

#onclose(event) ⇒ Object



16
17
18
# File 'lib/sarb/connection.rb', line 16

def onclose(event)
  @app.trigger :connection_close, :connection => self
end

#onmessage(message) ⇒ Object



12
13
14
# File 'lib/sarb/connection.rb', line 12

def onmessage(message)
  @app.invoke self, message
end

#onopen(handshake) ⇒ Object



8
9
10
# File 'lib/sarb/connection.rb', line 8

def onopen(handshake)
  @app.trigger :connection_open, :connection => self
end