Implementation of Android socket chat room function
Premise summary
The author actually learned socket a long time ago. Of course, I also used socket as a chat room, but I think this knowledge point is relatively general and there are no particularly difficult technical points, so I didn't go deep into it. However, the use of socket in a recent project makes the author feel that socket is extremely powerful and can realize many functions.
Personal socket experience
The project is mainly related to smart home. Multiple mobile phones need to operate the lights (on or off) at the same time. The main points are as follows:
1. Get the status of all lights when entering the interface. 2. One mobile phone changes the state of the light, which can be displayed on other mobile phones. 3. The status of the light is changed in the hardware (manually switch the light), and it should be displayed on all mobile phones.
This function is not appropriate if it is implemented by HTTP reading. On the one hand, it is difficult to ensure the synchronization of files read by the client and the server. Even if it is guaranteed, it also needs to waste a lot of performance; On the other hand, similar to the project function of the author, the server and client interact more frequently and have high requirements for "immediacy". Using HTTP not only consumes too much performance, but also is difficult to ensure "immediacy".
However, it is easy to implement using socket. The main logic is as follows:
1. Each time you enter the interface, establish a socket connection with the server and get the status of the lamp at this time. 2. Each time you need to operate the lamp, establish a thread to transfer the status of the lamp to the server. After receiving it, the server will transfer the status to each client that has established a connection with the server at this time.
This experience also reminds me of a pen test question made by the senior student. The questions are as follows:
Open the account of Taobao web page and mobile version at the same time, and the mobile phone stays in the shopping cart interface. At this time, an item will be added to the shopping cart on the web page. How can the design make the mobile phone automatically refresh the shopping cart.
If you use socket, I believe it is a good idea.
OK, let's get to the point and show the socket chat room demo.
Effect (source code at the end of the article)
Main ideas
Android
1. After entering the interface, the client establishes a socket with the server. At the same time, a thread is started to receive messages sent by the server all the time. 2. Each time you click button to get the string in EditText, call the sub thread to send the string to the server.
The server
1. Create an ArrayList storage socket. 2. The client that circularly receives the request to access the port, after receiving it, stores the socket in the ArrayList, and opens a thread for each socket for communication. 3. The logic of each socket thread is as follows: loop to receive the message sent by the client. After receiving it, use the previous ArrayList to send it to each client. If a client returns a null value or cannot send the past, it indicates that the client has been disconnected and is removed from the ArrayList.
code
(learn from Android crazy handout)
Android
Don't forget to add access to the network in Android manifest
MainActivity:
ClientThread
activity_ main
The server:
MySever
SeverThread
Source address: Android socket chat room
The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.