【问题标题】:Error Subscribing to Pusher Presence channel from Android从 Android 订阅 Pusher Presence 频道时出错
【发布时间】:2015-04-09 13:35:21
【问题描述】:

我正在尝试从 Android 客户端订阅在线状态频道,但我一直未能通过身份验证部分。我可以毫无问题地订阅普通频道。 连接推送器:

      try {
            authorizer = new HttpAuthorizer("http://test.xxxx.io/pusher/auth");
            authorizer.setHeaders(getHeaders());
            PusherOptions options = new PusherOptions().setEncrypted(true).setAuthorizer(authorizer);

            pusher = new Pusher(JWTHelper.pusherKey, options);
            pusher.connect(new ConnectionEventListener() {
                @Override
                public void onConnectionStateChange(ConnectionStateChange change) {
                    Log.d(TAG, "State changed to " + change.getCurrentState() + " from " + change.getPreviousState());
                    if (change.getCurrentState().toString().equalsIgnoreCase("CONNECTED")) {
                        Log.d(TAG, "Try to connect to presence channel.");
                        connectToChannels(pusher);
                    }
                }

                @Override
                public void onError(String message, String code, Exception e) {
                    Log.d(TAG, "There was a problem connecting to Pusher." + message);
                }
            }, ConnectionState.ALL);

            if (pusher.getConnection().getState() == ConnectionState.DISCONNECTED) {
                pusher.connect();
            } else if (pusher.getConnection().getState() == ConnectionState.CONNECTED) {
                pusher.disconnect();
            }
        } catch (Exception e) {
            Log.d(TAG, "Pusher exception:" + e.getMessage());
        }

然后我尝试订阅一个频道(有效)和一个出席频道(这是它不起作用的地方):

String socketId = pusher.getConnection().getSocketId();
Log.d(TAG, "socket id: " + socketId);
HashMap<String, String> parameters = new HashMap<String, String>();
parameters.put("socket_id", socketId);
parameters.put("channel_name", "presence-mod_111");
authorizer.setQueryStringParameters(parameters);

// test. this works
pusher.subscribe("account_111");

// error
pusher.subscribePresence("presence-mod_111");

这是堆栈跟踪:

State changed to CONNECTING from DISCONNECTED
(standard input):D/TEST( 6949): State changed to CONNECTED from CONNECTING
(standard input):D/TEST( 6949): Try to connect to presence channel.
(standard input):D/TEST( 6949): (in method)socket id: 45048.110272
(standard input):D/TEST( 7676): connect to account 111...
(standard input):D/TEST( 7676): State changed to CONNECTING from DISCONNECTED
(standard input):D/TEST( 7676): connection..null
(standard input):D/TEST( 7676): State changed to CONNECTED from CONNECTING
(standard input):D/TEST( 7676): Try to connect to presence channel.
(standard input):D/TEST( 7676): (in method)socket id: 45142.76981
(standard input):D/TEST( 9258): State changed to CONNECTING from DISCONNECTED
(standard input):D/TEST( 9258): State changed to CONNECTED from CONNECTING
(standard input):D/TEST( 9258): Try to connect to presence channel.
(standard input):D/TEST( 9258): socket id: 45107.97746
(standard input):D/TEST( 9258): arg0 java.io.FileNotFoundException: http://test.xxx.com/pusher/auth, arg1: com.pusher.client.AuthorizationFailureException: java.io.FileNotFoundException: http://test.xxx.com/pusher/auth

我没有更多的想法,任何提示都会很棒。谢谢。

【问题讨论】:

  • 嗨,您找到解决方案了吗?

标签: android pusher


【解决方案1】:

当您订阅在线状态频道时,图书馆需要针对服务器对订阅进行身份验证(需要有一个通常是服务器的授权)。

您可以在日志中看到此身份验证尝试:

http://test.xxx.com/pusher/auth, arg1: com.pusher.client.AuthorizationFailureException: java.io.FileNotFoundException: http://test.xxx.com/pusher/auth

有两种解决方案:

  1. 将身份验证端点设置为有效端点并实现authentication mechanism。您可以找到客户端代码here 的示例。

  2. 定义您自己的 Authorizer,它以其他方式创建身份验证令牌。在Pusher Java Client README 上查看setAuthorizer

【讨论】:

  • 您好,谢谢您的回答。端点存在,因为 .js 客户端可以连接到它。只是为了我与 android 的连接,它不起作用。是否需要在授权对象中设置特殊的标头参数?
  • @GabrielaRadu 您用于身份验证的内容完全取决于您。这实际上取决于您的应用程序登录过程。您的应用程序如何知道当前用户是谁?它应该将该信息作为身份验证回调的一部分传递。使用HttpAuthorizer,您可以设置标头或查询字符串参数。
猜你喜欢
  • 1970-01-01
  • 2013-08-10
  • 2015-06-05
  • 2014-07-15
  • 1970-01-01
  • 2017-05-12
  • 2016-07-14
  • 2017-05-26
  • 1970-01-01
相关资源
最近更新 更多