当前位置: 首页>>代码示例>>Golang>>正文


Golang WebSocketEvent.ToJson方法代码示例

本文整理汇总了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()
	}
}
开发者ID:RangerRick,项目名称:platform,代码行数:100,代码来源:websocket_test.go


注:本文中的github.com/mattermost/platform/model.WebSocketEvent.ToJson方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。