github.com/keys-pub/mattermost-server@v4.10.10+incompatible/api/websocket.go (about)

     1  // Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
     2  // See License.txt for license information.
     3  
     4  package api
     5  
     6  import (
     7  	"fmt"
     8  	"net/http"
     9  
    10  	"github.com/gorilla/websocket"
    11  	"github.com/mattermost/mattermost-server/mlog"
    12  	"github.com/mattermost/mattermost-server/model"
    13  )
    14  
    15  func (api *API) InitWebSocket() {
    16  	api.BaseRoutes.Users.Handle("/websocket", api.ApiAppHandlerTrustRequester(connect)).Methods("GET")
    17  }
    18  
    19  func connect(c *Context, w http.ResponseWriter, r *http.Request) {
    20  	upgrader := websocket.Upgrader{
    21  		ReadBufferSize:  model.SOCKET_MAX_MESSAGE_SIZE_KB,
    22  		WriteBufferSize: model.SOCKET_MAX_MESSAGE_SIZE_KB,
    23  		CheckOrigin:     c.App.OriginChecker(),
    24  	}
    25  
    26  	ws, err := upgrader.Upgrade(w, r, nil)
    27  	if err != nil {
    28  		mlog.Error(fmt.Sprintf("websocket connect err: %v", err))
    29  		c.Err = model.NewAppError("connect", "api.web_socket.connect.upgrade.app_error", nil, "", http.StatusInternalServerError)
    30  		return
    31  	}
    32  
    33  	wc := c.App.NewWebConn(ws, c.Session, c.T, c.Locale)
    34  
    35  	if len(c.Session.UserId) > 0 {
    36  		c.App.HubRegister(wc)
    37  	}
    38  
    39  	wc.Pump()
    40  }