|
@@ -15,6 +15,8 @@ type HubConfig struct {
|
|
|
DisconnectSize int
|
|
|
MessageSize int
|
|
|
ServerId string // serverId 服务器ID
|
|
|
+
|
|
|
+ Rdb redis.UniversalClient
|
|
|
}
|
|
|
|
|
|
type Hub struct {
|
|
@@ -31,11 +33,7 @@ type Hub struct {
|
|
|
func NewHub(cfg HubConfig) *Hub {
|
|
|
hub := &Hub{
|
|
|
serverId: cfg.ServerId,
|
|
|
- rdb: redis.NewUniversalClient(&redis.UniversalOptions{
|
|
|
- Addrs: []string{"106.75.230.4:6379"},
|
|
|
- Password: "sikey!Q@W#E456",
|
|
|
- DB: 0,
|
|
|
- }),
|
|
|
+ rdb: cfg.Rdb,
|
|
|
|
|
|
clients: make(map[string]*Client),
|
|
|
mutex: sync.RWMutex{},
|
|
@@ -46,29 +44,25 @@ func NewHub(cfg HubConfig) *Hub {
|
|
|
}
|
|
|
|
|
|
go hub.run()
|
|
|
-
|
|
|
go hub.remotelyEvent() // 远程事件
|
|
|
return hub
|
|
|
}
|
|
|
|
|
|
func (h *Hub) run() {
|
|
|
for {
|
|
|
+ ctx := context.Background()
|
|
|
select {
|
|
|
case client := <-h.Connect:
|
|
|
h.clients[client.UserId] = client
|
|
|
- h.OnPublishConnect(context.Background(), client)
|
|
|
+ h.OnPublishConnect(ctx, client)
|
|
|
case client := <-h.Disconnect:
|
|
|
close(client.Send)
|
|
|
delete(h.clients, client.UserId)
|
|
|
- h.OnPublishDisconnect(context.Background(), client)
|
|
|
+ h.OnPublishDisconnect(ctx, client)
|
|
|
case message := <-h.Message:
|
|
|
if client, ok := h.clients[message.receiver]; ok {
|
|
|
if client.isRemotely {
|
|
|
- ctx := context.Background()
|
|
|
- err := h.rdb.Publish(ctx, messageChannelEvent, message).Err()
|
|
|
- if err != nil {
|
|
|
- zlog.Error(err)
|
|
|
- }
|
|
|
+ h.OnPublishMessage(ctx, message)
|
|
|
} else {
|
|
|
client.Send <- message
|
|
|
}
|
|
@@ -96,36 +90,64 @@ func (h *Hub) remotelyEvent() {
|
|
|
|
|
|
switch rMsg.Channel {
|
|
|
case connectChannelEvent:
|
|
|
- h.Connect <- &Client{
|
|
|
- hub: h,
|
|
|
- UserId: rMsg.Payload,
|
|
|
- isRemotely: true,
|
|
|
- Send: make(chan *Message),
|
|
|
+ var event = deserializeEvent([]byte(rMsg.Payload))
|
|
|
+ if event.ServerId != h.serverId {
|
|
|
+ h.Connect <- &Client{
|
|
|
+ hub: h,
|
|
|
+ UserId: event.UserId,
|
|
|
+ isRemotely: true,
|
|
|
+ Send: make(chan *Message),
|
|
|
+ }
|
|
|
}
|
|
|
case disconnectChannelEvent:
|
|
|
- h.Disconnect <- h.clients[rMsg.Payload]
|
|
|
- case messageChannelEvent:
|
|
|
- var message Message
|
|
|
- if err = json.Unmarshal([]byte(rMsg.Payload), &message); err != nil {
|
|
|
- zlog.Error(err)
|
|
|
- break
|
|
|
+ var event = deserializeEvent([]byte(rMsg.Payload))
|
|
|
+ if event.ServerId != h.serverId {
|
|
|
+ h.Disconnect <- h.clients[event.UserId]
|
|
|
}
|
|
|
- h.Message <- &message
|
|
|
+ case messageChannelEvent:
|
|
|
+ h.Message <- deserializeMessage([]byte(rMsg.Payload))
|
|
|
}
|
|
|
fmt.Println(rMsg)
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+func deserializeEvent(bytes []byte) *PublishEvent {
|
|
|
+ var event PublishEvent
|
|
|
+ _ = json.Unmarshal(bytes, &event)
|
|
|
+ return &event
|
|
|
+}
|
|
|
+
|
|
|
+type PublishEvent struct {
|
|
|
+ ServerId string
|
|
|
+ UserId string
|
|
|
+}
|
|
|
+
|
|
|
+type PublishMessage struct {
|
|
|
+ ServerId string
|
|
|
+ message *Message
|
|
|
+}
|
|
|
+
|
|
|
func (h *Hub) OnPublishConnect(ctx context.Context, client *Client) error {
|
|
|
if !client.isRemotely {
|
|
|
- return h.rdb.Publish(ctx, connectChannelEvent, client.UserId).Err()
|
|
|
+ event := &PublishEvent{UserId: client.UserId, ServerId: h.serverId}
|
|
|
+ return h.rdb.Publish(ctx, connectChannelEvent, event).Err()
|
|
|
}
|
|
|
return nil
|
|
|
}
|
|
|
|
|
|
func (h *Hub) OnPublishDisconnect(ctx context.Context, client *Client) error {
|
|
|
if !client.isRemotely {
|
|
|
- return h.rdb.Publish(ctx, disconnectChannelEvent, client.UserId).Err()
|
|
|
+ event := &PublishEvent{UserId: client.UserId, ServerId: h.serverId}
|
|
|
+ return h.rdb.Publish(ctx, disconnectChannelEvent, event).Err()
|
|
|
+ }
|
|
|
+ return nil
|
|
|
+}
|
|
|
+
|
|
|
+func (h *Hub) OnPublishMessage(ctx context.Context, message *Message) error {
|
|
|
+ err := h.rdb.Publish(ctx, messageChannelEvent, message).Err()
|
|
|
+ if err != nil {
|
|
|
+ zlog.Error(err)
|
|
|
+ return err
|
|
|
}
|
|
|
return nil
|
|
|
}
|