连接后,Java Pusher Client断开连接

时间:2019-10-17 02:12:32

标签: java websocket spring-websocket pusher

我正在使用JAVA代码连接到Pusher服务器,问题是客户端(使用Java客户端库)在与服务器建立连接几秒钟后断开连接,并且无法重新连接自身。

此外,java-client上的连接状态显示为 CONNECTED ,如onConnectionStateChange()回调方法所示,但在后台似乎它保持断开连接,因为当我尝试推送请求时从服务器Java客户端收到的任何信息都没有,但是在重新连接时(手动)我收到了请求。

代码:

@Component
@Slf4j
public class PosWebSocketClient {

private Pusher pusher;

private boolean isConnected = false;

private String pusherAppKey;

private Timer timer;

private Date activeTime;

private boolean isUserLoggedIn = false;

private PusherOptions pusherOptions;

public synchronized void init(String pusherAppKey) {
    log.info("Initializing Pusher");
    pusher = new Pusher(pusherAppKey/* , pusherOptions() */);
    this.pusherAppKey = pusherAppKey;
    this.isUserLoggedIn = true;
    pusher.connect(new ConnectionEventListener() {
        @Override
        public void onConnectionStateChange(ConnectionStateChange change) {
            log.info("State changed to " + change.getCurrentState() + " from "
                    + change.getPreviousState());

            if (change.getCurrentState() == ConnectionState.CONNECTED) {
                isConnected = true;
            } else {
                isConnected = false;
            }
            log.info("isConnected {}", isConnected);                
        }

        @Override
        public void onError(String message, String code, Exception e) {
            log.info("Error while connecting to the server with {} {} {}", message, code, e.getMessage());
            log.error("Exception: - ",e);
        }
    }, ConnectionState.ALL);


    Channel channel = pusher.subscribe("*****");         
    channel.bind("any-event-1", sendDataListener());
    channel.bind("any-event-2", receiveOrderListener());
    channel.bind("any-event-3", logOutListener());
    channel.bind("any-event-4", getOrderStatusListener());


    activeTime = new Date();
    /*
     * if (timer == null) { timer = new Timer(); timer.schedule(new MyTask(), 0,
     * 1000 * 60 * 2); }
     */
}

class MyTask extends TimerTask {
    public void run() {
        long idleTimeInMinutes = (new Date().getTime() - activeTime.getTime()) / (1000 * 60);
        log.info("Pusher Idle Time {} ", idleTimeInMinutes);
        if (isUserLoggedIn && idleTimeInMinutes >= 10 && StringUtils.isNotBlank(pusherAppKey)) {
            log.info("Pusher idle time is greater than 10 mins, initializing again");
            disconnect();
            init(pusherAppKey);
        }
    }
}

private SubscriptionEventListener logOutListener() {
    return new SubscriptionEventListener() {
        @Override
        public void onEvent(PusherEvent pusherEvent) {
        }
    };
}

private SubscriptionEventListener sendDataListener() {
    return new SubscriptionEventListener() {
        @Override
        public void onEvent(PusherEvent pusherEvent) {
            log.info("Received SendData event");
        }
    };
}

private SubscriptionEventListener receiveOrderListener() {
    log.info("Received FetchOrder event");
    return new SubscriptionEventListener() {
        }
    };
}

private SubscriptionEventListener getOrderStatusListener() {
    log.info("Received SendStatus event");
    return new SubscriptionEventListener() {
        }
    };
}

public synchronized void disconnect() {
    // Disconnect from the service (or become disconnected my network conditions)
    if (pusher != null && pusher.getConnection() != null) {
        log.info("Disconnecting Pusher");
        Channel channel = pusher.getChannel("*****");
        channel.unbind("any-event-1", sendDataListener());
        channel.unbind("any-event-2", receiveOrderListener());
        channel.unbind("any-event-3", logOutListener());
        channel.unbind("any-event-4", getOrderStatusListener());
        pusher.unsubscribe("*****");
        pusher.disconnect();
    }
}

public void restart() {
    if (StringUtils.isNotBlank(pusherAppKey)) {
        log.info("Restarting Pusher");
        disconnect();
        this.init(this.pusherAppKey);
    }
}

/*
 * private PusherOptions pusherOptions() { if (pusherOptions == null) {
 * pusherOptions = new PusherOptions();
 * pusherOptions.setMaxReconnectionAttempts(1000000); } return pusherOptions; }
 */

public boolean isConnected() {
    return isConnected;
}

private void setPusherStatus() {
    activeTime = new Date();
}

public void userLoggedOut() {
    this.isUserLoggedIn = false;
}

}

使用的Maven依赖项是

    <dependency>
        <groupId>com.pusher</groupId>
        <artifactId>pusher-java-client</artifactId>
        <version>2.0.0</version>
    </dependency>

任何人都可以看一下,并让我知道与服务器建立连接时所缺少的代码或依赖项或任何属性的问题吗? TIA。

0 个答案:

没有答案