Class: KabustationClient::PushClient
- Inherits:
-
Object
- Object
- KabustationClient::PushClient
- Defined in:
- lib/kabustation_client/push_client.rb
Instance Attribute Summary collapse
-
#ws ⇒ Object
readonly
Returns the value of attribute ws.
Instance Method Summary collapse
- #close ⇒ Object
- #connect ⇒ Object
-
#initialize(callbacks = {}) ⇒ PushClient
constructor
A new instance of PushClient.
Constructor Details
#initialize(callbacks = {}) ⇒ PushClient
Returns a new instance of PushClient.
9 10 11 12 13 14 15 16 17 18 19 20 |
# File 'lib/kabustation_client/push_client.rb', line 9 def initialize(callbacks = {}) @callbacks = { open: -> {}, message: -> (_board) {}, error: -> (_e) {}, close: -> (_e) {} }.merge(callbacks) config = Configuration.default @uri = "ws://#{config.host}#{config.base_path}/websocket" end |
Instance Attribute Details
#ws ⇒ Object (readonly)
Returns the value of attribute ws.
7 8 9 |
# File 'lib/kabustation_client/push_client.rb', line 7 def ws @ws end |
Instance Method Details
#close ⇒ Object
49 50 51 |
# File 'lib/kabustation_client/push_client.rb', line 49 def close @ws.close end |
#connect ⇒ Object
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/kabustation_client/push_client.rb', line 22 def connect callbacks = @callbacks @ws = WebSocket::Client::Simple.connect @uri do |ws| ws.on :open do callbacks[:open].call end ws.on :message do |msg| data = msg.data.force_encoding("ASCII-8BIT").force_encoding('UTF-8') json = JSON.parse(data) board = KabustationClient::ApiClient.new.convert_to_type(json, 'BoardSuccess') callbacks[:message].call(board) end ws.on :error do |e| callbacks[:error].call(e) end ws.on :close do |e| callbacks[:close].call(e) end end end |