From c4873f26691984dbca7472644d8ac2ed86cd0765 Mon Sep 17 00:00:00 2001 From: RA Date: Fri, 31 Mar 2023 22:05:47 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E5=90=8E=E7=AB=AF=E6=8A=A5?= =?UTF-8?q?=E9=94=99=E5=90=8E=E5=89=8D=E5=8F=B0http=E9=A1=B5=E9=9D=A2?= =?UTF-8?q?=E6=B2=A1=E6=9C=89=E5=85=B3=E9=97=ADwss=E8=BF=9E=E6=8E=A5?= =?UTF-8?q?=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- channel/http/static/1.js | 33 +++++++++++++++++++++++++++------ 1 file changed, 27 insertions(+), 6 deletions(-) diff --git a/channel/http/static/1.js b/channel/http/static/1.js index 7466104..241ff2e 100644 --- a/channel/http/static/1.js +++ b/channel/http/static/1.js @@ -1,5 +1,5 @@ -function generateUUID () { +function generateUUID() { return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) { var r = Math.random() * 16 | 0, v = c == 'x' ? r : (r & 0x3 | 0x8); @@ -11,7 +11,7 @@ const conversationType = { DISPOSABLE: 1, STREAM: 1 << 1 } -function ConvState (wrapper, form, params) { +function ConvState(wrapper, form, params) { this.id = generateUUID() this.form = form; this.wrapper = wrapper; @@ -31,13 +31,37 @@ ConvState.prototype.printAnswer = function (uuid, answer = '我是ChatGPT, 一 $(this.wrapper).find(this.parameters.inputIdHashTagName).focus(); }.bind(this), 500); }; + ConvState.prototype.updateAnswer = function (question, uuid) { setTimeout(function () { var socket = io('/chat'); socket.connect('/chat'); + let timerId; + var _this = this + // 设置计时器,如果在规定的时间内没有接收到消息,则手动断开连接 + function setTimer() { + timerId = setTimeout(() => { + if (socket.connected) { + socket.disconnect(); + handle_disconnect(); + } + }, 60000); + } + function resetTimer() { + clearTimeout(timerId); + setTimer(); + } + setTimer(); var messageObj = $(this.wrapper).find(`#${uuid}`); + function handle_disconnect() { + messageObj.removeClass('typing').addClass('ready'); + _this.scrollDown(); + $(_this.wrapper).find(_this.parameters.inputIdHashTagName).focus(); + } this.scrollDown(); socket.on('message', msg => { + // 接收到消息时重置计时器 + resetTimer(); if (msg.result) messageObj.html(msg.result + `
`); this.scrollDown(); @@ -50,10 +74,7 @@ ConvState.prototype.updateAnswer = function (question, uuid) { answer = marked.parse(msg.result); messageObj.html(answer); } - messageObj.removeClass('typing').addClass('ready'); - this.scrollDown(); - $(this.wrapper).find(this.parameters.inputIdHashTagName).focus(); - console.log("disconnect", msg) + handle_disconnect() }); }.bind(this), 1000); };