【问题标题】:Pusher Private Channel Subscription in androidandroid中的推送器私人频道订阅
【发布时间】:2017-05-22 11:58:02
【问题描述】:

我在 android 中的私人频道订阅有问题。 这是我的代码

        HashMap<String, String> headers = new HashMap<>();
        headers.put("Authorization", code);
        headers.put("Content-Type", "application/x-www-form-urlencoded");
        headers.put("Accept", "application/json");

        final HttpAuthorizer authorizer = new HttpAuthorizer("My URL");

        authorizer.setHeaders(headers);

        PusherOptions options = new PusherOptions()
                .setEncrypted(true)
                .setCluster("ap2")
                .setAuthorizer(authorizer);

        final Pusher pusher = new Pusher("KEY", options);

        pusher.connect(new ConnectionEventListener() {
            @Override
            public void onConnectionStateChange(ConnectionStateChange change) {

            }

            @Override
            public void onError(String message, String code, Exception e) { }
        }, ConnectionState.ALL);

        PrivateChannel channel = pusher.subscribePrivate(channelName);
        channel.bind("message-sent", new PrivateChannelEventListener() {
            @Override
            public void onAuthenticationFailure(String string, Exception ex) {}
            @Override
            public void onSubscriptionSucceeded(String string) {
            }

            @Override
            public void onEvent(String string, String string1, String string2) {}});
pusher.connect();

这会创建成功连接,但在订阅中它确实返回了任何结果。 请帮我解决这个问题。

【问题讨论】:

    标签: android pusher


    【解决方案1】:

    我认为您的问题来自在连接通道之前调用了 bind() 。

    您可以尝试将订阅和绑定调用放入回调中,类似于此(我将它们移到方法中以便更容易理解):

    Pusher pusher;
    PrivateChannel channel;
    
    void subscribeToChannel() {
        channel = pusher.subscribePrivate(channelName, new PrivateChannelEventListener() {
            @Override
            public void onSubscriptionSucceeded(String s) {
                bindToEvents();
            }
            @Override
            public void onAuthenticationFailure(String s, Exception e) {}
            @Override
            public void onEvent(String s, String s1, String s2) {}
        });
    }
    
    void bindToEvents() {
        channel.bind("message-sent", new PrivateChannelEventListener() {
            @Override
            public void onAuthenticationFailure(String string, Exception ex) {}
            @Override
            public void onSubscriptionSucceeded(String string) {}
            @Override
            public void onEvent(String string, String string1, String string2) {}});
    }
    

    在 main 方法中,您只需调用pusher.connect(),连接成功后将调用subscribeToChannel()

        HashMap<String, String> headers = new HashMap<>();
        headers.put("Authorization", code);
        headers.put("Content-Type", "application/x-www-form-urlencoded");
        headers.put("Accept", "application/json");
        final HttpAuthorizer authorizer = new HttpAuthorizer("My URL");
        authorizer.setHeaders(headers);
    
        PusherOptions options = new PusherOptions()
                .setEncrypted(true)
                .setCluster("ap2")
                .setAuthorizer(authorizer);
    
        final Pusher pusher = new Pusher("KEY", options);
    
        pusher.connect(new ConnectionEventListener() {
    
            @Override
            public void onConnectionStateChange(ConnectionStateChange change) {
    
                if (change.getCurrentState() == ConnectionState.CONNECTED) {
                    subscribeToChannel();
                }
            }
    
            @Override
            public void onError(String s, String s1, Exception e) {}
        });
    

    【讨论】:

    • 我试过了,但现在我在 subscribePrivate 中收到 onAuthenticationFailure 调用。
    • 我用过这个,但得到这个错误'致命异常:java.lang.IllegalArgumentException:指定为非空的参数为空:方法 kotlin.jvm.internal.Intrinsics.checkParameterIsNotNull,com 中的参数 e .mobile.raymond.hireplicity.modules.main.view.MainActivity$connectToChannel$1.onError(:12) at com.pusher.client.connection.websocket.WebSocketConnection$5.run(WebSocketConnection.java:238) at com.pusher。 client.util.Factory$1.run(Factory.java:109) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)'
    猜你喜欢
    • 2014-07-15
    • 2020-11-22
    • 1970-01-01
    • 2018-02-26
    • 2020-05-21
    • 2017-05-12
    • 2021-03-09
    • 1970-01-01
    • 2017-10-08
    相关资源
    最近更新 更多