gpt4 book ai didi

Error in Websocket connectivity between R and Discord(R和Discorde之间的WebSocket连接出错)

转载 作者:bug小助手 更新时间:2023-10-24 22:19:41 24 4
gpt4 key购买 nike



I am developing an R code that uses a web-socket to send messages as they arrive from Discord to R (not to be confuse with having R querying Discord constantly for new messages, using httr::GET).

我正在开发一种R代码,它使用网络套接字在消息从不一致到达R时发送它们(不要与让R使用HTTR::GET不断查询不一致的新消息相混淆)。


Rather than having R asking Discord every certain amount of time for new messages (so you know, Discord does not allows this; Discord has a limit of how many times you can request messages), the websocket allows for Discord to send messages to R only when the arrive.

网络套接字不是让R每隔一段时间就向R请求一条新消息(所以你知道,不一致是不允许的;不一致有你可以请求消息的次数限制),而是允许不一致仅在消息到达时才向R发送消息。


In R there are two libraries (websocket and httpuv) that do this, but the code I developed while working, closes the connection after some random amount of time from a few minutes to hours (I think Python has a safety reconnecting code, yet to be implemented in R, but not sure). The errors have different names like: "Unhandled promise error: invalid state", etc.

在R中有两个库(WebSocket和Httpuv)可以做到这一点,但我在工作时开发的代码会在几分钟到几小时之间的一段随机时间后关闭连接(我认为Python有一个安全的重新连接代码,尚未在R中实现,但不确定)。这些错误有不同的名称,如“未处理的承诺错误:无效状态”等。


I wonder what am I getting wrong in this code, am I missing a line of code to re-start connections upon the connection is closed or crashed?

我想知道我在这段代码中犯了什么错误,我是否遗漏了一行代码,以便在连接关闭或崩溃时重新启动连接?


library(websocket)
library(jsonlite)
library(async)


Heartbeat =10 #container for heartbite

DiscordSignals <- function(DiscordToken){

#payloads or messages that R has to send to Discord.
Rpayload = list(
op= 2,
d= list(
token= DiscordToken,
intents= 513,
properties=list(
os= "windows",
browser= "firefox",
device= "firefox")
)
)

KeepAlive = list(op= 1, d= "null" )


Jsonpayload = toJSON(Rpayload, auto_unbox = TRUE, pretty = TRUE)
JsonKeepAlive = toJSON(KeepAlive, auto_unbox = TRUE, pretty = TRUE)

ws <<- WebSocket$new("wss://gateway.discord.gg/?v=9&encording=json", errorLogChannels ="warn")

ws$onOpen(function(event) {ws$send(Jsonpayload)}) #Handshake

ws$onMessage(function(event) {
d <- event$data
json = fromJSON(d)
Alert= json$d$content
OP<<-as.numeric(json$op) #Type of message sent from Discord

#https://discord.com/developers/docs/topics/gateway#resuming
#Discord may request additional heartbeats from your app by sending a Heartbeat (opcode OP1) event. Upon receiving the event, .. immediately send back .. Heartbeat ...

#reset heartbeat rate to whatever asked by Discord. Heartbite rate requested from Discord is sent in OPs 1 or 10.
if (OP %in% c(1,10)){Heartbeat <<-round(abs(((json$d$heartbeat_interval )/1000)-runif(1)-3),0)}

print (Alert)
})

#send heartbeat every given interval
async({
p=1
while (p==1){
await(delay(Heartbeat))
ws$send(JsonKeepAlive)

}
})

}


DiscordSignals (BotToken)

Discord.py has an extensive set of functions about this connectivity, but I could not make sense of it.

Discord.py有一组关于这种连接性的广泛函数,但我不能理解它。


更多回答

Please only show code in other languages if it's needed to explain the problem requirements; simply saying "it's possible to solve this problem in another language" is at best unhelpful and at worst could be interpreted as ranting about the language you're trying to use. Especially please don't tag languages simply because you include such examples; tags should be used to indicate what kind of expertise is needed to answer your question. Python experts have no inherent advantage in figuring out a problem with R code.

如果需要解释问题需求,请只显示其他语言的代码;简单地说“可以用另一种语言解决这个问题”充其量是没有帮助的,在最坏的情况下可能被解释为对您试图使用的语言大喊大叫。尤其是,请不要仅仅因为包含了这样的例子就给语言加上标签;标签应该用来表明回答你的问题需要什么样的专业知识。在找出R代码的问题方面,Python专家没有先天的优势。

please note that providing the example of how it works in Python, could give hits into how should be done in R, specially if Python has that functionality more developed.

请注意,提供它在Python中如何工作的示例可能会给应该如何在R中完成工作带来一些启发,特别是如果Python有更发达的功能的话。

优秀答案推荐

I found a solution to this...probably not the most elegant, but still a solution, but please feel free to post any alternative solution as a legacy.

我找到了一个解决方案…可能不是最优雅的,但仍然是一个解决方案,但请随时张贴任何替代解决方案作为遗产。


My solution is as this: it turns out that the breakdown of the connection between Discord and R can be caused by either of them, and can be for diverse reasons.

我的解决方案是这样的:事实证明,不和谐和R之间的联系的崩溃可能是由它们中的任何一个引起的,也可能是出于不同的原因。


So my solution is to create an asynchronicity task checking that the connection is open, if not reset the connection. I have had this connection running for 24hours and is still on, while 4 times could have crashed in that time interval.

因此,我的解决方案是创建一个异步性任务,检查连接是否打开,如果没有,则重置连接。我已经让这个连接运行了24小时,而且还在运行,而在这段时间间隔内,可能已经崩溃了4次。


DiscordSignals(BotToken) #Start connection

#Check in the background for the connection, if close open again.
async({
q=1
while (q==1){
await(delay(5))
Socket=ws$readyState()[1] #If opened, value should be 1
if (Socket!=1){
print("Restart");
ws$close();
DiscordSignals(BotToken);
await(delay(40))} #wait some time for next check, to prevent connecting too many times
}
})

更多回答

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com