diff options
author | Joram Wilander <jwawilander@gmail.com> | 2017-03-28 04:58:19 -0400 |
---|---|---|
committer | George Goldberg <george@gberg.me> | 2017-03-28 09:58:19 +0100 |
commit | daca0d93f621bcb1daae149c178af0631bcd120a (patch) | |
tree | 6127936c76dbc6fd20a7377385469980e5bd72e3 /model/websocket_client.go | |
parent | ca8b8d1245026672b1a56d256bb8ff3c8bb1bba9 (diff) | |
download | chat-daca0d93f621bcb1daae149c178af0631bcd120a.tar.gz chat-daca0d93f621bcb1daae149c178af0631bcd120a.tar.bz2 chat-daca0d93f621bcb1daae149c178af0631bcd120a.zip |
Move WebSocket API to it's own package and add websocket v4 endpoint (#5881)
Diffstat (limited to 'model/websocket_client.go')
-rw-r--r-- | model/websocket_client.go | 31 |
1 files changed, 29 insertions, 2 deletions
diff --git a/model/websocket_client.go b/model/websocket_client.go index 083fe110a..2da83be56 100644 --- a/model/websocket_client.go +++ b/model/websocket_client.go @@ -15,6 +15,7 @@ const ( type WebSocketClient struct { Url string // The location of the server like "ws://localhost:8065" ApiUrl string // The api location of the server like "ws://localhost:8065/api/v3" + ConnectUrl string // The websocket URL to connect to like "ws://localhost:8065/api/v3/path/to/websocket" Conn *websocket.Conn // The WebSocket connection AuthToken string // The token used to open the WebSocket Sequence int64 // The ever-incrementing sequence attached to each WebSocket action @@ -34,6 +35,32 @@ func NewWebSocketClient(url, authToken string) (*WebSocketClient, *AppError) { client := &WebSocketClient{ url, url + API_URL_SUFFIX_V3, + url + API_URL_SUFFIX_V3 + "/users/websocket", + conn, + authToken, + 1, + make(chan *WebSocketEvent, 100), + make(chan *WebSocketResponse, 100), + nil, + } + + client.SendMessage(WEBSOCKET_AUTHENTICATION_CHALLENGE, map[string]interface{}{"token": authToken}) + + return client, nil +} + +// NewWebSocketClient4 constructs a new WebSocket client with convienence +// methods for talking to the server. Uses the v4 endpoint. +func NewWebSocketClient4(url, authToken string) (*WebSocketClient, *AppError) { + conn, _, err := websocket.DefaultDialer.Dial(url+API_URL_SUFFIX+"/websocket", nil) + if err != nil { + return nil, NewLocAppError("NewWebSocketClient4", "model.websocket_client.connect_fail.app_error", nil, err.Error()) + } + + client := &WebSocketClient{ + url, + url + API_URL_SUFFIX, + url + API_URL_SUFFIX + "/websocket", conn, authToken, 1, @@ -49,9 +76,9 @@ func NewWebSocketClient(url, authToken string) (*WebSocketClient, *AppError) { func (wsc *WebSocketClient) Connect() *AppError { var err error - wsc.Conn, _, err = websocket.DefaultDialer.Dial(wsc.ApiUrl+"/users/websocket", nil) + wsc.Conn, _, err = websocket.DefaultDialer.Dial(wsc.ConnectUrl, nil) if err != nil { - return NewLocAppError("NewWebSocketClient", "model.websocket_client.connect_fail.app_error", nil, err.Error()) + return NewLocAppError("Connect", "model.websocket_client.connect_fail.app_error", nil, err.Error()) } wsc.EventChannel = make(chan *WebSocketEvent, 100) |