github.com/mad-app/mattermost-server@v5.11.1+incompatible/api4/websocket.go (about)

     1  // Copyright (c) 2017-present Mattermost, Inc. All Rights Reserved.
     2  // See License.txt for license information.
     3  
     4  package api4
     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.ApiRoot.Handle("/websocket", api.ApiHandlerTrustRequester(connectWebSocket)).Methods("GET")
    17  }
    18  
    19  func connectWebSocket(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.App.Session, c.App.T, "")
    34  
    35  	if len(c.App.Session.UserId) > 0 {
    36  		c.App.HubRegister(wc)
    37  	}
    38  
    39  	wc.Pump()
    40  }