mirror of
				https://github.com/yangjian102621/geekai.git
				synced 2025-11-04 16:23:42 +08:00 
			
		
		
		
	opt: add heartbeat message for websocket connects
This commit is contained in:
		@@ -9,6 +9,8 @@
 | 
				
			|||||||
* 功能新增:新增阿里通义千问大模型支持
 | 
					* 功能新增:新增阿里通义千问大模型支持
 | 
				
			||||||
* Bug修复:修复 MJ 放大任务失败时候 img_call 会增加的 Bug
 | 
					* Bug修复:修复 MJ 放大任务失败时候 img_call 会增加的 Bug
 | 
				
			||||||
* 功能优化:新增虎皮椒和PayJS订单状态校验功能,增加安全性
 | 
					* 功能优化:新增虎皮椒和PayJS订单状态校验功能,增加安全性
 | 
				
			||||||
 | 
					* Bug修复:修复微信转账交易 ID 提取失败 Bug
 | 
				
			||||||
 | 
					* 功能优化:给所有的 websocket 连接加上心跳,解决 "close 1006 (abnormal closure): unexpected EOF" Bug
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
## v3.2.5
 | 
					## v3.2.5
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -134,7 +134,6 @@ func (h *ChatHandler) ChatHandle(c *gin.Context) {
 | 
				
			|||||||
		for {
 | 
							for {
 | 
				
			||||||
			_, msg, err := client.Receive()
 | 
								_, msg, err := client.Receive()
 | 
				
			||||||
			if err != nil {
 | 
								if err != nil {
 | 
				
			||||||
				logger.Error(err)
 | 
					 | 
				
			||||||
				client.Close()
 | 
									client.Close()
 | 
				
			||||||
				h.App.ChatClients.Delete(sessionId)
 | 
									h.App.ChatClients.Delete(sessionId)
 | 
				
			||||||
				cancelFunc := h.App.ReqCancelFunc.Get(sessionId)
 | 
									cancelFunc := h.App.ReqCancelFunc.Get(sessionId)
 | 
				
			||||||
@@ -145,19 +144,30 @@ func (h *ChatHandler) ChatHandle(c *gin.Context) {
 | 
				
			|||||||
				return
 | 
									return
 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
			message := string(msg)
 | 
								var message types.WsMessage
 | 
				
			||||||
			logger.Info("Receive a message: ", message)
 | 
								err = utils.JsonDecode(string(msg), &message)
 | 
				
			||||||
			//utils.ReplyMessage(client, "这是一条测试消息!")
 | 
								if err != nil {
 | 
				
			||||||
 | 
									continue
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
								// 心跳消息
 | 
				
			||||||
 | 
								if message.Type == "heartbeat" {
 | 
				
			||||||
 | 
									logger.Debug("收到 Chat 心跳消息:", message.Content)
 | 
				
			||||||
 | 
									continue
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
								logger.Info("Receive a message: ", message.Content)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
			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, message, client)
 | 
								err = h.sendMessage(ctx, session, chatRole, utils.InterfaceToString(message.Content), client)
 | 
				
			||||||
			if err != nil {
 | 
								if err != nil {
 | 
				
			||||||
				logger.Error(err)
 | 
									logger.Error(err)
 | 
				
			||||||
				utils.ReplyChunkMessage(client, types.WsMessage{Type: types.WsEnd})
 | 
									utils.ReplyChunkMessage(client, types.WsMessage{Type: types.WsEnd})
 | 
				
			||||||
			} else {
 | 
								} else {
 | 
				
			||||||
				utils.ReplyChunkMessage(client, types.WsMessage{Type: types.WsEnd})
 | 
									utils.ReplyChunkMessage(client, types.WsMessage{Type: types.WsEnd})
 | 
				
			||||||
				logger.Info("回答完毕: " + string(message))
 | 
									logger.Infof("回答完毕: %v", message.Content)
 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -543,6 +543,7 @@ const lineBuffer = ref(''); // 输出缓冲行
 | 
				
			|||||||
const socket = ref(null);
 | 
					const socket = ref(null);
 | 
				
			||||||
const activelyClose = ref(false); // 主动关闭
 | 
					const activelyClose = ref(false); // 主动关闭
 | 
				
			||||||
const canSend = ref(true);
 | 
					const canSend = ref(true);
 | 
				
			||||||
 | 
					const heartbeatHandle = ref(null)
 | 
				
			||||||
const connect = function (chat_id, role_id) {
 | 
					const connect = function (chat_id, role_id) {
 | 
				
			||||||
  let isNewChat = false;
 | 
					  let isNewChat = false;
 | 
				
			||||||
  if (!chat_id) {
 | 
					  if (!chat_id) {
 | 
				
			||||||
@@ -589,6 +590,14 @@ const connect = function (chat_id, role_id) {
 | 
				
			|||||||
      loadChatHistory(chat_id);
 | 
					      loadChatHistory(chat_id);
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    // 发送心跳消息
 | 
				
			||||||
 | 
					    clearInterval(heartbeatHandle.value)
 | 
				
			||||||
 | 
					    heartbeatHandle.value = setInterval(() => {
 | 
				
			||||||
 | 
					      if (socket.value !== null) {
 | 
				
			||||||
 | 
					        socket.value.send(JSON.stringify({type: "heartbeat", content: "ping"}))
 | 
				
			||||||
 | 
					      }
 | 
				
			||||||
 | 
					    }, 5000);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  });
 | 
					  });
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  _socket.addEventListener('message', event => {
 | 
					  _socket.addEventListener('message', event => {
 | 
				
			||||||
@@ -718,7 +727,7 @@ const sendMessage = function () {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
  showHello.value = false
 | 
					  showHello.value = false
 | 
				
			||||||
  disableInput(false)
 | 
					  disableInput(false)
 | 
				
			||||||
  socket.value.send(prompt.value);
 | 
					  socket.value.send(JSON.stringify({type: "chat", content: prompt.value}));
 | 
				
			||||||
  previousText.value = prompt.value;
 | 
					  previousText.value = prompt.value;
 | 
				
			||||||
  prompt.value = '';
 | 
					  prompt.value = '';
 | 
				
			||||||
  return true;
 | 
					  return true;
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -200,6 +200,7 @@ const lineBuffer = ref(''); // 输出缓冲行
 | 
				
			|||||||
const socket = ref(null);
 | 
					const socket = ref(null);
 | 
				
			||||||
const activelyClose = ref(false); // 主动关闭
 | 
					const activelyClose = ref(false); // 主动关闭
 | 
				
			||||||
const canSend = ref(true);
 | 
					const canSend = ref(true);
 | 
				
			||||||
 | 
					const heartbeatHandle = ref(null)
 | 
				
			||||||
const connect = function (chat_id, role_id) {
 | 
					const connect = function (chat_id, role_id) {
 | 
				
			||||||
  let isNewChat = false;
 | 
					  let isNewChat = false;
 | 
				
			||||||
  if (!chat_id) {
 | 
					  if (!chat_id) {
 | 
				
			||||||
@@ -238,6 +239,14 @@ const connect = function (chat_id, role_id) {
 | 
				
			|||||||
        orgContent: role.helloMsg,
 | 
					        orgContent: role.helloMsg,
 | 
				
			||||||
      })
 | 
					      })
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    // 发送心跳消息
 | 
				
			||||||
 | 
					    clearInterval(heartbeatHandle.value)
 | 
				
			||||||
 | 
					    heartbeatHandle.value = setInterval(() => {
 | 
				
			||||||
 | 
					      if (socket.value !== null) {
 | 
				
			||||||
 | 
					        socket.value.send(JSON.stringify({type: "heartbeat", content: "ping"}))
 | 
				
			||||||
 | 
					      }
 | 
				
			||||||
 | 
					    }, 5000);
 | 
				
			||||||
  });
 | 
					  });
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  _socket.addEventListener('message', event => {
 | 
					  _socket.addEventListener('message', event => {
 | 
				
			||||||
@@ -370,7 +379,7 @@ const sendMessage = () => {
 | 
				
			|||||||
  })
 | 
					  })
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  disableInput(false)
 | 
					  disableInput(false)
 | 
				
			||||||
  socket.value.send(prompt.value);
 | 
					  socket.value.send(JSON.stringify({type: "chat", content: prompt.value}));
 | 
				
			||||||
  previousText.value = prompt.value;
 | 
					  previousText.value = prompt.value;
 | 
				
			||||||
  prompt.value = '';
 | 
					  prompt.value = '';
 | 
				
			||||||
  return true;
 | 
					  return true;
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user