0

I'm basically trying to start a Socket.IO connection in one activity (The LogIn Activity) and carry that throughout the various other activities. If anyone has any idea on what would work the best in this situation that would be great. All I need is probably a service that I can run in the background but I haven't been able to figure out how that works. I tried doing this but that doesn't work.

public class KlassAttack extends Application {
    Socket gameConnection;
    @Override
    public void onCreate() {
        try {gameConnection = IO.socket("http://71.13.36.124:56543");}catch(URISyntaxException e){}
        gameConnection.connect();
        Log.e("TEST", "EEE");
    }
}
Lokio27
  • 23
  • 6
  • This will work, no problem. You don't need to create a service because client manages a background thread to send heartbeats and emit data. – Fatih S. Aug 27 '15 at 21:50

1 Answers1

0

U can make a singleton class

public class SocketIOClient {

private static Socket mSocket;

private static void initSocket(Activity activity) {
    try {
       mSocket = IO.socket(Constants.CHAT_SOCKET_URL);
    } catch (URISyntaxException e) {
        e.printStackTrace();
        throw new RuntimeException(e);
    }
}

public static Socket getInstance(Activity activity) {
    if (mSocket != null) {
        return mSocket;
    } else {
        initSocket(activity);
        return mSocket;
    }
} }
Mohamed Hatem Abdu
  • 468
  • 1
  • 7
  • 17