本文整理汇总了Golang中github.com/mattermost/platform/model.WebSocketEvent.ToJson方法的典型用法代码示例。如果您正苦于以下问题:Golang WebSocketEvent.ToJson方法的具体用法?Golang WebSocketEvent.ToJson怎么用?Golang WebSocketEvent.ToJson使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类github.com/mattermost/platform/model.WebSocketEvent
的用法示例。
在下文中一共展示了WebSocketEvent.ToJson方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: TestWebSocketAuthentication
func TestWebSocketAuthentication(t *testing.T) {
th := Setup().InitBasic()
WebSocketClient, err := th.CreateWebSocketClient()
if 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.Close()
authToken := WebSocketClient.AuthToken
WebSocketClient.AuthToken = "junk"
if err := WebSocketClient.Connect(); err != nil {
t.Fatal(err)
}
WebSocketClient.Listen()
if resp := <-WebSocketClient.ResponseChannel; resp != nil {
t.Fatal("should have closed")
}
WebSocketClient.Close()
if conn, _, err := websocket.DefaultDialer.Dial(WebSocketClient.ApiUrl+"/users/websocket", nil); err != nil {
t.Fatal("should have connected")
} else {
req := &model.WebSocketRequest{}
req.Seq = 1
req.Action = "ping"
conn.WriteJSON(req)
closedAutomatically := false
hitNotAuthedError := false
go func() {
time.Sleep(10 * time.Second)
conn.Close()
if !closedAutomatically {
t.Fatal("should have closed automatically in 5 seconds")
}
}()
for {
if _, rawMsg, err := conn.ReadMessage(); err != nil {
closedAutomatically = true
conn.Close()
break
} else {
var response model.WebSocketResponse
if err := json.Unmarshal(rawMsg, &response); err != nil && !response.IsValid() {
t.Fatal("should not have failed")
} else {
if response.Error == nil || response.Error.Id != "api.web_socket_router.not_authenticated.app_error" {
t.Log(response.Error.Id)
t.Fatal("wrong error")
continue
}
hitNotAuthedError = true
}
}
}
if !hitNotAuthedError {
t.Fatal("should have received a not authenticated response")
}
}
header := http.Header{}
header.Set(model.HEADER_AUTH, "BEARER "+authToken)
if conn, _, err := websocket.DefaultDialer.Dial(WebSocketClient.ApiUrl+"/users/websocket", header); err != nil {
t.Fatal("should have connected")
} else {
if _, rawMsg, err := conn.ReadMessage(); err != nil {
t.Fatal("should not have closed automatically")
} else {
var event model.WebSocketEvent
if err := json.Unmarshal(rawMsg, &event); err != nil && !event.IsValid() {
t.Fatal("should not have failed")
} else if event.Event != model.WEBSOCKET_EVENT_HELLO {
t.Log(event.ToJson())
t.Fatal("should have helloed")
}
}
conn.Close()
}
}