Class: Mattermost::WebSocketClient
- Inherits:
-
Object
- Object
- Mattermost::WebSocketClient
- Includes:
- EventEmitter
- Defined in:
- lib/mattermost/websocket_client.rb
Instance Method Summary collapse
- #close ⇒ Object
- #connected? ⇒ Boolean
-
#initialize(url, token, option = {}) {|_self| ... } ⇒ WebSocketClient
constructor
A new instance of WebSocketClient.
- #on_close(msg) ⇒ Object
- #on_error(err) ⇒ Object
- #on_message(data) ⇒ Object
- #on_open ⇒ Object
- #send_msg(action, data) ⇒ Object
Constructor Details
#initialize(url, token, option = {}) {|_self| ... } ⇒ WebSocketClient
Returns a new instance of WebSocketClient.
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/mattermost/websocket_client.rb', line 9 def initialize(url, token, option = {}) @token = token @url = url @seq_mutex = Mutex.new @seq = 0 @connected = false mm_ws = self @client = WebSocket::Client::Simple.connect(url, option) do |ws| ws.on :open do mm_ws.on_open end ws.on :message do |msg| mm_ws. msg.data end ws.on :close do |e| mm_ws.on_close e end ws.on :error do |e| mm_ws.on_error e end end yield self if block_given? end |
Instance Method Details
#close ⇒ Object
73 74 75 |
# File 'lib/mattermost/websocket_client.rb', line 73 def close @client.close end |
#connected? ⇒ Boolean
69 70 71 |
# File 'lib/mattermost/websocket_client.rb', line 69 def connected? @connected && @client.connected? end |
#on_close(msg) ⇒ Object
51 52 53 54 |
# File 'lib/mattermost/websocket_client.rb', line 51 def on_close(msg) @connected = false emit :close, msg end |
#on_error(err) ⇒ Object
56 57 58 |
# File 'lib/mattermost/websocket_client.rb', line 56 def on_error(err) emit :error, err end |
#on_message(data) ⇒ Object
37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/mattermost/websocket_client.rb', line 37 def (data) json = JSON.parse data event = json["event"] #puts "on_message json:#{json}, event:#{event}" seq_up json case event when "hello" @connected = true else emit event.to_sym, json emit :message, json end end |
#on_open ⇒ Object
33 34 35 |
# File 'lib/mattermost/websocket_client.rb', line 33 def on_open emit :open end |
#send_msg(action, data) ⇒ Object
60 61 62 63 64 65 66 67 |
# File 'lib/mattermost/websocket_client.rb', line 60 def send_msg(action, data) payload = { :seq => next_seq, :action => action, :data => data }.to_json @client.send payload end |