summaryrefslogtreecommitdiffstats
path: root/api/web_socket.go
blob: 995e2a6776e9f13514ec15d1c801b6c324e9434a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
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/mux"
	"github.com/gorilla/websocket"
	"github.com/mattermost/platform/model"
	"net/http"
)

func InitWebSocket(r *mux.Router) {
	l4g.Debug("Initializing web socket api routes")
	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("websocket connect err: %v", err)
		c.Err = model.NewAppError("connect", "Failed to upgrade websocket connection", "")
		return
	}

	wc := NewWebConn(ws, c.Session.TeamId, c.Session.UserId, c.Session.Id)
	hub.Register(wc)
	go wc.writePump()
	wc.readPump()
}