github.com/adacta-ru/mattermost-server/v6@v6.0.0/api4/websocket.go (about)

     1  // Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
     2  // See LICENSE.txt for license information.
     3  
     4  package api4
     5  
     6  import (
     7  	"net/http"
     8  
     9  	"github.com/gorilla/websocket"
    10  	"github.com/adacta-ru/mattermost-server/v6/mlog"
    11  	"github.com/adacta-ru/mattermost-server/v6/model"
    12  )
    13  
    14  func (api *API) InitWebSocket() {
    15  	// Optionally supports a trailing slash
    16  	api.BaseRoutes.ApiRoot.Handle("/{websocket: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("websocket connect err.", mlog.Err(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  }