• [free] exclusive: this is a very valuable collection of Android knowledge system!!

One, a brief introduction

Socket.IO is a cross-platform open source framework that is completely implemented by JavaScript, based on Node.js and supports WebSocket protocol for real-time communication. It includes client-side JavaScript and server-side Node.js.

The official introduction seems a bit muddled, but to put it simply:

The client can establish a real-time communication channel with the server through socket. IOCopy the code

Second, the application of

The following is the introduction of socket. IO communication pipeline laying, communication and destruction work.

2.1 Importing socket. IO packets

The compile 'IO. Sockets: the socket. IO - client: 0.8.3'Copy the code

2.2 Instantiating Socket Objects

Server address (this address is the official test address of socket. IO, which needs to be changed in the actual project) :

String CHAT_SERVER_URL = "https://socket-io-chat.now.sh/"
Copy the code

Instantiate the Socket object based on the server address:

Socket mSocket = IO.socket(CHAT_SERVER_URL, options);
Copy the code

Options are available for users to select some configuration parameters. Some configurations are as follows:

Public static class, which extends the IO. Socket. Engineio. Client. Socket. Options {/ / whether the automatic reconnection public Boolean reconnection = true; Public int reconnectionAttempts; Public long reconnectionDelay; Public long reconnectionDelayMax; // Connection timeout duration (ms). If this parameter is set to -1, no connection timeout is set. Public Long timeout = 20000; }Copy the code

2.3 Registering Listener

In this case, we need to register some listener events, which are used to listen for some behaviors generated during Socket communication. For example, the following is the listener callback after the Socket connection is registered successfully:

Emitter.Listener connectListener;
connectListener = new Emitter.Listener() {
            @Override
            public void call(Object... args) {

            }
        };
socket.on(Socket.EVENT_CONNECT, connectListener);
Copy the code

2.4 Establishing connections through Socket objects

At this point, our pipeline laying work has been completed, next we just need to turn on the switch of the pipeline, so that the client and server can communicate with each other:

mSocket.connect();
Copy the code

So how do we know if the connection has been made? Yes, we registered the listener in 2.3. Once the Socket connection is successful, the callback will be triggered. At this point, we can do the corresponding processing according to the actual needs of the project.

2.5 Simple Use

At this point, if we want to send a message to the server, how do we do that? Socket.IO provides the following operations:

mSocket.emit("new message", content);
Copy the code

You can also do this:

mSocket.emit("new message", content, new Ack() {
        @Override
        public void call(Object... args) {
            
        }
    });
Copy the code

2.6 Destruction of pipelines

When we do not want to use the pipe any more, we need to destroy the pipe. It is easy to disconnect the pipe and unplug the listener:

mSocket.disconnect();
mSocket.off(Socket.EVENT_CONNECT, connectListener);
Copy the code

Ok, socket. IO the whole communication process is complete, is not easy to use for us.

Complete source code is provided

I do not know the part is not popular (ha ha ~), the source code is based on socket. IO official server address combined with the actual project fusion, strong expansion, special share out for your reference.

Implement Android chat function based on socket. IO

Finally, thank you for your arrival, congratulations, adhere to the end, the article and source code if there are inappropriate, please be corrected.


  • [free] exclusive: this is a very valuable collection of Android knowledge system!!