[英]Why isn't my timer task working for my Discord API wrapper?
I'm making an API wrapper for Discord and I'm trying to send a heartbeat payload at the interval given in the hello payload.我正在为 Discord 制作 API 包装器,并且我正在尝试以 hello 有效负载中给定的间隔发送心跳有效负载。
private void handleReceive(GatewayReceive gatewayReceive)
{
PayloadReceiveOpcode opcode=PayloadReceiveOpcode.getInstance(gatewayReceive.op);
if(opcode==null)
{
//TODO Add throws here or something
return;
}
switch(opcode)
{
case DISPATCH:
try
{
System.out.println(gatewayReceive.t);
client.getEventManager().handle(gatewayReceive.t,gatewayReceive.d);
}
catch(InvocationTargetException|IllegalAccessException e)
{
e.printStackTrace();
}
break;
case HEARTBEAT:
break;
case RECONNECT:
break;
case INVALID_SESSION:
break;
case HELLO:
new Timer().scheduleAtFixedRate(new TimerTask(){
@Override
public void run()
{
send(new GatewaySend(PayloadSendOpcode.HEARTBEAT,"null"));
}
},0,gatewayReceive.d.get("heartbeat_interval").getAsInt());
break;
case HEARTBEAT_ACK:
}
}
It's supposed to send it at the interval, but it only sends it once.它应该在间隔发送它,但它只发送一次。 There are no errors.
没有错误。
I fixed it by making it not a daemon thread, so it will keep the program running while the loop is running, using this code timer=new Timer(false);
我通过使它不是一个守护线程来修复它,所以它会在循环运行时保持程序运行,使用这个代码
timer=new Timer(false);
. . However, I will make a new main loop instead of using this one to keep the program running.
但是,我将创建一个新的主循环,而不是使用这个来保持程序运行。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.