Now I need to implement such a function: the android side and the server side need to keep connected, and the android side needs to send heartbeat every other period of time,

Reported answer
Now I need to implement such a function: the android side and the server side need to keep connected, and the android side needs to send heartbeat every other period of time,
Ask online customer service
Scan the code to ask online customer service

It is necessary to record the user's online time and departure time. Now I use the netty framework, but when the connection is successful, I cannot know the specific user ID (because the time needs to be recorded in the database), ask for some ideas, or have other more View All

  • Number of answers

    four

  • Number of browses

    8,759

4 answers Default sorting
  • Default sorting
  • Sort by time

Adopted
In order to maintain the connection between the Android side and the server side, WebSocket technology can be used. WebSocket is a protocol that implements full duplex communication in the browser, and can also be implemented on the Android side using the related SDK.
On the Android side, you first need to initiate a connection to the server through a network request and perform authentication. Then, a heartbeat message is sent to the server every once in a while. When you receive a response from the server, you can perform corresponding operations according to the response.
The specific implementation steps are as follows:
1. Send a connection request to the server and perform authentication.
2. Send a heartbeat message to the server every other period of time. The heartbeat message can be string data in JSON format.
3. When receiving a response from the server, perform corresponding operations according to the response.
4. If no response is received from the server for a long time, the connection is considered disconnected and the connection request is re initiated.
Pay attention to the following issues during code writing:
1. Whether the connection request is successful: If the connection request is not successful, you need to re initiate the connection request.
2. Heartbeat message content: Heartbeat message content needs to be designed according to specific business needs, and cannot be too frequent or too few.
3. Processing server response: When receiving the server response, you need to parse and process the data.
To sum up, the above steps and precautions are to maintain connection with the server and send heartbeat messages on the Android side. By using WebSocket technology, reliable data transmission and real-time communication can be realized.
cancel comment
Hello!

It's enough to build a machine

If you have any questions, please follow up.
cancel comment
Can't it be made into a url interface? Make a url interface and call the interface regularly.
cancel comment
Use a timer to send a data and receive a return. If there is no return or the return is incorrect, it is considered offline.........
cancel comment
ZOL Q&A > mobile phone > Other classifications > Now I need to implement such a function: the android side and the server side need to keep connected, and the android side needs to send heartbeat every other period of time,

report

Thank you for contributing to the harmony of the community. Please select the type of report

Reported successfully

It will be handled after verification
Thank you for your contribution to community harmony

Scan the code to participate in the trial of new products at 0 yuan
You can get the sheets and grand gifts on the top floor

 Scan and pay attention to us
Tips

Are you sure you want to cancel this registration and exit this activity?