opt: unset timeout for websocket connection

This commit is contained in:
RockYang 2023-08-15 18:29:53 +08:00
parent e65c32c4c7
commit 9d9ee7c585
2 changed files with 8 additions and 5 deletions

View File

@ -50,8 +50,8 @@ func (h *ChatHandler) ChatHandle(c *gin.Context) {
return return
} }
// 设置读写超时时间 // 设置读写超时时间
_ = ws.SetWriteDeadline(time.Now().Add(300 * time.Second)) //_ = ws.SetWriteDeadline(time.Now().Add(300 * time.Second))
_ = ws.SetReadDeadline(time.Now().Add(300 * time.Second)) //_ = ws.SetReadDeadline(time.Now().Add(300 * time.Second))
sessionId := c.Query("session_id") sessionId := c.Query("session_id")
roleId := h.GetInt(c, "role_id", 0) roleId := h.GetInt(c, "role_id", 0)
@ -109,7 +109,7 @@ func (h *ChatHandler) ChatHandle(c *gin.Context) {
h.App.ChatClients.Put(sessionId, client) h.App.ChatClients.Put(sessionId, client)
go func() { go func() {
for { for {
_, message, err := client.Receive() _, msg, err := client.Receive()
if err != nil { if err != nil {
logger.Error(err) logger.Error(err)
client.Close() client.Close()
@ -117,12 +117,14 @@ func (h *ChatHandler) ChatHandle(c *gin.Context) {
h.App.ReqCancelFunc.Delete(sessionId) h.App.ReqCancelFunc.Delete(sessionId)
return return
} }
logger.Info("Receive a message: ", string(message))
message := string(msg)
logger.Info("Receive a message: ", message)
//utils.ReplyMessage(client, "这是一条测试消息!") //utils.ReplyMessage(client, "这是一条测试消息!")
ctx, cancel := context.WithCancel(context.Background()) ctx, cancel := context.WithCancel(context.Background())
h.App.ReqCancelFunc.Put(sessionId, cancel) h.App.ReqCancelFunc.Put(sessionId, cancel)
// 回复消息 // 回复消息
err = h.sendMessage(ctx, session, chatRole, string(message), client) err = h.sendMessage(ctx, session, chatRole, message, client)
if err != nil { if err != nil {
logger.Error(err) logger.Error(err)
} else { } else {

View File

@ -534,6 +534,7 @@ const connect = function (chat_id, role_id) {
} else { // } else { //
loadChatHistory(chat_id); loadChatHistory(chat_id);
} }
}); });
_socket.addEventListener('message', event => { _socket.addEventListener('message', event => {