feat(ws): implement WebSocket cluster mode with Redis Pub/Sub
Introduce a new WebSocket messaging architecture that supports both standalone and cluster modes. This allows for horizontal scaling of WebSocket servers by using Redis Pub/Sub to synchronize messages across multiple instances. Key changes: - Added `ws.MessagePublisher` interface to abstract message distribution. - Implemented `ws.Bus` to handle cluster-mode messaging via Redis. - Added `ws.OnlineTracker` to manage user online status across the cluster. - Refactored multiple services (Chat, Group, Push, Call, etc.) to use the new `MessagePublisher` instead of a concrete `ws.Hub`. - Added WebSocket configuration options (mode, instance ID, channel, TTL, heartbeat) to `config.yaml` and `config.go`. - Updated dependency injection with Wire to support the new publisher and Redis client. - Improved logging by replacing standard `log` with `zap` in several service components.
This commit is contained in:
23
internal/pkg/ws/publisher.go
Normal file
23
internal/pkg/ws/publisher.go
Normal file
@@ -0,0 +1,23 @@
|
||||
package ws
|
||||
|
||||
// MessagePublisher 定义消息发布接口
|
||||
// ws.Hub 隐式满足此接口,ws.Bus 组合 Hub 并扩展跨实例能力
|
||||
type MessagePublisher interface {
|
||||
// 消息发布
|
||||
PublishToUser(userID string, eventType string, payload any) Event
|
||||
PublishToUsers(userIDs []string, eventType string, payload any)
|
||||
PublishToUsersConcurrent(userIDs []string, eventType string, payload any)
|
||||
PublishToUserOnline(userID string, eventType string, payload any) bool
|
||||
PublishToUserOnlineReliable(userID string, eventType string, payload any) bool
|
||||
Broadcast(eventType string, payload any)
|
||||
|
||||
// 在线状态
|
||||
HasClients(userID string) bool
|
||||
FilterOnline(userIDs []string) (online, offline []string)
|
||||
|
||||
// 本地操作(不经过 Redis)
|
||||
SendError(client *Client, code string, message string)
|
||||
NextID() uint64
|
||||
OnDisconnect(handler DisconnectHandler)
|
||||
OnConnect(handler ConnectHandler)
|
||||
}
|
||||
Reference in New Issue
Block a user