【问题标题】:How to use Pusher on an android app with multiple activities?如何在具有多个活动的 android 应用程序上使用 Pusher?
【发布时间】:2015-11-15 18:36:51
【问题描述】:

我们有这个 android 应用程序,我们使用 Pusher 将实时更新从我们的服务器中继到 android 客户端。

问题是,如果我们将以下代码 sn-p 添加到我们所有的活动页面,Pusher 会为每个页面打开一个新的 Web 套接字。

如何与 pusher 建立一个连接并将其用于我们所有的活动?

String apiKey = "abcde";
        PusherOptions options = (new PusherOptions()).setEncrypted(false);
        Pusher ph = new Pusher(apiKey, options);
        ph.connect(new ConnectionEventListener() {
            @Override
            public void onConnectionStateChange(ConnectionStateChange change) {
                System.out.println("State changed to " + change.getCurrentState() +
                        " from " + change.getPreviousState());
            }

            @Override
            public void onError(String message, String code, Exception e) {
                System.out.println("There was a problem connecting!");
            }
        }, ConnectionState.ALL);

        if(pusher_channel != null){
            Channel channel = ph.subscribe(pusher_channel);

            // Bind to listen for events called "my-event" sent to "my-channel"
            channel.bind("user_is_typing", new SubscriptionEventListener() {
                @Override
                public void onEvent(String channel, String event, String data) {
                    //System.out.println("Received event with data: " + data);
                }
            });


        }

【问题讨论】:

    标签: android java-websocket


    【解决方案1】:

    我们通过将连接脚本放在以下位置解决了这个问题:

    public class MyApplication extends MultiDexApplication
    

    现在我们的应用只打开一个 websocket。

    【讨论】:

      猜你喜欢
      • 2021-03-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-06-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多