feat(internal): 实现网关服务并优化聊天功能
- 新增 Gateway 结构体和相关方法,用于管理客户端连接和消息分发 - 重构 ChatService,集成 Gateway 功能 - 添加客户端绑定 UID 功能,支持消息发送到指定用户 - 实现全局消息广播和单用户消息发送的 HTTP 接口 - 优化聊天消息处理逻辑,增加消息回显功能
This commit is contained in:
parent
8a2411016c
commit
addcf2d24d
|
@ -81,17 +81,10 @@ func (h *ChatService) Chat(c *websocket.Conn) {
|
|||
break
|
||||
}
|
||||
//简单协议:bind:<uid>
|
||||
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 {
|
||||
|
|
Loading…
Reference in New Issue