Files
mattermost/api/websocket.go
T

46 lines
1.1 KiB
Go
Raw Normal View History

2017-04-12 08:27:57 -04:00
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
2015-06-14 23:53:32 -08:00
// See License.txt for license information.
package api
import (
"net/http"
2016-01-11 09:12:51 -06:00
l4g "github.com/alecthomas/log4go"
2015-06-14 23:53:32 -08:00
"github.com/gorilla/websocket"
"github.com/mattermost/platform/app"
2015-06-14 23:53:32 -08:00
"github.com/mattermost/platform/model"
"github.com/mattermost/platform/utils"
)
func InitWebSocket() {
l4g.Debug(utils.T("api.web_socket.init.debug"))
BaseRoutes.Users.Handle("/websocket", ApiAppHandlerTrustRequester(connect)).Methods("GET")
2017-03-23 14:10:52 +01:00
}
2015-06-14 23:53:32 -08:00
func connect(c *Context, w http.ResponseWriter, r *http.Request) {
originChecker := utils.GetOriginChecker(r)
2017-03-23 14:10:52 +01:00
2015-06-14 23:53:32 -08:00
upgrader := websocket.Upgrader{
ReadBufferSize: model.SOCKET_MAX_MESSAGE_SIZE_KB,
WriteBufferSize: model.SOCKET_MAX_MESSAGE_SIZE_KB,
2017-03-23 14:10:52 +01:00
CheckOrigin: originChecker,
2015-06-14 23:53:32 -08:00
}
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, "")
2015-06-14 23:53:32 -08:00
return
}
wc := app.NewWebConn(ws, c.Session, c.T, c.Locale)
if len(c.Session.UserId) > 0 {
app.HubRegister(wc)
}
go wc.WritePump()
wc.ReadPump()
2015-06-14 23:53:32 -08:00
}