diff options
author | =Corey Hulen <corey@hulen.com> | 2015-06-14 23:53:32 -0800 |
---|---|---|
committer | =Corey Hulen <corey@hulen.com> | 2015-06-14 23:53:32 -0800 |
commit | 56e74239d6b34df8f30ef046f0b0ff4ff0866a71 (patch) | |
tree | 044da29848cf0f5c8607eac34de69065171669cf /api/web_socket.go | |
download | chat-56e74239d6b34df8f30ef046f0b0ff4ff0866a71.tar.gz chat-56e74239d6b34df8f30ef046f0b0ff4ff0866a71.tar.bz2 chat-56e74239d6b34df8f30ef046f0b0ff4ff0866a71.zip |
first commit
Diffstat (limited to 'api/web_socket.go')
-rw-r--r-- | api/web_socket.go | 40 |
1 files changed, 40 insertions, 0 deletions
diff --git a/api/web_socket.go b/api/web_socket.go new file mode 100644 index 000000000..75936a8d5 --- /dev/null +++ b/api/web_socket.go @@ -0,0 +1,40 @@ +// Copyright (c) 2015 Spinpunch, Inc. All Rights Reserved. +// See License.txt for license information. + +package api + +import ( + l4g "code.google.com/p/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() +} |