Class: Adminix::WebsocketClient
- Inherits:
-
Object
- Object
- Adminix::WebsocketClient
- Defined in:
- lib/adminix/websocket_client.rb
Instance Attribute Summary collapse
-
#channel_handlers ⇒ Object
Returns the value of attribute channel_handlers.
-
#client ⇒ Object
Returns the value of attribute client.
-
#connected ⇒ Object
Returns the value of attribute connected.
-
#connection_handler ⇒ Object
Returns the value of attribute connection_handler.
-
#disconnect_handler ⇒ Object
Returns the value of attribute disconnect_handler.
-
#url ⇒ Object
Returns the value of attribute url.
Instance Method Summary collapse
- #connect(&block) ⇒ Object
- #create_publish_message(identifier, data) ⇒ Object
- #create_subscribe_message(identifier) ⇒ Object
-
#initialize(url, &disconnect_handler) ⇒ WebsocketClient
constructor
A new instance of WebsocketClient.
- #parse_message(message) ⇒ Object
- #publish(identifier, data) ⇒ Object
- #push(msg) ⇒ Object
- #subscribe(identifier, &block) ⇒ Object
Constructor Details
#initialize(url, &disconnect_handler) ⇒ WebsocketClient
Returns a new instance of WebsocketClient.
12 13 14 15 |
# File 'lib/adminix/websocket_client.rb', line 12 def initialize(url, &disconnect_handler) self.url = url self.disconnect_handler = disconnect_handler end |
Instance Attribute Details
#channel_handlers ⇒ Object
Returns the value of attribute channel_handlers.
8 9 10 |
# File 'lib/adminix/websocket_client.rb', line 8 def channel_handlers @channel_handlers end |
#client ⇒ Object
Returns the value of attribute client.
5 6 7 |
# File 'lib/adminix/websocket_client.rb', line 5 def client @client end |
#connected ⇒ Object
Returns the value of attribute connected.
6 7 8 |
# File 'lib/adminix/websocket_client.rb', line 6 def connected @connected end |
#connection_handler ⇒ Object
Returns the value of attribute connection_handler.
7 8 9 |
# File 'lib/adminix/websocket_client.rb', line 7 def connection_handler @connection_handler end |
#disconnect_handler ⇒ Object
Returns the value of attribute disconnect_handler.
9 10 11 |
# File 'lib/adminix/websocket_client.rb', line 9 def disconnect_handler @disconnect_handler end |
#url ⇒ Object
Returns the value of attribute url.
10 11 12 |
# File 'lib/adminix/websocket_client.rb', line 10 def url @url end |
Instance Method Details
#connect(&block) ⇒ Object
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/adminix/websocket_client.rb', line 17 def connect(&block) self.connection_handler = block self.channel_handlers = {} self.client = EventMachine::WebSocketClient.connect(url) client.callback do system.log "Connected WebSocket to #{url}." self.connected = true end client.stream do |raw| = (raw.to_s) if ['identifier'] channel_handlers[['identifier'].to_json].call() else connection_handler.call() end end client.disconnect do self.connected = false disconnect_handler.call end end |
#create_publish_message(identifier, data) ⇒ Object
54 55 56 57 58 59 60 61 62 |
# File 'lib/adminix/websocket_client.rb', line 54 def (identifier, data) = { command: 'message', identifier: identifier } [:data] = data.to_json if data [:identifier] = [:identifier].to_json .to_json end |
#create_subscribe_message(identifier) ⇒ Object
64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/adminix/websocket_client.rb', line 64 def (identifier) = { command: 'subscribe', identifier: identifier.to_json } # message[:identifier].merge!(data) if data # message[:data] = data.to_json if data # message[:identifier] = message[:identifier].to_json # puts message .to_json end |
#parse_message(message) ⇒ Object
76 77 78 79 80 81 |
# File 'lib/adminix/websocket_client.rb', line 76 def () result = JSON.parse() result['identifier'] = JSON.parse(result['identifier']) if result['identifier'] result['data'] = JSON.parse(result['data']) if result['data'] result end |
#publish(identifier, data) ⇒ Object
48 49 50 51 52 |
# File 'lib/adminix/websocket_client.rb', line 48 def publish(identifier, data) msg = (identifier, data) # puts "PUSHING: #{msg}" push(msg) end |
#push(msg) ⇒ Object
44 45 46 |
# File 'lib/adminix/websocket_client.rb', line 44 def push(msg) client.send_msg(msg) end |
#subscribe(identifier, &block) ⇒ Object
39 40 41 42 |
# File 'lib/adminix/websocket_client.rb', line 39 def subscribe(identifier, &block) push((identifier)) channel_handlers[identifier.to_json] = block end |