为什么我的计时器任务不能用于Discord API包装器?

时间:2019-10-03 22:55:55

标签: java websocket discord wrapper api-design

我正在为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:

    }
}

应该按一定的时间间隔发送它,但是只发送一次。没有错误。

1 个答案:

答案 0 :(得分:0)

我通过使其不是守护程序线程来对其进行了修复,因此可以使用代码I/flutter (11067): Typing: N I/flutter (11067): Typing: Ne I/flutter (11067): Typing: New I/flutter (11067): Typing: New I/flutter (11067): Typing: New t I/flutter (11067): Typing: New te I/flutter (11067): Typing: New tex I/flutter (11067): Typing: New text I/flutter (11067): Typing: New text I/flutter (11067): Typing: New text T I/flutter (11067): Typing: New text Ty I/flutter (11067): Typing: New text Typ I/flutter (11067): Typing: New text Type I/flutter (11067): Typing: New text Typed (After typing Text, User pressed FlatButton) I/flutter (11067): Value got: 在循环运行时使程序保持运行状态。但是,我将创建一个新的主循环,而不是使用该主循环来保持程序运行。