我在服务器上使用NodeJs
来发送和请求响应,我的服务和发送请求以及当我从活动发送请求时获得响应正常,但是当我尝试从Fragment
发送请求时请求是重复和两次,我无法解决这个问题
我的服务:
public class SignalChatServiceProvider extends Service {
private SignalApplication signalApplication;
private Realm realm;
private String marketUUid;
public static SignalChatServiceProvider instance = null;
public static boolean isInstanceCreated() {
return instance == null ? false : true;
}
private final IBinder myBinder = new LocalBinder();
public class LocalBinder extends Binder {
public SignalChatServiceProvider getService() {
return SignalChatServiceProvider.this;
}
}
public void IsBendable() {
}
@Override
public IBinder onBind(Intent intent) {
return myBinder;
}
@Override
public void onCreate() {
if (isInstanceCreated()) {
return;
}
super.onCreate();
new Handler(getMainLooper()).post(new Runnable() {
@Override
public void run() {
Utils.toast("onCreate",getBaseContext());
}
});
realm = Realm.getDefaultInstance();
signalApplication = (SignalApplication) getApplication();
if (signalApplication.getSocket() == null)
signalApplication.CHAT_SOCKET = signalApplication.getSocket();
signalApplication.getSocket().on(Socket.EVENT_CONNECT_ERROR, onConnectError);
signalApplication.getSocket().on(Socket.EVENT_CONNECT_TIMEOUT, onConnectError);
signalApplication.getSocket().on(Socket.EVENT_CONNECT, onConnect);
//@formatter:off
signalApplication.getSocket().on("getMarketBanners" , getMarketBanners);
signalApplication.getSocket().on("getMarketProducts" , getMarketProducts);
signalApplication.getSocket().on("getProductImages" , getProductImages);
signalApplication.getSocket().on("getMarketRandomProductImages" , getMarketRandomProductImages);
signalApplication.getSocket().on("getRandomMarketLists" , getRandomMarketLists);
//@formatter:on
EventBus.getDefault().register(this);
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
if (isInstanceCreated()) {
return 0;
}
super.onStartCommand(intent, flags, startId);
connectConnection();
return START_STICKY;
}
private Emitter.Listener onConnect = new Emitter.Listener() {
@Override
public void call(Object... args) {
};
private Emitter.Listener onDisconnect = new Emitter.Listener() {
@Override
public void call(Object... args) {
};
private Emitter.Listener onConnectError = new Emitter.Listener() {
@Override
public void call(Object... args) {
};
private Emitter.Listener getMarketBanners = new Emitter.Listener() {
@Override
public void call(final Object... args) {
};
private Emitter.Listener getMarketRandomProductImages = new Emitter.Listener() {
@Override
public void call(final Object... args) {
};
private Emitter.Listener getProductImages = new Emitter.Listener() {
@Override
public void call(final Object... args) {
};
private Emitter.Listener getRandomMarketLists = new Emitter.Listener() {
@Override
public void call(final Object... args) {
};
private Emitter.Listener getMarketProducts = new Emitter.Listener() {
@Override
public void call(final Object... args) {
};
private void connectConnection() {
instance = this;
signalApplication.getSocket().connect();
}
private void disconnectConnection() {
instance = null;
signalApplication.getSocket().disconnect();
}
@Override
public void onDestroy() {
super.onDestroy();
if (!realm.isClosed())
realm.close();
signalApplication.getSocket().off(Socket.EVENT_CONNECT, onConnect);
signalApplication.getSocket().off(Socket.EVENT_DISCONNECT, onDisconnect);
signalApplication.getSocket().off(Socket.EVENT_CONNECT_ERROR, onConnectError);
signalApplication.getSocket().off(Socket.EVENT_CONNECT_TIMEOUT, onConnectError);
//@formatter:off
signalApplication.getSocket().off("getMarketBanners" , getMarketBanners);
signalApplication.getSocket().off("getMarketProducts" , getMarketProducts);
signalApplication.getSocket().off("getProductImages" , getProductImages);
signalApplication.getSocket().off("getMarketRandomProductImages" , getMarketRandomProductImages);
signalApplication.getSocket().off("getRandomMarketLists" , getRandomMarketLists);
//@formatter:on
disconnectConnection();
}
@Subscribe
public void onEvent(EventChangeChatServerStateEvent event) {
switch (event.getState()) {
case connected:
instance = this;
connectConnection();
break;
case disconnected:
instance = null;
disconnectConnection();
break;
}
}
}
我的活动禁用多种运行服务:
if (SignalApplication.signalApplication.getSocket() != null && !SignalChatServiceProvider.isInstanceCreated()) {
startService(new Intent(getBaseContext(), SignalChatServiceProvider.class));
doBindService();
}
现在当我尝试使用此活动的代码发送请求时:
SignalApplication.signalApplication.getSocket().emit("getRandomMarketLists");
请求计数是一个
我的片段进入ViewPager
:
@Override
public Fragment getItem(int position) {
switch (position) {
case 0:
return FragmentMainMarkets.newInstance();
...
}
}
当我尝试从FragmentMainMarkets
片段发送请求时,请求是两次