【发布时间】:2020-11-13 21:37:31
【问题描述】:
这不会是一个关于解决一个问题的非常具体的问题,因此对此深表歉意。这是我之前询问的关于获取我的 Discord 机器人网站的实时服务器计数的问题的后续。在我看来,websocket 连接是获取此信息的最佳方式,因为通过 discord.js 获取此信息占用了太多内存,因为 discord.js 比我需要的要多得多。
我知道对于 websocket 连接,我需要获取连接 URL,连接,然后进行心跳和接收心跳确认。不过,实际上将这些想法转化为代码时,我遇到了很多麻烦。到目前为止,我可以请求一个 URL、连接并开始心跳,但我不知道如何 1)在接收信息的同时保持心跳,以及 2)如果连接中断(我没有收到确认)停止心跳。引起我问题的主要原因是无法与多个消息侦听器一起工作。如果有人能指出我正确的总体方向或解决我当前问题的方法,将不胜感激。
当前代码,如果有帮助的话(我知道它并没有真正起作用):
const XMLHttpRequest = require('xmlhttprequest').XMLHttpRequest
const WebSocket = require('ws')
let xhr = new XMLHttpRequest()
xhr.onload = (req, res) => {
if (xhr.readyState === 4 && xhr.status === 200) {
console.log(xhr.responseText)
main(JSON.parse(xhr.responseText))
} else {
console.error(`ReadyState: ${xhr.readyState} Status: ${xhr.status}`)
}
}
xhr.open("GET", 'https://discord.com/api/gateway/bot', false)
xhr.setRequestHeader("Authorization", "MY_BOT_TOKEN_THIS_IS_A_PLACEHOLDER_DONT_TELL_ME_ITS_WRONG_IN_COMMENTS")
xhr.send()
function main(response) {
let socket = new WebSocket(`${response.url}/?v=6&encoding=json`)
var hello
socket.onopen = (event) => {
console.log("Connection established")
}
socket.onclose = (event) => {
console.log(`Connection closed: code=${event.code} reason=${event.reason}`)
}
socket.addEventListener('message', (event) => {
console.log(`Data received: ${event.data}`)
hello = JSON.parse(event.data)
if (hello.op == 10) {
let alive = true
heartbeat(socket, hello.s, hello.d.heartbeat_interval, alive)
socket.send(JSON.stringify({
"op": 2,
"d": {
"token": "MY_BOT_TOKEN_THIS_IS_A_PLACEHOLDER_DONT_TELL_ME_ITS_WRONG_IN_COMMENTS",
"properties": {
"$os": "windows",
"$device": "test",
"$browser": "test"
},
"guild_subscriptions": false,
"intents": 1
}
}))
socket.removeEventListener('message')
socket.addEventListener('message', (event) => {
if (JSON.parse(event.data).op !== 11) {
console.log(`Message received: ${event.data}`)
}
})
}
})
}
function heartbeat(socket, s, interval, alive) {
socket.send(JSON.stringify({"op": 1, "d": s}))
console.log("Heartbeat sent")
setTimeout(() => {
alive = false
}, interval)
}
function listenForMessage(event) {
if (JSON.parse(event.data).op === 11) {
let data = JSON.parse(event.data)
console.log(`Heartbeat received: ${event.data}`)
if (data.hasOwnProperty("s")) {
s = data.s
}
socket.removeEventListener('message')
if (alive) {
heartbeat(socket, s, interval)
socket.addEventListener('message', listenForMessage)
}
} else {
}
}
【问题讨论】:
标签: javascript websocket discord