mattermost/api/web_socket.go

42 lines
1 KiB
Go
Raw Normal View History

// Copyright (c) 2015 Mattermost, Inc. All Rights Reserved.
2015-06-15 03:53:32 -04:00
// See License.txt for license information.
package api
import (
2016-01-11 10:12:51 -05:00
l4g "github.com/alecthomas/log4go"
2015-06-15 03:53:32 -04:00
"github.com/gorilla/mux"
"github.com/gorilla/websocket"
"github.com/mattermost/platform/model"
"github.com/mattermost/platform/utils"
2015-06-15 03:53:32 -04:00
"net/http"
)
func InitWebSocket(r *mux.Router) {
l4g.Debug(utils.T("api.web_socket.init.debug"))
2015-06-15 03:53:32 -04:00
r.Handle("/websocket", ApiUserRequired(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, "")
2015-06-15 03:53:32 -04:00
return
}
wc := NewWebConn(ws, c.Session.TeamId, c.Session.UserId, c.Session.Id)
hub.Register(wc)
go wc.writePump()
wc.readPump()
}