Class: WebSocketHelper
- Inherits:
-
Object
- Object
- WebSocketHelper
- Defined in:
- lib/sinatra/liveviews/json-websocket.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#latency ⇒ Object
Returns the value of attribute latency.
Instance Method Summary collapse
-
#initialize(ws) ⇒ WebSocketHelper
constructor
A new instance of WebSocketHelper.
- #on_close ⇒ Object
- #on_message(event, data) ⇒ Object
- #on_open ⇒ Object
-
#send(event, data = {}) ⇒ Object
send a message in to the current client.
-
#send_all(event, data) ⇒ Object
sends a message to all connected clients, including the current client.
-
#send_others(event, data) ⇒ Object
sends a message to all connected clients, except the current one.
- #simulate_latency ⇒ Object
Constructor Details
#initialize(ws) ⇒ WebSocketHelper
Returns a new instance of WebSocketHelper.
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/sinatra/liveviews/json-websocket.rb', line 17 def initialize(ws) self.latency = nil # the sockets list is the list of all other WebSocketHelper classes # for all other connections. This makes it easy to send a message to # all connected clients @sockets = SharedList.list @ws = ws @ws.onopen { self.on_open } @ws.onclose { self.on_close } @ws. { |msg| d = JSON.parse(msg, {:symbolize_names => true}) if d != nil && d[:event] != nil Thread.new { self.(d[:event], d[:data]) } end } end |
Instance Attribute Details
#latency ⇒ Object
Returns the value of attribute latency.
15 16 17 |
# File 'lib/sinatra/liveviews/json-websocket.rb', line 15 def latency @latency end |
Instance Method Details
#on_close ⇒ Object
56 57 58 |
# File 'lib/sinatra/liveviews/json-websocket.rb', line 56 def on_close @sockets.delete(self) end |
#on_message(event, data) ⇒ Object
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/sinatra/liveviews/json-websocket.rb', line 39 def (event, data) event_method = 'on_' + event.underscore begin simulate_latency if !self.latency.nil? method(event_method).call(data) if self.respond_to? event_method rescue => e # if the subclass has defined an error handler, then use that if something has # happened, otherwise we just rethrow it if self.respond_to? 'handle_error' handle_error(e, event_method, data) else raise end end end |
#on_open ⇒ Object
35 36 37 |
# File 'lib/sinatra/liveviews/json-websocket.rb', line 35 def on_open @sockets.push self end |
#send(event, data = {}) ⇒ Object
send a message in to the current client
61 62 63 |
# File 'lib/sinatra/liveviews/json-websocket.rb', line 61 def send(event, data = {}) @ws.send({:event => event, :data => data}.to_json) end |
#send_all(event, data) ⇒ Object
sends a message to all connected clients, including the current client
66 67 68 69 70 71 72 |
# File 'lib/sinatra/liveviews/json-websocket.rb', line 66 def send_all(event, data) EM.next_tick { @sockets.each do |s| s.send(event, data) end } end |
#send_others(event, data) ⇒ Object
sends a message to all connected clients, except the current one
75 76 77 78 79 80 81 |
# File 'lib/sinatra/liveviews/json-websocket.rb', line 75 def send_others(event, data) EM.next_tick { @sockets.each do |s| s.send(event, data) if s != self end } end |
#simulate_latency ⇒ Object
83 84 85 86 87 88 |
# File 'lib/sinatra/liveviews/json-websocket.rb', line 83 def simulate_latency return if self.latency.nil? factor = ((rand() * 0.2 - 0.1) + 1) # 0.9 to 1.1 delay = self.latency * factor sleep delay end |