From 1d5cd1d7c479c382c9cddaf02f1e59bf55971f12 Mon Sep 17 00:00:00 2001 From: Wim Date: Sat, 12 Nov 2016 22:00:53 +0100 Subject: Sync with mattermost 3.5.0 --- .../mattermost/platform/model/websocket_client.go | 45 +++++++++++++++++----- 1 file changed, 35 insertions(+), 10 deletions(-) (limited to 'vendor/github.com/mattermost/platform/model/websocket_client.go') 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) +} -- cgit v1.2.3