Move WebSocket API to it's own package and add websocket v4 endpoint (#5881)
Этот коммит содержится в:
коммит произвёл
George Goldberg
родитель
ca8b8d1245
Коммит
daca0d93f6
@@ -61,7 +61,6 @@ var BaseRoutes *Routes
|
|||||||
func InitRouter() {
|
func InitRouter() {
|
||||||
app.Srv.Router = mux.NewRouter()
|
app.Srv.Router = mux.NewRouter()
|
||||||
app.Srv.Router.NotFoundHandler = http.HandlerFunc(Handle404)
|
app.Srv.Router.NotFoundHandler = http.HandlerFunc(Handle404)
|
||||||
app.Srv.WebSocketRouter = app.NewWebSocketRouter()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func InitApi() {
|
func InitApi() {
|
||||||
|
|||||||
@@ -11,6 +11,7 @@ import (
|
|||||||
"github.com/mattermost/platform/model"
|
"github.com/mattermost/platform/model"
|
||||||
"github.com/mattermost/platform/store"
|
"github.com/mattermost/platform/store"
|
||||||
"github.com/mattermost/platform/utils"
|
"github.com/mattermost/platform/utils"
|
||||||
|
"github.com/mattermost/platform/wsapi"
|
||||||
|
|
||||||
l4g "github.com/alecthomas/log4go"
|
l4g "github.com/alecthomas/log4go"
|
||||||
)
|
)
|
||||||
@@ -42,10 +43,12 @@ func SetupEnterprise() *TestHelper {
|
|||||||
app.NewServer()
|
app.NewServer()
|
||||||
app.InitStores()
|
app.InitStores()
|
||||||
InitRouter()
|
InitRouter()
|
||||||
|
wsapi.InitRouter()
|
||||||
app.StartServer()
|
app.StartServer()
|
||||||
utils.InitHTML()
|
utils.InitHTML()
|
||||||
api4.InitApi(false)
|
api4.InitApi(false)
|
||||||
InitApi()
|
InitApi()
|
||||||
|
wsapi.InitApi()
|
||||||
utils.EnableDebugLogForTest()
|
utils.EnableDebugLogForTest()
|
||||||
app.Srv.Store.MarkSystemRanUnitTests()
|
app.Srv.Store.MarkSystemRanUnitTests()
|
||||||
|
|
||||||
@@ -70,8 +73,10 @@ func Setup() *TestHelper {
|
|||||||
app.NewServer()
|
app.NewServer()
|
||||||
app.InitStores()
|
app.InitStores()
|
||||||
InitRouter()
|
InitRouter()
|
||||||
|
wsapi.InitRouter()
|
||||||
app.StartServer()
|
app.StartServer()
|
||||||
InitApi()
|
InitApi()
|
||||||
|
wsapi.InitApi()
|
||||||
utils.EnableDebugLogForTest()
|
utils.EnableDebugLogForTest()
|
||||||
app.Srv.Store.MarkSystemRanUnitTests()
|
app.Srv.Store.MarkSystemRanUnitTests()
|
||||||
|
|
||||||
|
|||||||
@@ -10,7 +10,6 @@ import (
|
|||||||
|
|
||||||
l4g "github.com/alecthomas/log4go"
|
l4g "github.com/alecthomas/log4go"
|
||||||
|
|
||||||
"github.com/mattermost/platform/app"
|
|
||||||
"github.com/mattermost/platform/model"
|
"github.com/mattermost/platform/model"
|
||||||
"github.com/mattermost/platform/utils"
|
"github.com/mattermost/platform/utils"
|
||||||
)
|
)
|
||||||
@@ -21,8 +20,6 @@ func InitGeneral() {
|
|||||||
BaseRoutes.General.Handle("/client_props", ApiAppHandler(getClientConfig)).Methods("GET")
|
BaseRoutes.General.Handle("/client_props", ApiAppHandler(getClientConfig)).Methods("GET")
|
||||||
BaseRoutes.General.Handle("/log_client", ApiAppHandler(logClient)).Methods("POST")
|
BaseRoutes.General.Handle("/log_client", ApiAppHandler(logClient)).Methods("POST")
|
||||||
BaseRoutes.General.Handle("/ping", ApiAppHandler(ping)).Methods("GET")
|
BaseRoutes.General.Handle("/ping", ApiAppHandler(ping)).Methods("GET")
|
||||||
|
|
||||||
app.Srv.WebSocketRouter.Handle("ping", ApiWebSocketHandler(webSocketPing))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func getClientConfig(c *Context, w http.ResponseWriter, r *http.Request) {
|
func getClientConfig(c *Context, w http.ResponseWriter, r *http.Request) {
|
||||||
@@ -72,13 +69,3 @@ func ping(c *Context, w http.ResponseWriter, r *http.Request) {
|
|||||||
m["server_time"] = fmt.Sprintf("%v", model.GetMillis())
|
m["server_time"] = fmt.Sprintf("%v", model.GetMillis())
|
||||||
w.Write([]byte(model.MapToJson(m)))
|
w.Write([]byte(model.MapToJson(m)))
|
||||||
}
|
}
|
||||||
|
|
||||||
func webSocketPing(req *model.WebSocketRequest) (map[string]interface{}, *model.AppError) {
|
|
||||||
data := map[string]interface{}{}
|
|
||||||
data["text"] = "pong"
|
|
||||||
data["version"] = model.CurrentVersion
|
|
||||||
data["server_time"] = model.GetMillis()
|
|
||||||
data["node_id"] = ""
|
|
||||||
|
|
||||||
return data, nil
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -18,8 +18,6 @@ func InitStatus() {
|
|||||||
|
|
||||||
BaseRoutes.Users.Handle("/status", ApiUserRequired(getStatusesHttp)).Methods("GET")
|
BaseRoutes.Users.Handle("/status", ApiUserRequired(getStatusesHttp)).Methods("GET")
|
||||||
BaseRoutes.Users.Handle("/status/ids", ApiUserRequired(getStatusesByIdsHttp)).Methods("POST")
|
BaseRoutes.Users.Handle("/status/ids", ApiUserRequired(getStatusesByIdsHttp)).Methods("POST")
|
||||||
app.Srv.WebSocketRouter.Handle("get_statuses", ApiWebSocketHandler(getStatusesWebSocket))
|
|
||||||
app.Srv.WebSocketRouter.Handle("get_statuses_by_ids", ApiWebSocketHandler(getStatusesByIdsWebSocket))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func getStatusesHttp(c *Context, w http.ResponseWriter, r *http.Request) {
|
func getStatusesHttp(c *Context, w http.ResponseWriter, r *http.Request) {
|
||||||
@@ -27,11 +25,6 @@ func getStatusesHttp(c *Context, w http.ResponseWriter, r *http.Request) {
|
|||||||
w.Write([]byte(model.StringInterfaceToJson(statusMap)))
|
w.Write([]byte(model.StringInterfaceToJson(statusMap)))
|
||||||
}
|
}
|
||||||
|
|
||||||
func getStatusesWebSocket(req *model.WebSocketRequest) (map[string]interface{}, *model.AppError) {
|
|
||||||
statusMap := app.GetAllStatuses()
|
|
||||||
return model.StatusMapToInterfaceMap(statusMap), nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func getStatusesByIdsHttp(c *Context, w http.ResponseWriter, r *http.Request) {
|
func getStatusesByIdsHttp(c *Context, w http.ResponseWriter, r *http.Request) {
|
||||||
userIds := model.ArrayFromJson(r.Body)
|
userIds := model.ArrayFromJson(r.Body)
|
||||||
|
|
||||||
@@ -48,18 +41,3 @@ func getStatusesByIdsHttp(c *Context, w http.ResponseWriter, r *http.Request) {
|
|||||||
|
|
||||||
w.Write([]byte(model.StringInterfaceToJson(statusMap)))
|
w.Write([]byte(model.StringInterfaceToJson(statusMap)))
|
||||||
}
|
}
|
||||||
|
|
||||||
func getStatusesByIdsWebSocket(req *model.WebSocketRequest) (map[string]interface{}, *model.AppError) {
|
|
||||||
var userIds []string
|
|
||||||
if userIds = model.ArrayFromInterface(req.Data["user_ids"]); len(userIds) == 0 {
|
|
||||||
l4g.Error(model.StringInterfaceToJson(req.Data))
|
|
||||||
return nil, NewInvalidWebSocketParamError(req.Action, "user_ids")
|
|
||||||
}
|
|
||||||
|
|
||||||
statusMap, err := app.GetStatusesByIds(userIds)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
return statusMap, nil
|
|
||||||
}
|
|
||||||
|
|||||||
25
api/user.go
25
api/user.go
@@ -71,8 +71,6 @@ func InitUser() {
|
|||||||
|
|
||||||
BaseRoutes.Root.Handle("/login/sso/saml", AppHandlerIndependent(loginWithSaml)).Methods("GET")
|
BaseRoutes.Root.Handle("/login/sso/saml", AppHandlerIndependent(loginWithSaml)).Methods("GET")
|
||||||
BaseRoutes.Root.Handle("/login/sso/saml", AppHandlerIndependent(completeSaml)).Methods("POST")
|
BaseRoutes.Root.Handle("/login/sso/saml", AppHandlerIndependent(completeSaml)).Methods("POST")
|
||||||
|
|
||||||
app.Srv.WebSocketRouter.Handle("user_typing", ApiWebSocketHandler(userTyping))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func createUser(c *Context, w http.ResponseWriter, r *http.Request) {
|
func createUser(c *Context, w http.ResponseWriter, r *http.Request) {
|
||||||
@@ -1442,29 +1440,6 @@ func completeSaml(c *Context, w http.ResponseWriter, r *http.Request) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func userTyping(req *model.WebSocketRequest) (map[string]interface{}, *model.AppError) {
|
|
||||||
var ok bool
|
|
||||||
var channelId string
|
|
||||||
if channelId, ok = req.Data["channel_id"].(string); !ok || len(channelId) != 26 {
|
|
||||||
return nil, NewInvalidWebSocketParamError(req.Action, "channel_id")
|
|
||||||
}
|
|
||||||
|
|
||||||
var parentId string
|
|
||||||
if parentId, ok = req.Data["parent_id"].(string); !ok {
|
|
||||||
parentId = ""
|
|
||||||
}
|
|
||||||
|
|
||||||
omitUsers := make(map[string]bool, 1)
|
|
||||||
omitUsers[req.Session.UserId] = true
|
|
||||||
|
|
||||||
event := model.NewWebSocketEvent(model.WEBSOCKET_EVENT_TYPING, "", channelId, "", omitUsers)
|
|
||||||
event.Add("parent_id", parentId)
|
|
||||||
event.Add("user_id", req.Session.UserId)
|
|
||||||
go app.Publish(event)
|
|
||||||
|
|
||||||
return nil, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func sanitizeProfile(c *Context, user *model.User) *model.User {
|
func sanitizeProfile(c *Context, user *model.User) *model.User {
|
||||||
options := utils.Cfg.GetSanitizeOptions()
|
options := utils.Cfg.GetSanitizeOptions()
|
||||||
|
|
||||||
|
|||||||
@@ -23,8 +23,6 @@ func InitWebrtc() {
|
|||||||
l4g.Debug(utils.T("api.webrtc.init.debug"))
|
l4g.Debug(utils.T("api.webrtc.init.debug"))
|
||||||
|
|
||||||
BaseRoutes.Webrtc.Handle("/token", ApiUserRequired(webrtcToken)).Methods("POST")
|
BaseRoutes.Webrtc.Handle("/token", ApiUserRequired(webrtcToken)).Methods("POST")
|
||||||
|
|
||||||
app.Srv.WebSocketRouter.Handle("webrtc", ApiWebSocketHandler(webrtcMessage))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func webrtcToken(c *Context, w http.ResponseWriter, r *http.Request) {
|
func webrtcToken(c *Context, w http.ResponseWriter, r *http.Request) {
|
||||||
@@ -52,20 +50,6 @@ func webrtcToken(c *Context, w http.ResponseWriter, r *http.Request) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func webrtcMessage(req *model.WebSocketRequest) (map[string]interface{}, *model.AppError) {
|
|
||||||
var ok bool
|
|
||||||
var toUserId string
|
|
||||||
if toUserId, ok = req.Data["to_user_id"].(string); !ok || len(toUserId) != 26 {
|
|
||||||
return nil, NewInvalidWebSocketParamError(req.Action, "to_user_id")
|
|
||||||
}
|
|
||||||
|
|
||||||
event := model.NewWebSocketEvent(model.WEBSOCKET_EVENT_WEBRTC, "", "", toUserId, nil)
|
|
||||||
event.Data = req.Data
|
|
||||||
go app.Publish(event)
|
|
||||||
|
|
||||||
return nil, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func getWebrtcToken(sessionId string) (string, *model.AppError) {
|
func getWebrtcToken(sessionId string) (string, *model.AppError) {
|
||||||
if !*utils.Cfg.WebrtcSettings.Enable {
|
if !*utils.Cfg.WebrtcSettings.Enable {
|
||||||
return "", model.NewLocAppError("WebRTC.getWebrtcToken", "api.webrtc.disabled.app_error", nil, "")
|
return "", model.NewLocAppError("WebRTC.getWebrtcToken", "api.webrtc.disabled.app_error", nil, "")
|
||||||
|
|||||||
@@ -5,7 +5,6 @@ package api
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"net/http"
|
"net/http"
|
||||||
"strings"
|
|
||||||
|
|
||||||
l4g "github.com/alecthomas/log4go"
|
l4g "github.com/alecthomas/log4go"
|
||||||
"github.com/gorilla/websocket"
|
"github.com/gorilla/websocket"
|
||||||
@@ -17,23 +16,10 @@ import (
|
|||||||
func InitWebSocket() {
|
func InitWebSocket() {
|
||||||
l4g.Debug(utils.T("api.web_socket.init.debug"))
|
l4g.Debug(utils.T("api.web_socket.init.debug"))
|
||||||
BaseRoutes.Users.Handle("/websocket", ApiAppHandlerTrustRequester(connect)).Methods("GET")
|
BaseRoutes.Users.Handle("/websocket", ApiAppHandlerTrustRequester(connect)).Methods("GET")
|
||||||
app.HubStart()
|
|
||||||
}
|
|
||||||
|
|
||||||
type OriginCheckerProc func(*http.Request) bool
|
|
||||||
|
|
||||||
func OriginChecker(r *http.Request) bool {
|
|
||||||
origin := r.Header.Get("Origin")
|
|
||||||
return *utils.Cfg.ServiceSettings.AllowCorsFrom == "*" || strings.Contains(origin, *utils.Cfg.ServiceSettings.AllowCorsFrom)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func connect(c *Context, w http.ResponseWriter, r *http.Request) {
|
func connect(c *Context, w http.ResponseWriter, r *http.Request) {
|
||||||
|
originChecker := utils.GetOriginChecker(r)
|
||||||
var originChecker OriginCheckerProc = nil
|
|
||||||
|
|
||||||
if len(*utils.Cfg.ServiceSettings.AllowCorsFrom) > 0 {
|
|
||||||
originChecker = OriginChecker
|
|
||||||
}
|
|
||||||
|
|
||||||
upgrader := websocket.Upgrader{
|
upgrader := websocket.Upgrader{
|
||||||
ReadBufferSize: model.SOCKET_MAX_MESSAGE_SIZE_KB,
|
ReadBufferSize: model.SOCKET_MAX_MESSAGE_SIZE_KB,
|
||||||
|
|||||||
@@ -92,7 +92,6 @@ var BaseRoutes *Routes
|
|||||||
func InitRouter() {
|
func InitRouter() {
|
||||||
app.Srv.Router = mux.NewRouter()
|
app.Srv.Router = mux.NewRouter()
|
||||||
app.Srv.Router.NotFoundHandler = http.HandlerFunc(Handle404)
|
app.Srv.Router.NotFoundHandler = http.HandlerFunc(Handle404)
|
||||||
app.Srv.WebSocketRouter = app.NewWebSocketRouter()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func InitApi(full bool) {
|
func InitApi(full bool) {
|
||||||
@@ -174,6 +173,7 @@ func InitApi(full bool) {
|
|||||||
InitBrand()
|
InitBrand()
|
||||||
InitCommand()
|
InitCommand()
|
||||||
InitStatus()
|
InitStatus()
|
||||||
|
InitWebSocket()
|
||||||
|
|
||||||
app.Srv.Router.Handle("/api/v4/{anything:.*}", http.HandlerFunc(Handle404))
|
app.Srv.Router.Handle("/api/v4/{anything:.*}", http.HandlerFunc(Handle404))
|
||||||
|
|
||||||
|
|||||||
@@ -20,6 +20,7 @@ import (
|
|||||||
"github.com/mattermost/platform/model"
|
"github.com/mattermost/platform/model"
|
||||||
"github.com/mattermost/platform/store"
|
"github.com/mattermost/platform/store"
|
||||||
"github.com/mattermost/platform/utils"
|
"github.com/mattermost/platform/utils"
|
||||||
|
"github.com/mattermost/platform/wsapi"
|
||||||
|
|
||||||
s3 "github.com/minio/minio-go"
|
s3 "github.com/minio/minio-go"
|
||||||
)
|
)
|
||||||
@@ -55,9 +56,11 @@ func SetupEnterprise() *TestHelper {
|
|||||||
app.NewServer()
|
app.NewServer()
|
||||||
app.InitStores()
|
app.InitStores()
|
||||||
InitRouter()
|
InitRouter()
|
||||||
|
wsapi.InitRouter()
|
||||||
app.StartServer()
|
app.StartServer()
|
||||||
utils.InitHTML()
|
utils.InitHTML()
|
||||||
InitApi(true)
|
InitApi(true)
|
||||||
|
wsapi.InitApi()
|
||||||
utils.EnableDebugLogForTest()
|
utils.EnableDebugLogForTest()
|
||||||
app.Srv.Store.MarkSystemRanUnitTests()
|
app.Srv.Store.MarkSystemRanUnitTests()
|
||||||
|
|
||||||
@@ -85,8 +88,10 @@ func Setup() *TestHelper {
|
|||||||
app.NewServer()
|
app.NewServer()
|
||||||
app.InitStores()
|
app.InitStores()
|
||||||
InitRouter()
|
InitRouter()
|
||||||
|
wsapi.InitRouter()
|
||||||
app.StartServer()
|
app.StartServer()
|
||||||
InitApi(true)
|
InitApi(true)
|
||||||
|
wsapi.InitApi()
|
||||||
utils.EnableDebugLogForTest()
|
utils.EnableDebugLogForTest()
|
||||||
app.Srv.Store.MarkSystemRanUnitTests()
|
app.Srv.Store.MarkSystemRanUnitTests()
|
||||||
|
|
||||||
@@ -167,6 +172,10 @@ func (me *TestHelper) CreateClient() *model.Client4 {
|
|||||||
return model.NewAPIv4Client("http://localhost" + utils.Cfg.ServiceSettings.ListenAddress)
|
return model.NewAPIv4Client("http://localhost" + utils.Cfg.ServiceSettings.ListenAddress)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (me *TestHelper) CreateWebSocketClient() (*model.WebSocketClient, *model.AppError) {
|
||||||
|
return model.NewWebSocketClient4("ws://localhost"+utils.Cfg.ServiceSettings.ListenAddress, me.Client.AuthToken)
|
||||||
|
}
|
||||||
|
|
||||||
func (me *TestHelper) CreateUser() *model.User {
|
func (me *TestHelper) CreateUser() *model.User {
|
||||||
return me.CreateUserWithClient(me.Client)
|
return me.CreateUserWithClient(me.Client)
|
||||||
}
|
}
|
||||||
|
|||||||
46
api4/websocket.go
Обычный файл
46
api4/websocket.go
Обычный файл
@@ -0,0 +1,46 @@
|
|||||||
|
// Copyright (c) 2017 Mattermost, Inc. All Rights Reserved.
|
||||||
|
// See License.txt for license information.
|
||||||
|
|
||||||
|
package api4
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net/http"
|
||||||
|
|
||||||
|
l4g "github.com/alecthomas/log4go"
|
||||||
|
"github.com/gorilla/websocket"
|
||||||
|
"github.com/mattermost/platform/app"
|
||||||
|
"github.com/mattermost/platform/model"
|
||||||
|
"github.com/mattermost/platform/utils"
|
||||||
|
)
|
||||||
|
|
||||||
|
func InitWebSocket() {
|
||||||
|
l4g.Debug(utils.T("api.web_socket.init.debug"))
|
||||||
|
|
||||||
|
BaseRoutes.ApiRoot.Handle("/websocket", ApiHandlerTrustRequester(connectWebSocket)).Methods("GET")
|
||||||
|
}
|
||||||
|
|
||||||
|
func connectWebSocket(c *Context, w http.ResponseWriter, r *http.Request) {
|
||||||
|
originChecker := utils.GetOriginChecker(r)
|
||||||
|
|
||||||
|
upgrader := websocket.Upgrader{
|
||||||
|
ReadBufferSize: model.SOCKET_MAX_MESSAGE_SIZE_KB,
|
||||||
|
WriteBufferSize: model.SOCKET_MAX_MESSAGE_SIZE_KB,
|
||||||
|
CheckOrigin: originChecker,
|
||||||
|
}
|
||||||
|
|
||||||
|
ws, err := upgrader.Upgrade(w, r, nil)
|
||||||
|
if err != nil {
|
||||||
|
l4g.Error(utils.T("api.web_socket.connect.error"), err)
|
||||||
|
c.Err = model.NewLocAppError("connect", "api.web_socket.connect.upgrade.app_error", nil, "")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
wc := app.NewWebConn(ws, c.Session, c.T, "")
|
||||||
|
|
||||||
|
if len(c.Session.UserId) > 0 {
|
||||||
|
app.HubRegister(wc)
|
||||||
|
}
|
||||||
|
|
||||||
|
go wc.WritePump()
|
||||||
|
wc.ReadPump()
|
||||||
|
}
|
||||||
73
api4/websocket_test.go
Обычный файл
73
api4/websocket_test.go
Обычный файл
@@ -0,0 +1,73 @@
|
|||||||
|
// Copyright (c) 2017 Mattermost, Inc. All Rights Reserved.
|
||||||
|
// See License.txt for license information.
|
||||||
|
|
||||||
|
package api4
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/mattermost/platform/model"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestWebSocket(t *testing.T) {
|
||||||
|
th := Setup().InitBasic()
|
||||||
|
defer TearDown()
|
||||||
|
WebSocketClient, err := th.CreateWebSocketClient()
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
defer WebSocketClient.Close()
|
||||||
|
|
||||||
|
time.Sleep(300 * time.Millisecond)
|
||||||
|
|
||||||
|
// Test closing and reconnecting
|
||||||
|
WebSocketClient.Close()
|
||||||
|
if err := WebSocketClient.Connect(); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
WebSocketClient.Listen()
|
||||||
|
|
||||||
|
time.Sleep(300 * time.Millisecond)
|
||||||
|
if resp := <-WebSocketClient.ResponseChannel; resp.Status != model.STATUS_OK {
|
||||||
|
t.Fatal("should have responded OK to authentication challenge")
|
||||||
|
}
|
||||||
|
|
||||||
|
WebSocketClient.SendMessage("ping", nil)
|
||||||
|
time.Sleep(300 * time.Millisecond)
|
||||||
|
if resp := <-WebSocketClient.ResponseChannel; resp.Data["text"].(string) != "pong" {
|
||||||
|
t.Fatal("wrong response")
|
||||||
|
}
|
||||||
|
|
||||||
|
WebSocketClient.SendMessage("", nil)
|
||||||
|
time.Sleep(300 * time.Millisecond)
|
||||||
|
if resp := <-WebSocketClient.ResponseChannel; resp.Error.Id != "api.web_socket_router.no_action.app_error" {
|
||||||
|
t.Fatal("should have been no action response")
|
||||||
|
}
|
||||||
|
|
||||||
|
WebSocketClient.SendMessage("junk", nil)
|
||||||
|
time.Sleep(300 * time.Millisecond)
|
||||||
|
if resp := <-WebSocketClient.ResponseChannel; resp.Error.Id != "api.web_socket_router.bad_action.app_error" {
|
||||||
|
t.Fatal("should have been bad action response")
|
||||||
|
}
|
||||||
|
|
||||||
|
req := &model.WebSocketRequest{}
|
||||||
|
req.Seq = 0
|
||||||
|
req.Action = "ping"
|
||||||
|
WebSocketClient.Conn.WriteJSON(req)
|
||||||
|
time.Sleep(300 * time.Millisecond)
|
||||||
|
if resp := <-WebSocketClient.ResponseChannel; resp.Error.Id != "api.web_socket_router.bad_seq.app_error" {
|
||||||
|
t.Fatal("should have been bad action response")
|
||||||
|
}
|
||||||
|
|
||||||
|
WebSocketClient.UserTyping("", "")
|
||||||
|
time.Sleep(300 * time.Millisecond)
|
||||||
|
if resp := <-WebSocketClient.ResponseChannel; resp.Error.Id != "api.websocket_handler.invalid_param.app_error" {
|
||||||
|
t.Fatal("should have been invalid param response")
|
||||||
|
} else {
|
||||||
|
if resp.Error.DetailedError != "" {
|
||||||
|
t.Fatal("detailed error not cleared")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -18,6 +18,7 @@ import (
|
|||||||
"github.com/mattermost/platform/model"
|
"github.com/mattermost/platform/model"
|
||||||
"github.com/mattermost/platform/utils"
|
"github.com/mattermost/platform/utils"
|
||||||
"github.com/mattermost/platform/web"
|
"github.com/mattermost/platform/web"
|
||||||
|
"github.com/mattermost/platform/wsapi"
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -62,8 +63,10 @@ func runServer(configFileLocation string) {
|
|||||||
app.NewServer()
|
app.NewServer()
|
||||||
app.InitStores()
|
app.InitStores()
|
||||||
api.InitRouter()
|
api.InitRouter()
|
||||||
|
wsapi.InitRouter()
|
||||||
api4.InitApi(false)
|
api4.InitApi(false)
|
||||||
api.InitApi()
|
api.InitApi()
|
||||||
|
wsapi.InitApi()
|
||||||
web.InitWeb()
|
web.InitWeb()
|
||||||
|
|
||||||
if model.BuildEnterpriseReady == "true" {
|
if model.BuildEnterpriseReady == "true" {
|
||||||
|
|||||||
@@ -13,6 +13,7 @@ import (
|
|||||||
"github.com/mattermost/platform/api4"
|
"github.com/mattermost/platform/api4"
|
||||||
"github.com/mattermost/platform/app"
|
"github.com/mattermost/platform/app"
|
||||||
"github.com/mattermost/platform/utils"
|
"github.com/mattermost/platform/utils"
|
||||||
|
"github.com/mattermost/platform/wsapi"
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
"os/signal"
|
"os/signal"
|
||||||
"syscall"
|
"syscall"
|
||||||
@@ -47,8 +48,10 @@ func webClientTestsCmdF(cmd *cobra.Command, args []string) error {
|
|||||||
initDBCommandContextCobra(cmd)
|
initDBCommandContextCobra(cmd)
|
||||||
utils.InitTranslations(utils.Cfg.LocalizationSettings)
|
utils.InitTranslations(utils.Cfg.LocalizationSettings)
|
||||||
api.InitRouter()
|
api.InitRouter()
|
||||||
|
wsapi.InitRouter()
|
||||||
api4.InitApi(false)
|
api4.InitApi(false)
|
||||||
api.InitApi()
|
api.InitApi()
|
||||||
|
wsapi.InitApi()
|
||||||
setupClientTests()
|
setupClientTests()
|
||||||
app.StartServer()
|
app.StartServer()
|
||||||
runWebClientTests()
|
runWebClientTests()
|
||||||
@@ -61,8 +64,10 @@ func serverForWebClientTestsCmdF(cmd *cobra.Command, args []string) error {
|
|||||||
initDBCommandContextCobra(cmd)
|
initDBCommandContextCobra(cmd)
|
||||||
utils.InitTranslations(utils.Cfg.LocalizationSettings)
|
utils.InitTranslations(utils.Cfg.LocalizationSettings)
|
||||||
api.InitRouter()
|
api.InitRouter()
|
||||||
|
wsapi.InitRouter()
|
||||||
api4.InitApi(false)
|
api4.InitApi(false)
|
||||||
api.InitApi()
|
api.InitApi()
|
||||||
|
wsapi.InitApi()
|
||||||
setupClientTests()
|
setupClientTests()
|
||||||
app.StartServer()
|
app.StartServer()
|
||||||
|
|
||||||
|
|||||||
16
i18n/en.json
16
i18n/en.json
@@ -151,6 +151,22 @@
|
|||||||
"id": "api.api.render.error",
|
"id": "api.api.render.error",
|
||||||
"translation": "Error rendering template %v err=%v"
|
"translation": "Error rendering template %v err=%v"
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
"id": "wsapi.user.init.debug",
|
||||||
|
"translation": "Initializing user WebSocket API routes"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"id": "wsapi.system.init.debug",
|
||||||
|
"translation": "Initializing system WebSocket API routes"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"id": "wsapi.status.init.debug",
|
||||||
|
"translation": "Initializing status WebSocket API routes"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"id": "wsapi.webrtc.init.debug",
|
||||||
|
"translation": "Initializing webrtc WebSocket API routes"
|
||||||
|
},
|
||||||
{
|
{
|
||||||
"id": "api.auth.unable_to_get_user.app_error",
|
"id": "api.auth.unable_to_get_user.app_error",
|
||||||
"translation": "Unable to get user to check permissions."
|
"translation": "Unable to get user to check permissions."
|
||||||
|
|||||||
@@ -15,6 +15,7 @@ const (
|
|||||||
type WebSocketClient struct {
|
type WebSocketClient struct {
|
||||||
Url string // The location of the server like "ws://localhost:8065"
|
Url string // The location of the server like "ws://localhost:8065"
|
||||||
ApiUrl string // The api location of the server like "ws://localhost:8065/api/v3"
|
ApiUrl string // The api location of the server like "ws://localhost:8065/api/v3"
|
||||||
|
ConnectUrl string // The websocket URL to connect to like "ws://localhost:8065/api/v3/path/to/websocket"
|
||||||
Conn *websocket.Conn // The WebSocket connection
|
Conn *websocket.Conn // The WebSocket connection
|
||||||
AuthToken string // The token used to open the WebSocket
|
AuthToken string // The token used to open the WebSocket
|
||||||
Sequence int64 // The ever-incrementing sequence attached to each WebSocket action
|
Sequence int64 // The ever-incrementing sequence attached to each WebSocket action
|
||||||
@@ -34,6 +35,32 @@ func NewWebSocketClient(url, authToken string) (*WebSocketClient, *AppError) {
|
|||||||
client := &WebSocketClient{
|
client := &WebSocketClient{
|
||||||
url,
|
url,
|
||||||
url + API_URL_SUFFIX_V3,
|
url + API_URL_SUFFIX_V3,
|
||||||
|
url + API_URL_SUFFIX_V3 + "/users/websocket",
|
||||||
|
conn,
|
||||||
|
authToken,
|
||||||
|
1,
|
||||||
|
make(chan *WebSocketEvent, 100),
|
||||||
|
make(chan *WebSocketResponse, 100),
|
||||||
|
nil,
|
||||||
|
}
|
||||||
|
|
||||||
|
client.SendMessage(WEBSOCKET_AUTHENTICATION_CHALLENGE, map[string]interface{}{"token": authToken})
|
||||||
|
|
||||||
|
return client, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewWebSocketClient4 constructs a new WebSocket client with convienence
|
||||||
|
// methods for talking to the server. Uses the v4 endpoint.
|
||||||
|
func NewWebSocketClient4(url, authToken string) (*WebSocketClient, *AppError) {
|
||||||
|
conn, _, err := websocket.DefaultDialer.Dial(url+API_URL_SUFFIX+"/websocket", nil)
|
||||||
|
if err != nil {
|
||||||
|
return nil, NewLocAppError("NewWebSocketClient4", "model.websocket_client.connect_fail.app_error", nil, err.Error())
|
||||||
|
}
|
||||||
|
|
||||||
|
client := &WebSocketClient{
|
||||||
|
url,
|
||||||
|
url + API_URL_SUFFIX,
|
||||||
|
url + API_URL_SUFFIX + "/websocket",
|
||||||
conn,
|
conn,
|
||||||
authToken,
|
authToken,
|
||||||
1,
|
1,
|
||||||
@@ -49,9 +76,9 @@ func NewWebSocketClient(url, authToken string) (*WebSocketClient, *AppError) {
|
|||||||
|
|
||||||
func (wsc *WebSocketClient) Connect() *AppError {
|
func (wsc *WebSocketClient) Connect() *AppError {
|
||||||
var err error
|
var err error
|
||||||
wsc.Conn, _, err = websocket.DefaultDialer.Dial(wsc.ApiUrl+"/users/websocket", nil)
|
wsc.Conn, _, err = websocket.DefaultDialer.Dial(wsc.ConnectUrl, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return NewLocAppError("NewWebSocketClient", "model.websocket_client.connect_fail.app_error", nil, err.Error())
|
return NewLocAppError("Connect", "model.websocket_client.connect_fail.app_error", nil, err.Error())
|
||||||
}
|
}
|
||||||
|
|
||||||
wsc.EventChannel = make(chan *WebSocketEvent, 100)
|
wsc.EventChannel = make(chan *WebSocketEvent, 100)
|
||||||
|
|||||||
24
utils/api.go
Обычный файл
24
utils/api.go
Обычный файл
@@ -0,0 +1,24 @@
|
|||||||
|
// Copyright (c) 2017 Mattermost, Inc. All Rights Reserved.
|
||||||
|
// See License.txt for license information.
|
||||||
|
|
||||||
|
package utils
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net/http"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
type OriginCheckerProc func(*http.Request) bool
|
||||||
|
|
||||||
|
func OriginChecker(r *http.Request) bool {
|
||||||
|
origin := r.Header.Get("Origin")
|
||||||
|
return *Cfg.ServiceSettings.AllowCorsFrom == "*" || strings.Contains(origin, *Cfg.ServiceSettings.AllowCorsFrom)
|
||||||
|
}
|
||||||
|
|
||||||
|
func GetOriginChecker(r *http.Request) OriginCheckerProc {
|
||||||
|
if len(*Cfg.ServiceSettings.AllowCorsFrom) > 0 {
|
||||||
|
return OriginChecker
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
21
wsapi/api.go
Обычный файл
21
wsapi/api.go
Обычный файл
@@ -0,0 +1,21 @@
|
|||||||
|
// Copyright (c) 2017 Mattermost, Inc. All Rights Reserved.
|
||||||
|
// See License.txt for license information.
|
||||||
|
|
||||||
|
package wsapi
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/mattermost/platform/app"
|
||||||
|
)
|
||||||
|
|
||||||
|
func InitRouter() {
|
||||||
|
app.Srv.WebSocketRouter = app.NewWebSocketRouter()
|
||||||
|
}
|
||||||
|
|
||||||
|
func InitApi() {
|
||||||
|
InitUser()
|
||||||
|
InitSystem()
|
||||||
|
InitStatus()
|
||||||
|
InitWebrtc()
|
||||||
|
|
||||||
|
app.HubStart()
|
||||||
|
}
|
||||||
38
wsapi/status.go
Обычный файл
38
wsapi/status.go
Обычный файл
@@ -0,0 +1,38 @@
|
|||||||
|
// Copyright (c) 2017 Mattermost, Inc. All Rights Reserved.
|
||||||
|
// See License.txt for license information.
|
||||||
|
|
||||||
|
package wsapi
|
||||||
|
|
||||||
|
import (
|
||||||
|
l4g "github.com/alecthomas/log4go"
|
||||||
|
"github.com/mattermost/platform/app"
|
||||||
|
"github.com/mattermost/platform/model"
|
||||||
|
"github.com/mattermost/platform/utils"
|
||||||
|
)
|
||||||
|
|
||||||
|
func InitStatus() {
|
||||||
|
l4g.Debug(utils.T("wsapi.status.init.debug"))
|
||||||
|
|
||||||
|
app.Srv.WebSocketRouter.Handle("get_statuses", ApiWebSocketHandler(getStatuses))
|
||||||
|
app.Srv.WebSocketRouter.Handle("get_statuses_by_ids", ApiWebSocketHandler(getStatusesByIds))
|
||||||
|
}
|
||||||
|
|
||||||
|
func getStatuses(req *model.WebSocketRequest) (map[string]interface{}, *model.AppError) {
|
||||||
|
statusMap := app.GetAllStatuses()
|
||||||
|
return model.StatusMapToInterfaceMap(statusMap), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func getStatusesByIds(req *model.WebSocketRequest) (map[string]interface{}, *model.AppError) {
|
||||||
|
var userIds []string
|
||||||
|
if userIds = model.ArrayFromInterface(req.Data["user_ids"]); len(userIds) == 0 {
|
||||||
|
l4g.Error(model.StringInterfaceToJson(req.Data))
|
||||||
|
return nil, NewInvalidWebSocketParamError(req.Action, "user_ids")
|
||||||
|
}
|
||||||
|
|
||||||
|
statusMap, err := app.GetStatusesByIds(userIds)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return statusMap, nil
|
||||||
|
}
|
||||||
27
wsapi/system.go
Обычный файл
27
wsapi/system.go
Обычный файл
@@ -0,0 +1,27 @@
|
|||||||
|
// Copyright (c) 2017 Mattermost, Inc. All Rights Reserved.
|
||||||
|
// See License.txt for license information.
|
||||||
|
|
||||||
|
package wsapi
|
||||||
|
|
||||||
|
import (
|
||||||
|
l4g "github.com/alecthomas/log4go"
|
||||||
|
"github.com/mattermost/platform/app"
|
||||||
|
"github.com/mattermost/platform/model"
|
||||||
|
"github.com/mattermost/platform/utils"
|
||||||
|
)
|
||||||
|
|
||||||
|
func InitSystem() {
|
||||||
|
l4g.Debug(utils.T("wsapi.system.init.debug"))
|
||||||
|
|
||||||
|
app.Srv.WebSocketRouter.Handle("ping", ApiWebSocketHandler(ping))
|
||||||
|
}
|
||||||
|
|
||||||
|
func ping(req *model.WebSocketRequest) (map[string]interface{}, *model.AppError) {
|
||||||
|
data := map[string]interface{}{}
|
||||||
|
data["text"] = "pong"
|
||||||
|
data["version"] = model.CurrentVersion
|
||||||
|
data["server_time"] = model.GetMillis()
|
||||||
|
data["node_id"] = ""
|
||||||
|
|
||||||
|
return data, nil
|
||||||
|
}
|
||||||
40
wsapi/user.go
Обычный файл
40
wsapi/user.go
Обычный файл
@@ -0,0 +1,40 @@
|
|||||||
|
// Copyright (c) 2017 Mattermost, Inc. All Rights Reserved.
|
||||||
|
// See License.txt for license information.
|
||||||
|
|
||||||
|
package wsapi
|
||||||
|
|
||||||
|
import (
|
||||||
|
l4g "github.com/alecthomas/log4go"
|
||||||
|
"github.com/mattermost/platform/app"
|
||||||
|
"github.com/mattermost/platform/model"
|
||||||
|
"github.com/mattermost/platform/utils"
|
||||||
|
)
|
||||||
|
|
||||||
|
func InitUser() {
|
||||||
|
l4g.Debug(utils.T("wsapi.user.init.debug"))
|
||||||
|
|
||||||
|
app.Srv.WebSocketRouter.Handle("user_typing", ApiWebSocketHandler(userTyping))
|
||||||
|
}
|
||||||
|
|
||||||
|
func userTyping(req *model.WebSocketRequest) (map[string]interface{}, *model.AppError) {
|
||||||
|
var ok bool
|
||||||
|
var channelId string
|
||||||
|
if channelId, ok = req.Data["channel_id"].(string); !ok || len(channelId) != 26 {
|
||||||
|
return nil, NewInvalidWebSocketParamError(req.Action, "channel_id")
|
||||||
|
}
|
||||||
|
|
||||||
|
var parentId string
|
||||||
|
if parentId, ok = req.Data["parent_id"].(string); !ok {
|
||||||
|
parentId = ""
|
||||||
|
}
|
||||||
|
|
||||||
|
omitUsers := make(map[string]bool, 1)
|
||||||
|
omitUsers[req.Session.UserId] = true
|
||||||
|
|
||||||
|
event := model.NewWebSocketEvent(model.WEBSOCKET_EVENT_TYPING, "", channelId, "", omitUsers)
|
||||||
|
event.Add("parent_id", parentId)
|
||||||
|
event.Add("user_id", req.Session.UserId)
|
||||||
|
go app.Publish(event)
|
||||||
|
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
31
wsapi/webrtc.go
Обычный файл
31
wsapi/webrtc.go
Обычный файл
@@ -0,0 +1,31 @@
|
|||||||
|
// Copyright (c) 2017 Mattermost, Inc. All Rights Reserved.
|
||||||
|
// See License.txt for license information.
|
||||||
|
|
||||||
|
package wsapi
|
||||||
|
|
||||||
|
import (
|
||||||
|
l4g "github.com/alecthomas/log4go"
|
||||||
|
"github.com/mattermost/platform/app"
|
||||||
|
"github.com/mattermost/platform/model"
|
||||||
|
"github.com/mattermost/platform/utils"
|
||||||
|
)
|
||||||
|
|
||||||
|
func InitWebrtc() {
|
||||||
|
l4g.Debug(utils.T("wsapi.webtrc.init.debug"))
|
||||||
|
|
||||||
|
app.Srv.WebSocketRouter.Handle("webrtc", ApiWebSocketHandler(webrtcMessage))
|
||||||
|
}
|
||||||
|
|
||||||
|
func webrtcMessage(req *model.WebSocketRequest) (map[string]interface{}, *model.AppError) {
|
||||||
|
var ok bool
|
||||||
|
var toUserId string
|
||||||
|
if toUserId, ok = req.Data["to_user_id"].(string); !ok || len(toUserId) != 26 {
|
||||||
|
return nil, NewInvalidWebSocketParamError(req.Action, "to_user_id")
|
||||||
|
}
|
||||||
|
|
||||||
|
event := model.NewWebSocketEvent(model.WEBSOCKET_EVENT_WEBRTC, "", "", toUserId, nil)
|
||||||
|
event.Data = req.Data
|
||||||
|
go app.Publish(event)
|
||||||
|
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
@@ -1,7 +1,7 @@
|
|||||||
// Copyright (c) 2016 Mattermost, Inc. All Rights Reserved.
|
// Copyright (c) 2016 Mattermost, Inc. All Rights Reserved.
|
||||||
// See License.txt for license information.
|
// See License.txt for license information.
|
||||||
|
|
||||||
package api
|
package wsapi
|
||||||
|
|
||||||
import (
|
import (
|
||||||
l4g "github.com/alecthomas/log4go"
|
l4g "github.com/alecthomas/log4go"
|
||||||
Ссылка в новой задаче
Block a user