summaryrefslogtreecommitdiffstats
path: root/vendor/github.com/mattermost/platform/model/websocket_client.go
diff options
context:
space:
mode:
authorWim <wim@42.be>2016-11-12 22:00:53 +0100
committerWim <wim@42.be>2016-11-12 22:00:53 +0100
commit1d5cd1d7c479c382c9cddaf02f1e59bf55971f12 (patch)
treeabaf22fc41326750e376f0831537bbbbe769d5b2 /vendor/github.com/mattermost/platform/model/websocket_client.go
parent08ebee6b4faf677da159db1cffea292050492fd5 (diff)
downloadmatterbridge-msglm-1d5cd1d7c479c382c9cddaf02f1e59bf55971f12.tar.gz
matterbridge-msglm-1d5cd1d7c479c382c9cddaf02f1e59bf55971f12.tar.bz2
matterbridge-msglm-1d5cd1d7c479c382c9cddaf02f1e59bf55971f12.zip
Sync with mattermost 3.5.0
Diffstat (limited to 'vendor/github.com/mattermost/platform/model/websocket_client.go')
-rw-r--r--vendor/github.com/mattermost/platform/model/websocket_client.go45
1 files changed, 35 insertions, 10 deletions
diff --git a/vendor/github.com/mattermost/platform/model/websocket_client.go b/vendor/github.com/mattermost/platform/model/websocket_client.go
index a048bd85..453ae49b 100644
--- a/vendor/github.com/mattermost/platform/model/websocket_client.go
+++ b/vendor/github.com/mattermost/platform/model/websocket_client.go
@@ -6,7 +6,6 @@ package model
import (
"encoding/json"
"github.com/gorilla/websocket"
- "net/http"
)
type WebSocketClient struct {
@@ -17,19 +16,18 @@ type WebSocketClient struct {
Sequence int64 // The ever-incrementing sequence attached to each WebSocket action
EventChannel chan *WebSocketEvent
ResponseChannel chan *WebSocketResponse
+ ListenError *AppError
}
// NewWebSocketClient constructs a new WebSocket client with convienence
// methods for talking to the server.
func NewWebSocketClient(url, authToken string) (*WebSocketClient, *AppError) {
- header := http.Header{}
- header.Set(HEADER_AUTH, "BEARER "+authToken)
- conn, _, err := websocket.DefaultDialer.Dial(url+API_URL_SUFFIX+"/users/websocket", header)
+ conn, _, err := websocket.DefaultDialer.Dial(url+API_URL_SUFFIX+"/users/websocket", nil)
if err != nil {
return nil, NewLocAppError("NewWebSocketClient", "model.websocket_client.connect_fail.app_error", nil, err.Error())
}
- return &WebSocketClient{
+ client := &WebSocketClient{
url,
url + API_URL_SUFFIX,
conn,
@@ -37,19 +35,26 @@ func NewWebSocketClient(url, authToken string) (*WebSocketClient, *AppError) {
1,
make(chan *WebSocketEvent, 100),
make(chan *WebSocketResponse, 100),
- }, nil
+ nil,
+ }
+
+ client.SendMessage(WEBSOCKET_AUTHENTICATION_CHALLENGE, map[string]interface{}{"token": authToken})
+
+ return client, nil
}
func (wsc *WebSocketClient) Connect() *AppError {
- header := http.Header{}
- header.Set(HEADER_AUTH, "BEARER "+wsc.AuthToken)
-
var err error
- wsc.Conn, _, err = websocket.DefaultDialer.Dial(wsc.ApiUrl+"/users/websocket", header)
+ wsc.Conn, _, err = websocket.DefaultDialer.Dial(wsc.ApiUrl+"/users/websocket", nil)
if err != nil {
return NewLocAppError("NewWebSocketClient", "model.websocket_client.connect_fail.app_error", nil, err.Error())
}
+ wsc.EventChannel = make(chan *WebSocketEvent, 100)
+ wsc.ResponseChannel = make(chan *WebSocketResponse, 100)
+
+ wsc.SendMessage(WEBSOCKET_AUTHENTICATION_CHALLENGE, map[string]interface{}{"token": wsc.AuthToken})
+
return nil
}
@@ -59,10 +64,20 @@ func (wsc *WebSocketClient) Close() {
func (wsc *WebSocketClient) Listen() {
go func() {
+ defer func() {
+ wsc.Conn.Close()
+ close(wsc.EventChannel)
+ close(wsc.ResponseChannel)
+ }()
+
for {
var rawMsg json.RawMessage
var err error
if _, rawMsg, err = wsc.Conn.ReadMessage(); err != nil {
+ if !websocket.IsCloseError(err, websocket.CloseNormalClosure, websocket.CloseNoStatusReceived) {
+ wsc.ListenError = NewLocAppError("NewWebSocketClient", "model.websocket_client.connect_fail.app_error", nil, err.Error())
+ }
+
return
}
@@ -77,6 +92,7 @@ func (wsc *WebSocketClient) Listen() {
wsc.ResponseChannel <- &response
continue
}
+
}
}()
}
@@ -107,3 +123,12 @@ func (wsc *WebSocketClient) UserTyping(channelId, parentId string) {
func (wsc *WebSocketClient) GetStatuses() {
wsc.SendMessage("get_statuses", nil)
}
+
+// GetStatusesByIds will fetch certain user statuses based on ids and return
+// a map of string statuses using user id as the key
+func (wsc *WebSocketClient) GetStatusesByIds(userIds []string) {
+ data := map[string]interface{}{
+ "user_ids": userIds,
+ }
+ wsc.SendMessage("get_statuses_by_ids", data)
+}