2015-10-08 12:27:09 -04:00
|
|
|
// Copyright (c) 2015 Mattermost, Inc. All Rights Reserved.
|
2015-06-14 23:53:32 -08:00
|
|
|
// See License.txt for license information.
|
|
|
|
|
|
|
|
|
|
package api
|
|
|
|
|
|
|
|
|
|
import (
|
2017-01-13 13:53:37 -05:00
|
|
|
"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"
|
2017-01-13 13:53:37 -05:00
|
|
|
"github.com/mattermost/platform/app"
|
2015-06-14 23:53:32 -08:00
|
|
|
"github.com/mattermost/platform/model"
|
2016-01-23 10:25:10 -03:00
|
|
|
"github.com/mattermost/platform/utils"
|
2016-10-19 14:49:25 -04:00
|
|
|
)
|
|
|
|
|
|
2016-04-21 22:37:01 -07:00
|
|
|
func InitWebSocket() {
|
2016-01-23 10:25:10 -03:00
|
|
|
l4g.Debug(utils.T("api.web_socket.init.debug"))
|
2016-10-31 08:59:23 -04:00
|
|
|
BaseRoutes.Users.Handle("/websocket", ApiAppHandlerTrustRequester(connect)).Methods("GET")
|
2017-01-13 13:53:37 -05:00
|
|
|
app.HubStart()
|
2015-06-14 23:53:32 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func connect(c *Context, w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
upgrader := websocket.Upgrader{
|
2017-01-13 13:53:37 -05:00
|
|
|
ReadBufferSize: model.SOCKET_MAX_MESSAGE_SIZE_KB,
|
|
|
|
|
WriteBufferSize: model.SOCKET_MAX_MESSAGE_SIZE_KB,
|
2017-01-31 09:48:06 -05:00
|
|
|
CheckOrigin: nil,
|
2015-06-14 23:53:32 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ws, err := upgrader.Upgrade(w, r, nil)
|
|
|
|
|
if err != nil {
|
2016-01-23 10:25:10 -03:00
|
|
|
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
|
|
|
|
|
}
|
|
|
|
|
|
2017-01-13 13:53:37 -05:00
|
|
|
wc := app.NewWebConn(ws, c.Session, c.T, c.Locale)
|
|
|
|
|
app.HubRegister(wc)
|
|
|
|
|
go wc.WritePump()
|
|
|
|
|
wc.ReadPump()
|
2015-06-14 23:53:32 -08:00
|
|
|
}
|