Android中的Pusher连接和订阅过程太慢。有时这两步需要15-20秒。我通过logcat观察到连接过程需要比订阅更长的时间。
我在Github Pusher库上阅读了this link,但我无法申请代码。谁能给我代码示例,如何提高性能?
我们正在使用两个不同的套接字库。以下是与Pusher相关的代码。首先,我们有推送器连接类。
public class PusherConnectivity implements GamingConnectivity {
public Pusher pusher;
public MultiPlayerActivity mpa;
public PusherConnectivity(MultiPlayerActivity mpa){
HttpAuthorizer authorizer = new HttpAuthorizer(URL_HERE);
PusherOptions options = new PusherOptions().setAuthorizer(authorizer).setEncrypted(true);
this.mpa = mpa;
pusher = new Pusher(API_KEY, options);
}
@Override
public void connect() {
String socketId = CommonObjects.getSharedPreferences(mpa.getContext(), "socket_id", "").toString();
pusher.connect(new ConnectivityViaPusher(pusher, mpa));
}
@Override
public void disconnect() {
pusher.disconnect();
}
}
现在我们有其他课程,我们正在用于游戏,其中订阅和游戏部分已完成。以下是相关代码。游戏代码被省略。
public class GamingThroughPusher extends SocketGamingInterface {
PrivateChannel channel;
public Pusher pusher;
private MultiPlayerActivity activity;
public GamingThroughPusher(GamingConnectivity gamingConnectivity) {
super(gamingConnectivity);
pusher = ((Abc)gamingConnectivity).pusher;
activity = ((Abc)gamingConnectivity).mpa;
}
@Override
public void subscribe(String channelName, PrivateChannelEventListener listener) {
try {
channel = pusher.subscribePrivate(channelName, listener, Constants.PUSHER_CONNECTION_EVENT);
} catch (Exception e){
e.printStackTrace();
}
}
@Override
public void unsubscribe(String channelName){
pusher.unsubscribe(channelName);
}
}
现在,最后从我们的片段我调用pusher connect方法和onpusher回调方法,它是onConnectionStateChange我调用了GamingThroughPusher类的subscribe方法。当我收到onSubscriptionSucceed的确认后,我开始游戏工作。
但问题是推动开始大部分时间都很慢。大约需要20秒。即便如此,我们也有非常好的互联网连接。