summaryrefslogtreecommitdiffstats
path: root/api4/websocket.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 /api4/websocket.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 'api4/websocket.go')
-rw-r--r--api4/websocket.go46
1 files changed, 46 insertions, 0 deletions
diff --git a/api4/websocket.go b/api4/websocket.go
new file mode 100644
index 000000000..c70327222
--- /dev/null
+++ b/api4/websocket.go
@@ -0,0 +1,46 @@
+// Copyright (c) 2017 Mattermost, Inc. All Rights Reserved.
+// See License.txt for license information.
+
+package api4
+
+import (
+ "net/http"
+
+ l4g "github.com/alecthomas/log4go"
+ "github.com/gorilla/websocket"
+ "github.com/mattermost/platform/app"
+ "github.com/mattermost/platform/model"
+ "github.com/mattermost/platform/utils"
+)
+
+func InitWebSocket() {
+ l4g.Debug(utils.T("api.web_socket.init.debug"))
+
+ BaseRoutes.ApiRoot.Handle("/websocket", ApiHandlerTrustRequester(connectWebSocket)).Methods("GET")
+}
+
+func connectWebSocket(c *Context, w http.ResponseWriter, r *http.Request) {
+ originChecker := utils.GetOriginChecker(r)
+
+ upgrader := websocket.Upgrader{
+ ReadBufferSize: model.SOCKET_MAX_MESSAGE_SIZE_KB,
+ WriteBufferSize: model.SOCKET_MAX_MESSAGE_SIZE_KB,
+ CheckOrigin: originChecker,
+ }
+
+ ws, err := upgrader.Upgrade(w, r, nil)
+ if err != nil {
+ l4g.Error(utils.T("api.web_socket.connect.error"), err)
+ c.Err = model.NewLocAppError("connect", "api.web_socket.connect.upgrade.app_error", nil, "")
+ return
+ }
+
+ wc := app.NewWebConn(ws, c.Session, c.T, "")
+
+ if len(c.Session.UserId) > 0 {
+ app.HubRegister(wc)
+ }
+
+ go wc.WritePump()
+ wc.ReadPump()
+}