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


Golang model.WebSocketEvent类代码示例

本文整理汇总了Golang中github.com/mattermost/platform/model.WebSocketEvent的典型用法代码示例。如果您正苦于以下问题:Golang WebSocketEvent类的具体用法?Golang WebSocketEvent怎么用?Golang WebSocketEvent使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。


在下文中一共展示了WebSocketEvent类的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。

示例1: Publish

func Publish(message *model.WebSocketEvent) {
	message.DoPreComputeJson()
	for _, hub := range hubs {
		hub.Broadcast(message)
	}

	if einterfaces.GetClusterInterface() != nil {
		einterfaces.GetClusterInterface().Publish(message)
	}
}
开发者ID:ZJvandeWeg,项目名称:platform,代码行数:10,代码来源:web_hub.go

示例2: WsReceiver

func (m *MMClient) WsReceiver() {
	for {
		var rawMsg json.RawMessage
		var err error

		if m.WsQuit {
			m.log.Debug("exiting WsReceiver")
			return
		}

		if !m.WsConnected {
			time.Sleep(time.Millisecond * 100)
			continue
		}

		if _, rawMsg, err = m.WsClient.ReadMessage(); err != nil {
			m.log.Error("error:", err)
			// reconnect
			m.Login()
		}

		var event model.WebSocketEvent
		if err := json.Unmarshal(rawMsg, &event); err == nil && event.IsValid() {
			m.log.Debugf("WsReceiver: %#v", event)
			msg := &Message{Raw: &event, Team: m.Credentials.Team}
			m.parseMessage(msg)
			m.MessageChan <- msg
			continue
		}

		var response model.WebSocketResponse
		if err := json.Unmarshal(rawMsg, &response); err == nil && response.IsValid() {
			m.log.Debugf("WsReceiver: %#v", response)
			m.parseResponse(response)
			continue
		}
	}
}
开发者ID:42wim,项目名称:matterircd,代码行数:38,代码来源:matterclient.go

示例3: PublishSkipClusterSend

func PublishSkipClusterSend(message *model.WebSocketEvent) {
	message.DoPreComputeJson()
	for _, hub := range hubs {
		hub.Broadcast(message)
	}
}
开发者ID:ZJvandeWeg,项目名称:platform,代码行数:6,代码来源:web_hub.go

示例4: 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类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。