Android socket.io switch activities without lost c

2019-02-20 09:01发布

问题:

I developed a game, which will be use socket.io connection, my server is writing in node js, and my android client use socket.io, and now, my question for You is:

how can I switch activities without lost connection with server, i have minimum 4 activities, chat, game view, online players list and more.

When i turn on my app, I create connection, and when I log in to server i would like to switch activity to another.

Any suggestions?

回答1:

try to make a singleton class SocketIO service

initialize socketio connection at first activity to establish your connection to server, here my example service class

    public class SocketIOClient {
    private static final String serverUrl = "http://example-socket-server.jit.su";
    private static SocketIO socket;
    private static SocketIOClient instance;
    private static Activity act;
    private static String id;

    public SocketIOClient() {
    }

    public static void initInstance(String uid) throws MalformedURLException {
        if (instance == null) {
            instance = new SocketIOClient();
            instance.initID(uid);
            if (SocketIOClient.getSocket() == null) {
                SocketIOClient.setSocket(new SocketIO());
            }
            SocketIOClient.connectIO();
        }
    }

    public static void setActivity(Activity a) {
        SocketIOClient.act = a;
    }

    public static SocketIO getSocket() {
        return socket;
    }

    public static void setSocket(SocketIO socket) {
        SocketIOClient.socket = socket;
    }

    public String getId() {
        return id;
    }

    private void initID(String uid) {
        if (SocketIOClient.id == null) {
            SocketIOClient.id = uid;
        }
    }

    public static void connectIO() throws MalformedURLException {
        try {
            SocketIO.setDefaultSSLSocketFactory(SSLContext.getDefault());
        } catch (NoSuchAlgorithmException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
        SocketIOClient.getSocket().connect(serverUrl, new IOCallback() {
            @Override
            public void onMessage(JSONObject json, IOAcknowledge ack) {
                // TODO Auto-generated method stub
            }

            @Override
            public void onMessage(String data, IOAcknowledge ack) {

            }

            @Override
            public void onError(SocketIOException socketIOException) {
            }

            @Override
            public void onDisconnect() {
                // TODO Auto-generated method stub

            }

            @Override
            public void onConnect() {

            }

            @Override
            public void on(String event, IOAcknowledge ack, Object... args) {

            }
        });
    }

    public static void emit(String event, Object args)
            throws MalformedURLException {
        if (SocketIOClient.getSocket().isConnected() == false) {
            SocketIOClient.getSocket().reconnect();
        }
        SocketIOClient.getSocket().emit(event, args);
    }

    public static void emitWithAcknowledge(String event, Object args)
            throws MalformedURLException {
        if (SocketIOClient.getSocket().isConnected() == false) {
            SocketIOClient.getSocket().reconnect();
        }
        SocketIOClient.getSocket().emit(event, new IOAcknowledge() {

            @Override
            public void ack(Object... args) {
                // TODO Auto-generated method stub

            }
        }, args);
    }

}

at the first activity i put this code:

SocketIOClient.initInstance(android_id);

it's create object and try connect to server, you can handle it at onConnect or onDisconnect if you want event or something

when you switching activities, you're still connected to server and if you want to close your connection use this :

SocketIOClient.getSocket().disconnect();