Reputation: 7425
My Android
app is using the AndroidAsync
library to connect to a Node.js
server with the Socket.IO
client. The socket client is established through a service.
I am currently starting the Socket.IO service in the onResume
and stopping in onPause
of each Activity
in the app.
This seems really inefficient because I basically stopping the socket and recreating a new one every time I press the home button, or switch to another activity in the app.
What would be the best way to handle the above requirements about keeping the socket open?
public class SocketIOService extends Service {
private Preferences prefs;
private SocketIOClient socket;
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
try {
ConnectCallback callback = new ConnectCallback() {
@Override
public void onConnectCompleted(Exception ex, SocketIOClient client) {
if (ex != null) {
ex.printStackTrace();
return;
}
Log.v("SOCKET IO CONNECTION", "ESTABLISHED");
client.setDisconnectCallback(new DisconnectCallback() {
@Override
public void onDisconnect(Exception e) {
Log.v("SOCKET IO CONNECTION", "TERMINATED");
}
});
client.setErrorCallback(new ErrorCallback() {
@Override
public void onError(String error) {
Log.e("SOCKET IO ERROR", error);
}
});
client.setExceptionCallback(new ExceptionCallback() {
@Override
public void onException(Exception e) {
e.printStackTrace();
}
});
client.setReconnectCallback(new ReconnectCallback() {
@Override
public void onReconnect() {
Log.v("SOCKET IO CONNECTION", "RECONNECTED");
}
});
client.on(EVENT_NEWS, new EventCallback() {
@Override
public void onEvent(JSONArray argument, Acknowledge acknowledge) {
Log.v("EVENT:NEWS", argument.toString());
}
});
client.on(EVENT_MESSAGE_RECEIVE, new EventCallback() {
@Override
public void onEvent(JSONArray argument, Acknowledge acknowledge) {
handleMessageReceive(argument);
}
});
}
};
socket = SocketIOClient.connect(AsyncHttpClient.getDefaultInstance(), URL_SERVER, callback).get();
JSONArray array = new JSONArray();
JSONObject obj = new JSONObject();
prefs = new Preferences(this);
try {
obj.put(KEY_USER_ID, prefs.getUserId());
} catch (JSONException e) {
e.printStackTrace();
}
array.put(obj);
socket.emit(EVENT_LOG_USER_ID, array);
Log.v("SOCKET LOG USER ID", array.toString());
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
// If we get killed, after returning from here, restart
return START_REDELIVER_INTENT;
}
@Override
public void onDestroy() {
Log.v("SOCKET IO SERVICE", "STOPPED");
if (socket != null) {
if (socket.isConnected()) {
socket.disconnect();
}
}
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
private void handleMessageReceive(JSONArray json) {
ChatMessage message = JReader.createMessage(json);
}
}
Upvotes: 18
Views: 6136
Reputation: 836
Looking into old unanswered questions and found this one. I think the best approach is to use a bound Service that autostarts.
Using the bindService() you can bind your Activities in onResume() or onStart() and let your service close the connection and stop itself when there is no client bound on it.
You can find more information here https://developer.android.com/guide/components/bound-services
Upvotes: 1
Reputation: 641
I have partial solution(won't solve your third requirement I think). I started to wrote a simple multi player game just for studing game programming. I decided to use TCP conntection(why? its another issue...), and I had to keep the connection open while the user proceeded in each Acitivty(login -> lobby -> choose opponent...), I used a Singleton patthern(double-locking check), containing the connection socket.
pseudo code
class ClientConnection{
private static ClientConnection conn = null;
Socket mSocket; // your socket
protected ClientConnection(){
mSocket = new Socket();
// mSocket.connect( bla bla bla )
}
public Socket getSocket(){
return mSocket();
}
public static ClientConnection getInstance(){
if(conn == null){
synchronized (LOCK) {
if(conn == null){
conn = new ClientConnection();
}
}
}
return conn;
}
}
Upvotes: 0