From ad343a0f4ad175053f7d0da12a0587bcbb396d1c Mon Sep 17 00:00:00 2001 From: Joram Wilander Date: Tue, 12 Jul 2016 09:36:27 -0400 Subject: Added infrastructure for basic WebSocket API (#3432) --- api/websocket.go | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 api/websocket.go (limited to 'api/websocket.go') diff --git a/api/websocket.go b/api/websocket.go new file mode 100644 index 000000000..fe9fa0bf9 --- /dev/null +++ b/api/websocket.go @@ -0,0 +1,40 @@ +// Copyright (c) 2015 Mattermost, Inc. All Rights Reserved. +// See License.txt for license information. + +package api + +import ( + l4g "github.com/alecthomas/log4go" + "github.com/gorilla/websocket" + "github.com/mattermost/platform/model" + "github.com/mattermost/platform/utils" + "net/http" +) + +func InitWebSocket() { + l4g.Debug(utils.T("api.web_socket.init.debug")) + BaseRoutes.Users.Handle("/websocket", ApiUserRequiredTrustRequester(connect)).Methods("GET") + hub.Start() +} + +func connect(c *Context, w http.ResponseWriter, r *http.Request) { + upgrader := websocket.Upgrader{ + ReadBufferSize: 1024, + WriteBufferSize: 1024, + CheckOrigin: func(r *http.Request) bool { + return true + }, + } + + 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 := NewWebConn(c, ws) + hub.Register(wc) + go wc.writePump() + wc.readPump() +} -- cgit v1.2.3-1-g7c22