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 (
|
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-09-06 23:05:10 -07:00
|
|
|
"github.com/mattermost/mattermost-server/model"
|
|
|
|
|
"github.com/mattermost/mattermost-server/utils"
|
2016-10-19 14:49:25 -04:00
|
|
|
)
|
|
|
|
|
|
2017-09-22 12:54:27 -05:00
|
|
|
func (api *API) InitWebSocket() {
|
|
|
|
|
api.BaseRoutes.Users.Handle("/websocket", api.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) {
|
|
|
|
|
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-11-22 15:58:03 -06:00
|
|
|
CheckOrigin: c.App.OriginChecker(),
|
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)
|
2017-09-01 14:58:43 +01:00
|
|
|
c.Err = model.NewAppError("connect", "api.web_socket.connect.upgrade.app_error", nil, "", http.StatusInternalServerError)
|
2015-06-14 23:53:32 -08:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2017-09-06 17:12:54 -05:00
|
|
|
wc := c.App.NewWebConn(ws, c.Session, c.T, c.Locale)
|
2017-02-14 17:25:40 -05:00
|
|
|
|
|
|
|
|
if len(c.Session.UserId) > 0 {
|
2017-09-27 11:52:34 -05:00
|
|
|
c.App.HubRegister(wc)
|
2017-02-14 17:25:40 -05:00
|
|
|
}
|
|
|
|
|
|
2017-10-03 10:53:53 -05:00
|
|
|
wc.Pump()
|
2015-06-14 23:53:32 -08:00
|
|
|
}
|