|
@@ -48,8 +48,6 @@ func NewHub(cfg HubConfig) *Hub {
|
|
|
go hub.run()
|
|
|
|
|
|
go hub.remotelyEvent() // 远程事件
|
|
|
- //go hub.remotelyMessageReader()
|
|
|
- //go hub.remotelyMessageWriter()
|
|
|
return hub
|
|
|
}
|
|
|
|
|
@@ -58,9 +56,11 @@ func (h *Hub) run() {
|
|
|
select {
|
|
|
case client := <-h.Connect:
|
|
|
h.clients[client.UserId] = client
|
|
|
+ h.OnPublishConnect(context.Background(), client)
|
|
|
case client := <-h.Disconnect:
|
|
|
close(client.Send)
|
|
|
delete(h.clients, client.UserId)
|
|
|
+ h.OnPublishDisconnect(context.Background(), client)
|
|
|
case message := <-h.Message:
|
|
|
if client, ok := h.clients[message.receiver]; ok {
|
|
|
if client.isRemotely {
|
|
@@ -115,3 +115,11 @@ func (h *Hub) remotelyEvent() {
|
|
|
fmt.Println(rMsg)
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+func (h *Hub) OnPublishConnect(ctx context.Context, client *Client) error {
|
|
|
+ return h.rdb.Publish(ctx, connectChannelEvent, client.UserId).Err()
|
|
|
+}
|
|
|
+
|
|
|
+func (h *Hub) OnPublishDisconnect(ctx context.Context, client *Client) error {
|
|
|
+ return h.rdb.Publish(ctx, disconnectChannelEvent, client.UserId).Err()
|
|
|
+}
|