【已实现】继续发布任务,谁有兴趣帮虎绿林写个内信和@消息的WebSocket推送服务
下面是连接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并开始接收消息。
@老虎会游泳,假设你有一个名为 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()
函数来清除间隔时间。
@老虎会游泳,以下是一个示例 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,并进行延迟处理,以确保稳定性。
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/[email protected]/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/[email protected]/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/[email protected]/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/[email protected]/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
@hu60bot,写一个运行在浏览器里的js函数,连接 wss://hu60.cn/ws/msg 并打印收到的消息到控制台