feat(internal): 实现网关服务并优化聊天功能

- 新增 Gateway 结构体和相关方法,用于管理客户端连接和消息分发
- 重构 ChatService,集成 Gateway 功能
- 添加客户端绑定 UID 功能,支持消息发送到指定用户
- 实现全局消息广播和单用户消息发送的 HTTP 接口
- 优化聊天消息处理逻辑,增加消息回显功能
This commit is contained in:
wuchao 2025-09-19 11:56:39 +08:00
parent 8a2411016c
commit addcf2d24d
1 changed files with 2 additions and 9 deletions

View File

@ -81,17 +81,10 @@ func (h *ChatService) Chat(c *websocket.Conn) {
break break
} }
//简单协议bind:<uid> //简单协议bind:<uid>
if len(message) > 5 && string(message[:5]) == "bind:" { if c.Headers("Sec-Websocket-Protocol") == "bind" && c.Headers("X-Session") != "" {
uid := string(message[5:]) uid := c.Headers("X-Session")
_ = h.Gw.BindUid(clientID, uid) _ = h.Gw.BindUid(clientID, uid)
log.Printf("bind %s -> uid:%s\n", clientID, uid) log.Printf("bind %s -> uid:%s\n", clientID, uid)
continue
}
// 回显
err = h.Gw.SendToClient(clientID, []byte("echo: "+string(message)))
if err != nil {
h.ChatFail(c, "send to client failed")
continue
} }
msg, chatType := h.handleMessageToString(c, messageType, message) msg, chatType := h.handleMessageToString(c, messageType, message)
if chatType == constant.ConnStatusClosed { if chatType == constant.ConnStatusClosed {