登录 立即注册

首页 > 绿虎论坛 > 建站 > 讨论/求助 (发帖)

【已实现】继续发布任务,谁有兴趣帮虎绿林写个内信和@消息的WebSocket推送服务


『回复列表(56|隐藏机器人聊天)』

40.

@hu60bot,写一个运行在浏览器里的js函数,连接 wss://hu60.cn/ws/msg 并打印收到的消息到控制台

(/@Ta/2023-03-19 02:13//)

41.

[新会话] @老虎会游泳

下面是连接wss://hu60.cn/ws/msg并打印收到的消息到控制台的JavaScript函数:

function connectToWebSocket() {
  const socket = new WebSocket('wss://hu60.cn/ws/msg');
  
  socket.onopen = function(event) {
    console.log('WebSocket连接已打开');
  }

  socket.onmessage = function(event) {
    console.log('收到消息:', event.data);
  }

  socket.onerror = function(error) {
    console.error('WebSocket连接发生错误', error);
  }

  socket.onclose = function(event) {
    console.log('WebSocket连接已关闭');
  }
}

你可以在你的JavaScript代码中调用这个函数,以连接到WebSocket并开始接收消息。

(/@Ta/2023-03-19 02:13//)

42.

@hu60bot,每隔1分钟发送一次keep alive消息

(/@Ta/2023-03-19 02:51//)

43.

[新会话] @老虎会游泳

作为AI语言模型,我没有真实的网络连接,因此无法发送任何消息。但就一般原理而言,keep alive是一种用于保持网络连接的消息,通常是在TCP协议中使用的。它的作用是避免不活动的连接被防火墙或路由器关闭,并确保连接的稳定性。在一些应用程序中,可以设置发送keep alive消息的时间间隔,以确保连接保持活动状态。通常的时间间隔是每隔1分钟或更短的时间发送一次keep alive消息。

(/@Ta/2023-03-19 02:51//)

44.

@hu60bot,修改上面的javascript函数,每隔1分钟发送一次keep alive消息

(/@Ta/2023-03-19 02:52//)

45.

@老虎会游泳,假设你有一个名为 sendKeepAlive() 的 JavaScript 函数,你可以使用 setInterval() 函数来在每隔一定时间间隔后多次调用它。

// 定义发送 keep alive 消息的函数
function sendKeepAlive() {
    // 发送 keep alive 消息的代码
    console.log("Sending keep alive message");
}

// 每隔一分钟调用一次 sendKeepAlive 函数
setInterval(sendKeepAlive, 60000);

在这个示例中,sendKeepAlive() 函数会每隔一分钟被调用一次,setInterval() 函数会把 sendKeepAlive() 函数设置为回调函数,并指定间隔时间为 60 秒(milliseconds)。每隔一个时间间隔后,就会调用 sendKeepAlive() 函数,并发送 keep alive 消息。当需要停止发送 keep alive 消息时,你可以使用 clearInterval() 函数来清除间隔时间。

(/@Ta/2023-03-19 02:52//)

46.

@hu60bot,写一个运行在浏览器里的js函数,连接 wss://hu60.cn/ws/msg 并打印收到的消息到控制台,每隔1分钟发送一次keep alive消息,并在出错时自动重连

(/@Ta/2023-03-19 02:52//)

47.

@老虎会游泳,以下是一个示例 JavaScript 函数,它可以连接 wss://hu60.cn/ws/msg,接收并打印消息,每隔1分钟发送一次 keep alive 消息,并在出现错误或连接中断时自动重新连接。请注意,由于我们无法使用真实的 Websocket 连接进行测试,以下代码仅提供示例。

function connectToWebSocket() {
  const socket = new WebSocket("wss://hu60.cn/ws/msg");

  // 开启 WebSocket 连接时触发
  socket.onopen = (event) => {
    console.log("WebSocket 连接已经建立");
    // 每隔一分钟发送一个 keep alive 消息
    setInterval(() => {
      socket.send("keep alive");
    }, 60000);
  }

  // 接收到 WebSocket 消息时触发
  socket.onmessage = (event) => {
    console.log("收到 WebSocket 消息", event.data);
  };

  // 当 WebSocket 连接出错时触发
  socket.onerror = (event) => {
    console.error("WebSocket 连接出错", event);
    // 关闭当前 WebSocket 连接
    socket.close();

    // 重新连接 WebSocket
    setTimeout(() => {
      console.log("重新连接 WebSocket");
      connectToWebSocket();
    }, 5000); // 延迟 5 秒重新连接
  };

  // 当 WebSocket 连接关闭时触发
  socket.onclose = (event) => {
    console.log("WebSocket 连接已关闭", event);
    // 重新连接 WebSocket
    setTimeout(() => {
      console.log("重新连接 WebSocket");
      connectToWebSocket();
    }, 5000); // 延迟 5 秒重新连接
  };
}

// 连接 WebSocket
connectToWebSocket();

在此示例中,我们首先创建并建立 WebSocket 连接。在连接成功后,我们设置每隔1分钟发送一次 keep alive 消息。接着,我们监听 WebSocket 的事件,包括接收消息、连接错误、连接关闭等。当出现错误或连接中断时,我们重新连接 WebSocket,并进行延迟处理,以确保稳定性。

(/@Ta/2023-03-19 02:53//)

48.

@ChatGPT,你好

(/@Ta/2023-03-19 14:39//)

49.

[新会话] @罐子,你好!有什么我可以帮助你的吗?

(/@Ta/2023-03-19 14:39//)

50.

@rkonfj,程序有时候会崩溃

Mar 19 19:26:07 hu60-kunpeng run.sh[2356097]: panic: runtime error: invalid memory address or nil pointer dereference
Mar 19 19:26:07 hu60-kunpeng run.sh[2356097]: [signal SIGSEGV: segmentation violation code=0x1 addr=0x0 pc=0x3e5ed8]
Mar 19 19:26:07 hu60-kunpeng run.sh[2356097]: goroutine 53 [running]:
Mar 19 19:26:07 hu60-kunpeng run.sh[2356097]: github.com/gorilla/websocket.(*Conn).Close(...)
Mar 19 19:26:07 hu60-kunpeng run.sh[2356097]:         /root/go/pkg/mod/github.com/gorilla/websocket@v1.5.0/conn.go:345
Mar 19 19:26:07 hu60-kunpeng run.sh[2356097]: github.com/rkonfj/hu60bot/server.(*WebsocketManager).Run.func1.1({0x4595, {0x40004a4bf6, 0x6}, {0x0, 0x0}, 0x5211e6dc, 0x1, 0x0}, 0x0?)
Mar 19 19:26:07 hu60-kunpeng run.sh[2356097]:         /root/chatgpt/hu60bot/server/server.go:181 +0x198
Mar 19 19:26:07 hu60-kunpeng run.sh[2356097]: created by github.com/rkonfj/hu60bot/server.(*WebsocketManager).Run.func1
Mar 19 19:26:07 hu60-kunpeng run.sh[2356097]:         /root/chatgpt/hu60bot/server/server.go:177 +0x460
Mar 19 19:26:12 hu60-kunpeng run.sh[2356097]: Sun Mar 19 19:26:12 CST 2023
Mar 19 19:26:12 hu60-kunpeng run.sh[2356097]: time="2023-03-19T19:26:12+08:00" level=debug msg="conversation: 0/0, openai tokens usage: 0/0/0"
Mar 19 19:26:12 hu60-kunpeng run.sh[2356097]: time="2023-03-19T19:26:12+08:00" level=info msg="bot listening on 127.0.0.1:4860 for interact now. websocket endpoint is /v1/ws"
Mar 19 19:26:12 hu60-kunpeng run.sh[2356097]: time="2023-03-19T19:26:12+08:00" level=info msg="bot watching db event now"
Mar 19 19:26:12 hu60-kunpeng run.sh[2356097]: time="2023-03-19T19:26:12+08:00" level=info msg="bot watching for chat now. sid is xxx, conversation window is 30m0s"
Mar 19 19:26:12 hu60-kunpeng run.sh[2356097]: time="2023-03-19T19:26:12+08:00" level=info msg="user ChatGPT is connected"
Mar 19 19:26:12 hu60-kunpeng run.sh[2356097]: time="2023-03-19T19:26:12+08:00" level=info msg="user 旧人 is connected"
Mar 19 19:26:13 hu60-kunpeng run.sh[2356097]: time="2023-03-19T19:26:13+08:00" level=info msg="user 旧人 is connected"
Mar 19 19:26:13 hu60-kunpeng run.sh[2356097]: time="2023-03-19T19:26:13+08:00" level=info msg="user 老虎会游泳 is connected"
Mar 19 19:26:13 hu60-kunpeng run.sh[2356097]: time="2023-03-19T19:26:13+08:00" level=info msg="user rkonfj is connected"
Mar 19 19:26:19 hu60-kunpeng run.sh[2356097]: time="2023-03-19T19:26:19+08:00" level=info msg="user rkonfj is disconnected"
Mar 19 19:26:30 hu60-kunpeng run.sh[2356097]: time="2023-03-19T19:26:30+08:00" level=info msg="user 旧人 is connected"
Mar 19 19:26:30 hu60-kunpeng run.sh[2356097]: time="2023-03-19T19:26:30+08:00" level=info msg="user 旧人 is connected"
Mar 19 19:26:31 hu60-kunpeng run.sh[2356097]: time="2023-03-19T19:26:31+08:00" level=info msg="user 旧人 is connected"
Mar 19 19:26:31 hu60-kunpeng run.sh[2356097]: time="2023-03-19T19:26:31+08:00" level=info msg="user 旧人 is connected"
Mar 19 19:26:31 hu60-kunpeng run.sh[2356097]: time="2023-03-19T19:26:31+08:00" level=info msg="user 旧人 is connected"
Mar 19 19:26:31 hu60-kunpeng run.sh[2356097]: time="2023-03-19T19:26:31+08:00" level=info msg="user 旧人 is connected"
Mar 19 19:26:40 hu60-kunpeng run.sh[2356097]: time="2023-03-19T19:26:40+08:00" level=info msg="user 旧人 is disconnected"
Mar 19 19:26:40 hu60-kunpeng run.sh[2356097]: panic: runtime error: invalid memory address or nil pointer dereference
Mar 19 19:26:40 hu60-kunpeng run.sh[2356097]: [signal SIGSEGV: segmentation violation code=0x1 addr=0x0 pc=0x3e5ed8]
Mar 19 19:26:40 hu60-kunpeng run.sh[2356097]: goroutine 49 [running]:
Mar 19 19:26:40 hu60-kunpeng run.sh[2356097]: github.com/gorilla/websocket.(*Conn).Close(...)
Mar 19 19:26:40 hu60-kunpeng run.sh[2356097]:         /root/go/pkg/mod/github.com/gorilla/websocket@v1.5.0/conn.go:345
Mar 19 19:26:40 hu60-kunpeng run.sh[2356097]: github.com/rkonfj/hu60bot/server.(*WebsocketManager).Run.func1.1({0x4595, {0x40003aadd6, 0x6}, {0x0, 0x0}, 0x5211e6dc, 0x1, 0x0}, 0x0?)
Mar 19 19:26:40 hu60-kunpeng run.sh[2356097]:         /root/chatgpt/hu60bot/server/server.go:181 +0x198
Mar 19 19:26:40 hu60-kunpeng run.sh[2356097]: created by github.com/rkonfj/hu60bot/server.(*WebsocketManager).Run.func1
Mar 19 19:26:40 hu60-kunpeng run.sh[2356097]:         /root/chatgpt/hu60bot/server/server.go:177 +0x460
Mar 19 19:26:45 hu60-kunpeng run.sh[2356097]: Sun Mar 19 19:26:45 CST 2023
Mar 19 19:26:45 hu60-kunpeng run.sh[2356097]: time="2023-03-19T19:26:45+08:00" level=debug msg="conversation: 0/0, openai tokens usage: 0/0/0"
Mar 19 19:26:45 hu60-kunpeng run.sh[2356097]: time="2023-03-19T19:26:45+08:00" level=info msg="bot listening on 127.0.0.1:4860 for interact now. websocket endpoint is /v1/ws"
Mar 19 19:26:45 hu60-kunpeng run.sh[2356097]: time="2023-03-19T19:26:45+08:00" level=info msg="bot watching db event now"
Mar 19 19:26:45 hu60-kunpeng run.sh[2356097]: time="2023-03-19T19:26:45+08:00" level=info msg="bot watching for chat now. sid is xxx, conversation window is 30m0s"
Mar 19 19:26:45 hu60-kunpeng run.sh[2356097]: time="2023-03-19T19:26:45+08:00" level=info msg="user 老虎会游泳 is connected"
Mar 19 19:26:45 hu60-kunpeng run.sh[2356097]: time="2023-03-19T19:26:45+08:00" level=info msg="user 旧人 is connected"
Mar 19 19:26:46 hu60-kunpeng run.sh[2356097]: time="2023-03-19T19:26:46+08:00" level=info msg="user ChatGPT is connected"
Mar 19 19:26:46 hu60-kunpeng run.sh[2356097]: time="2023-03-19T19:26:46+08:00" level=info msg="user 旧人 is connected"
Mar 19 19:27:41 hu60-kunpeng run.sh[2356097]: time="2023-03-19T19:27:41+08:00" level=info msg="user 旧人 is connected"
Mar 19 19:27:41 hu60-kunpeng run.sh[2356097]: time="2023-03-19T19:27:41+08:00" level=info msg="user 旧人 is connected"
Mar 19 19:27:42 hu60-kunpeng run.sh[2356097]: time="2023-03-19T19:27:42+08:00" level=info msg="user 旧人 is connected"
Mar 19 19:27:44 hu60-kunpeng run.sh[2356097]: time="2023-03-19T19:27:44+08:00" level=info msg="user 旧人 is connected"
Mar 19 19:27:45 hu60-kunpeng run.sh[2356097]: time="2023-03-19T19:27:45+08:00" level=info msg="user 旧人 is connected"
Mar 19 19:28:00 hu60-kunpeng run.sh[2356097]: time="2023-03-19T19:28:00+08:00" level=info msg="user 旧人 is disconnected"
Mar 19 19:28:00 hu60-kunpeng run.sh[2356097]: panic: runtime error: invalid memory address or nil pointer dereference
Mar 19 19:28:00 hu60-kunpeng run.sh[2356097]: [signal SIGSEGV: segmentation violation code=0x1 addr=0x0 pc=0x3e5ed8]
Mar 19 19:28:00 hu60-kunpeng run.sh[2356097]: goroutine 39 [running]:
Mar 19 19:28:00 hu60-kunpeng run.sh[2356097]: github.com/gorilla/websocket.(*Conn).Close(...)
Mar 19 19:28:00 hu60-kunpeng run.sh[2356097]:         /root/go/pkg/mod/github.com/gorilla/websocket@v1.5.0/conn.go:345
Mar 19 19:28:00 hu60-kunpeng run.sh[2356097]: github.com/rkonfj/hu60bot/server.(*WebsocketManager).Run.func1.1({0x4595, {0x400032be46, 0x6}, {0x0, 0x0}, 0x5211e6dc, 0x1, 0x0}, 0x0?)
Mar 19 19:28:00 hu60-kunpeng run.sh[2356097]:         /root/chatgpt/hu60bot/server/server.go:181 +0x198
Mar 19 19:28:00 hu60-kunpeng run.sh[2356097]: created by github.com/rkonfj/hu60bot/server.(*WebsocketManager).Run.func1
Mar 19 19:28:00 hu60-kunpeng run.sh[2356097]:         /root/chatgpt/hu60bot/server/server.go:177 +0x460
Mar 19 19:28:05 hu60-kunpeng run.sh[2356097]: Sun Mar 19 19:28:05 CST 2023
Mar 19 19:28:05 hu60-kunpeng run.sh[2356097]: time="2023-03-19T19:28:05+08:00" level=debug msg="conversation: 0/0, openai tokens usage: 0/0/0"
Mar 19 19:28:05 hu60-kunpeng run.sh[2356097]: time="2023-03-19T19:28:05+08:00" level=info msg="bot listening on 127.0.0.1:4860 for interact now. websocket endpoint is /v1/ws"
Mar 19 19:28:05 hu60-kunpeng run.sh[2356097]: time="2023-03-19T19:28:05+08:00" level=info msg="bot watching db event now"
Mar 19 19:28:05 hu60-kunpeng run.sh[2356097]: time="2023-03-19T19:28:05+08:00" level=info msg="bot watching for chat now. sid is xxx, conversation window is 30m0s"
Mar 19 19:28:05 hu60-kunpeng run.sh[2356097]: time="2023-03-19T19:28:05+08:00" level=info msg="user 老虎会游泳 is connected"
Mar 19 19:28:05 hu60-kunpeng run.sh[2356097]: time="2023-03-19T19:28:05+08:00" level=info msg="user ChatGPT is connected"
Mar 19 19:28:05 hu60-kunpeng run.sh[2356097]: time="2023-03-19T19:28:05+08:00" level=info msg="user 旧人 is connected"
Mar 19 19:28:06 hu60-kunpeng run.sh[2356097]: time="2023-03-19T19:28:06+08:00" level=info msg="user 旧人 is connected"
Mar 19 19:28:22 hu60-kunpeng run.sh[2356097]: time="2023-03-19T19:28:22+08:00" level=info msg="user 旧人 is disconnected"
Mar 19 19:28:22 hu60-kunpeng run.sh[2356097]: panic: runtime error: invalid memory address or nil pointer dereference
Mar 19 19:28:22 hu60-kunpeng run.sh[2356097]: [signal SIGSEGV: segmentation violation code=0x1 addr=0x0 pc=0x3e5ed8]
Mar 19 19:28:22 hu60-kunpeng run.sh[2356097]: goroutine 35 [running]:
Mar 19 19:28:22 hu60-kunpeng run.sh[2356097]: github.com/gorilla/websocket.(*Conn).Close(...)
Mar 19 19:28:22 hu60-kunpeng run.sh[2356097]:         /root/go/pkg/mod/github.com/gorilla/websocket@v1.5.0/conn.go:345
Mar 19 19:28:22 hu60-kunpeng run.sh[2356097]: github.com/rkonfj/hu60bot/server.(*WebsocketManager).Run.func1.1({0x4595, {0x4000388916, 0x6}, {0x0, 0x0}, 0x5211e6dc, 0x1, 0x0}, 0x0?)
Mar 19 19:28:22 hu60-kunpeng run.sh[2356097]:         /root/chatgpt/hu60bot/server/server.go:181 +0x198
Mar 19 19:28:22 hu60-kunpeng run.sh[2356097]: created by github.com/rkonfj/hu60bot/server.(*WebsocketManager).Run.func1
Mar 19 19:28:22 hu60-kunpeng run.sh[2356097]:         /root/chatgpt/hu60bot/server/server.go:177 +0x460
(/@Ta/2023-03-19 19:34//)

51.

@旧人,你连上推送服务后它偶尔会崩溃,你做了什么特别的操作吗?

(/@Ta/2023-03-19 19:35//)

52.

@老虎会游泳,我用了两个插件,一个是我那个推送插件,一个是·@rkonfj,的hu60bot插件,似乎也是WebSocket推送,不知道会不会有冲突?

(/@Ta/2023-03-19 19:38//)

53. @老虎会游泳,出错这块代码是读取客户端消息出错时服务器主动关闭连接,怀疑是关闭了已经关闭的连接再次关闭会导致 `nil pointer`,改了下关闭的逻辑。同时,打印相关错误的 debug 日志。https://github.com/rkonfj/hu60bot/blob/54087f5d696a8d1260a13a4b5dd43b2b61831700/server/server.go#L106-L109

@旧人,提到的情况应该也是一个问题。手机和电脑多个设备或者多个插件使用 WebSocket 时,对于同一个用户是否允许建立多个 连接?目前是允许多个用户建立连接,但是只有最新的连接会收到站内的新消息。
(/@Ta/2023-03-19 20:46//)

54.

@rkonfj,我倾向于改为让所有连接同时收到新消息。

(/@Ta/2023-03-19 21:10//)

55.

@老虎会游泳,嗯,目前的逻辑上不够严谨。我改下

(/@Ta/2023-03-19 21:12//)

56.

hu60bot.jpg(159.45 KB)

@老虎会游泳,更新了几个BUG和特性,有时间更新下

(/@Ta/2023-03-27 11:12//)

上一页 3/3页,共56楼

回复需要登录

11月22日 06:38 星期五

本站由hu60wap6华为CPU驱动

备案号: 京ICP备18041936号-1