If you need to use a long connection in development, especially websocket, we usually use Android-webSocket, or okHTTP for links. But of all the options I prefer to use Socketio. The most intuitive feature of Socketio is message classification, which makes message management more convenient. Here I briefly explain how socketio can be used on Android. If you are a server, please find other information to learn.
1. First, we need to introduce the relevant socket. IO development dependencies
Implementation 'IO. Sockets: the socket. IO - client: 1.0.0'Copy the code
2. Next we add network permissions to the project
<uses-permission android:name="android.permission.INTERNET"/>
Copy the code
3. Next we create the socket. IO link
Creating a Socket object
private Socket mSocket;
Copy the code
The mSocket is then initialized
try { IO.Options opts = new IO.Options(); opts.query = "loginUserNum=" + 78; MSocket = IO. Socket (" http://192.168.0.162:9099 ", opts); } catch (URISyntaxException e) {e.printStackTrace(); }Copy the code
Opts is an identification received by the background when establishing a link. It identifies which client is establishing a long connection with the background
4. Start linking
mSocket.connect();
Copy the code
5. Link callback listener successfully
mSocket.on("connect", new Emitter.Listener() { @Override public void call(Object... Args) {runOnUiThread(new Runnable() {@override public void run() {tv_lj.settext (); }}); }});Copy the code
The callbacks here are child threads, so be aware that if you need to change the UI, you must go into the main thread to make the changes.
6. Message receive callback listening
mSocket.on("text", new Emitter.Listener() { @Override public void call(final Object... args) { runOnUiThread(new Runnable() { @Override public void run() { tv_content.setText(args[0].toString()); }}); }});Copy the code
7. Send messages
Msocket. emit("text"," finally sent ");Copy the code
8. Link destruction
Finally, don’t forget to destroy the link
@Override
public void onDestroy() {
super.onDestroy();
mSocket.off();
mSocket.disconnect();
}
Copy the code
9. Configure the network
Now you find that the connection is still not successful, don’t worry, we need to add a configuration
Create the XML package under res and create network_security_config.xml as follows
<? The XML version = "1.0" encoding = "utf-8"? > <network-security-config> <base-config cleartextTrafficPermitted="true" /> </network-security-config>Copy the code
It is then configured under the Application node in the manifest file, androidmanifest.xml
android:networkSecurityConfig="@xml/network_security_config"
Copy the code
At this point our socket. IO link is complete. We can use this set of code for autonomous IM instant messaging, heartbeat setup and so on.
Each socket. IO message can be assigned a message type to ensure that we set up multiple socket. IO callbacks to listen for different messages and achieve message classification. At the same time, socket. IO also supports multi-platform access, which can realize the purpose of using a set of framework for multiple platforms.