Skip to content

Commit

Permalink
feat(chat):添加重新连接功能
Browse files Browse the repository at this point in the history
  • Loading branch information
li1553770945 committed Apr 24, 2023
1 parent a26eba1 commit e5a6036
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
22 changes: 18 additions & 4 deletions biz/internal/service/chat/chat.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func (s *ChatService) JoinChat(ctx context.Context, c *app.RequestContext) {
},
}
ChatID := c.DefaultQuery("chat_id", "")

clientID := c.DefaultQuery("client_id", "")
chatInterface, found := s.Cache.Get(ChatID)
if !found {
c.JSON(200, utils.H{"code": 4004, "msg": "未找到相关chat"})
Expand All @@ -67,10 +67,24 @@ func (s *ChatService) JoinChat(ctx context.Context, c *app.RequestContext) {
c.JSON(200, utils.H{"code": 5001, "msg": "内部错误,chatInterface断言失败"})
return
}
var role string
if clientID != "" {
if clientID == chatEntity.CreaterID {
role = "creater"
} else if clientID == chatEntity.JoinerID {
role = "joiner"
} else {
c.JSON(200, utils.H{"code": 5001, "msg": "客户端ID错误"})
s.Log.Error("client id not found")
return
}
} else {
chatEntity.JoinerID = U.RandSeq(10)
role = "joiner"
}

chatEntity.JoinerID = U.RandSeq(10)
err := upgrader.Upgrade(c, func(conn *websocket.Conn) {
s.MessageHandler(conn, chatEntity, "joiner")
s.MessageHandler(conn, chatEntity, role)
})
if err != nil {
c.JSON(200, utils.H{"code": 5001, "msg": err.Error()})
Expand Down Expand Up @@ -148,7 +162,7 @@ func (s *ChatService) MessageHandler(recvConn *websocket.Conn, chatEntity *domai
}
} else {
sendMsgEntity.Data = ""
sendMsgEntity.Type = "error"
sendMsgEntity.Type = "warning"
sendMsgEntity.Time = time.Now()
sendMsgEntity.ErrorMsg = "对方未上线,发送失败"
jsonBytes, _ := json.Marshal(sendMsgEntity)
Expand Down
File renamed without changes.

0 comments on commit e5a6036

Please sign in to comment.