summaryrefslogtreecommitdiffstats
path: root/model/websocket_client.go
diff options
context:
space:
mode:
authorJoram Wilander <jwawilander@gmail.com>2017-03-28 04:58:19 -0400
committerGeorge Goldberg <george@gberg.me>2017-03-28 09:58:19 +0100
commitdaca0d93f621bcb1daae149c178af0631bcd120a (patch)
tree6127936c76dbc6fd20a7377385469980e5bd72e3 /model/websocket_client.go
parentca8b8d1245026672b1a56d256bb8ff3c8bb1bba9 (diff)
downloadchat-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.go31
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)