我们有这个Android应用程序,我们使用Pusher将实时更新从我们的服务器转发到Android客户端。
问题是如果我们将以下代码片段添加到我们的所有活动页面,Pusher会为每个页面打开一个新的Web套接字。
如何连接推送器并将其用于我们的所有活动?
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);
}
});
}
答案 0 :(得分:1)
我们通过将连接脚本放在:
下解决了这个问题public class MyApplication extends MultiDexApplication
现在我们的应用只打开一个websocket。