class Mattermost::WebSocketClient

Public Class Methods

new(url, token, option = {}) { |self| ... } click to toggle source
# File lib/mattermost/websocket_client.rb, line 9
def initialize(url, token, option = {})
        @token = token
        @url = url
        @option = option
        @seq_mutex = Mutex.new
        @connected = false
        yield self if block_given?
        connect
        self
end

Public Instance Methods

close() click to toggle source
# File lib/mattermost/websocket_client.rb, line 70
def close
        @connected = false
        @client.close if @client != nil
        @client = nil
        Thread.kill @em if @em != nil
        @em = nil
end
connect() click to toggle source
# File lib/mattermost/websocket_client.rb, line 20
def connect
        return self if connected?
        mm_ws = self
        @em = Thread.new do
                EM.run do
                        mm_ws.connect_internal
                end
        end
end
connect_internal() click to toggle source
# File lib/mattermost/websocket_client.rb, line 78
def connect_internal
        @seq = 0
        mm_ws = self
        @client = Faye::WebSocket::Client.new(@url.gsub(/^http(s?):/, 'ws\1:'), {}, { :headers => {
                'Authorization' => "Bearer #{@token}"
        }})
        @client.on :open do |e|
                mm_ws.on_open e
        end
        @client.on :message do |msg|
                mm_ws.on_message msg.data
        end
        @client.on :close do |e|
                mm_ws.on_close e
        end
        @client.on :error do |e|
                mm_ws.on_error e
        end
end
connected?() click to toggle source
# File lib/mattermost/websocket_client.rb, line 66
def connected?
        @connected && (@client != nil && @client.ready_state == Faye::WebSocket::API::OPEN)
end
on_close(msg) click to toggle source
# File lib/mattermost/websocket_client.rb, line 48
def on_close(msg)
        @connected = false
        emit :close, msg
end
on_error(err) click to toggle source
# File lib/mattermost/websocket_client.rb, line 53
def on_error(err)
        emit :error, err
end
on_message(data) click to toggle source
# File lib/mattermost/websocket_client.rb, line 34
def on_message(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
        end
        emit :message, json
end
on_open(e) click to toggle source
# File lib/mattermost/websocket_client.rb, line 30
def on_open(e)
        emit :open, e
end
send_msg(action, data) click to toggle source
# File lib/mattermost/websocket_client.rb, line 57
def send_msg(action, data)
        payload = {
                :seq => next_seq,
                :action => action,
                :data => data
        }.to_json
        @client.send payload
end

Private Instance Methods

next_seq() click to toggle source
# File lib/mattermost/websocket_client.rb, line 108
def next_seq
        @seq_mutex.synchronize do
                @seq = @seq + 1
                @seq
        end
end
seq_up(event) click to toggle source
# File lib/mattermost/websocket_client.rb, line 100
def seq_up(event)
        @seq_mutex.synchronize do
                return if !event.key? "seq"
                new_seq = event["seq"]
                @seq = new_seq if @seq < new_seq
        end
end