我正在为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:
}
}
它应该按间隔发送,但只发送一次。没有错误。
最佳答案
我通过使其不是守护程序线程来对其进行修复,因此使用代码timer=new Timer(false);
可以在循环运行时保持程序运行。但是,我将创建一个新的主循环,而不是使用该主循环来保持程序运行。
关于java - 为什么我的计时器任务不能用于Discord API包装器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58227875/