【问题标题】:Pusher connection - socketId is nullPusher 连接 - socketId 为空
【发布时间】:2015-06-12 21:29:53
【问题描述】:
Pusher pusher = new Pusher(app key);  
String socketId = pusher.getConnection().getSocketId();

socketId 在尝试连接推送器时为空。

Pusher 客户端用来进行 websocket 调用的 uri 是

ws://ws.pusherapp.com:80/app/{app Key}?client=java-client&protocol=5&version=0.3.3

这将返回一个NULL 套接字 ID

但是,如果我使用使用相同 URI 的测试客户端建立 Websocket 连接,我会得到一个有效的 socketId。我做错了什么?

【问题讨论】:

    标签: java pusher


    【解决方案1】:

    在建立连接之前不会设置 socketId。请在此处查看 onConnectionStateChange 接口方法: https://github.com/pusher/pusher-websocket-java#api-overview

    这是专门为获取socketId而更新的代码:

    // Create a new Pusher instance
    Pusher pusher = new Pusher(YOUR_APP_KEY);
    
    pusher.connect(new ConnectionEventListener() {
        @Override
        public void onConnectionStateChange(ConnectionStateChange change) {
            String socketId = pusher.getConnection().getSocketId();
            System.out.printLn("The socketId is: " + socketId);
        }
    
        @Override
        public void onError(String message, String code, Exception e) {
            System.out.println("There was a problem connecting!");
        }
    }, ConnectionState.Connected);
    

    【讨论】:

      猜你喜欢
      • 2020-09-05
      • 2015-04-25
      • 1970-01-01
      • 2019-12-26
      • 1970-01-01
      • 1970-01-01
      • 2020-07-19
      • 2014-08-15
      • 2019-10-14
      相关资源
      最近更新 更多