From addcf2d24d4ce60fa254c9f458f8cb56bccfe86d Mon Sep 17 00:00:00 2001 From: wuchao <1272174216@qq.com> Date: Fri, 19 Sep 2025 11:56:39 +0800 Subject: [PATCH] =?UTF-8?q?feat(internal):=20=E5=AE=9E=E7=8E=B0=E7=BD=91?= =?UTF-8?q?=E5=85=B3=E6=9C=8D=E5=8A=A1=E5=B9=B6=E4=BC=98=E5=8C=96=E8=81=8A?= =?UTF-8?q?=E5=A4=A9=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 新增 Gateway 结构体和相关方法,用于管理客户端连接和消息分发 - 重构 ChatService,集成 Gateway 功能 - 添加客户端绑定 UID 功能,支持消息发送到指定用户 - 实现全局消息广播和单用户消息发送的 HTTP 接口 - 优化聊天消息处理逻辑,增加消息回显功能 --- internal/services/chat.go | 11 ++--------- 1 file changed, 2 insertions(+), 9 deletions(-) diff --git a/internal/services/chat.go b/internal/services/chat.go index d44d032..43804b8 100644 --- a/internal/services/chat.go +++ b/internal/services/chat.go @@ -81,17 +81,10 @@ func (h *ChatService) Chat(c *websocket.Conn) { break } //简单协议:bind: - if len(message) > 5 && string(message[:5]) == "bind:" { - uid := string(message[5:]) + if c.Headers("Sec-Websocket-Protocol") == "bind" && c.Headers("X-Session") != "" { + uid := c.Headers("X-Session") _ = h.Gw.BindUid(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) if chatType == constant.ConnStatusClosed {