简体   繁体   中英

Android Socket Io

I'm trying to access to this server http://zajelme.com:3400 by using socket Io. But socket.connected() give me false.

Android Code

在此处输入图片说明

Socket Client Code

在此处输入图片说明

Gradle

implementation('com.github.nkzawa:socket.io-client:0.3.0') {
    exclude group: 'org.json', module: 'json'
}

The way you have implemented Socket is not enough to know why my connection is not being build or what error is occurred. Try this way may be this can help you better implement SOCKET and will help you to debug better.

Start Socket connection like this:

Socket mSocket;
 /**
 * Initialize IO Socket Object
 */
private void initSocketConnection() {
    try {
        mSocket = IO.socket("http://zajelme.com:3400");
        connectSocket();
    } catch (URISyntaxException e) {
        throw new RuntimeException(e);
    }
}

/**
 * Connect socket with the server
 */
private void connectSocket() {
    try {
        mSocket.connect();
        mSocket.on(Socket.EVENT_CONNECT, onConnect);
        mSocket.on(Socket.EVENT_CONNECT_ERROR, onError);
        mSocket.on(Socket.EVENT_CONNECT_TIMEOUT, onError);
        mSocket.on(Socket.EVENT_ERROR, onError);
        mSocket.on(Socket.EVENT_DISCONNECT, onDisconnect);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

Stop Socket connection like this:

/**
 * Called to stop socket connection and all listeners associated with it
 */
public void stopSocket() {
    if (mSocket != null) {
        mSocket.disconnect();
        mSocket.off(Socket.EVENT_CONNECT, onConnect);
        mSocket.off(Socket.EVENT_DISCONNECT, onDisconnect);
        mSocket.off(Socket.EVENT_CONNECT_ERROR, onError);
        mSocket.off(Socket.EVENT_CONNECT_TIMEOUT, onError);
        mSocket.off(Socket.EVENT_ERROR, onError);
        mSocket.off(Socket.EVENT_CONNECTING);
    }
}

And here are the default listeners that you will connect bind while connecting so that in case of errors etc you can get the clear idea what happened:

 /**
 * Receiver class for socket disconnected event
 */
private Emitter.Listener onDisconnect = new Emitter.Listener() {
    @Override
    public void call(Object... args) {
        Log.e("SOCKET_TEST", "DISCONNECTION SUCCESSFULL");
    }
};

/**
 * Receiver class for socket connected event
 */
private Emitter.Listener onConnect = new Emitter.Listener() {
    @Override
    public void call(Object... args) {
        Log.e("SOCKET_TEST", "CONNECTION SUCCESSFULL");
    }
};

/**
 * Receiver for socket failed events
 */
private Emitter.Listener onError = new Emitter.Listener() {
    @Override
    public void call(Object... args) {
        Log.e("SOCKET_TEST", "CONNECTION FAIL " + args.toString());

    }
};

In app gradle:

     compile('io.socket:socket.io-client:0.8.3') {
        // excluding org.json which is provided by Android
        exclude group: 'org.json', module: 'json'
    }

Note: check logs so that you know what is going on.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM