WebSocket can not only do chat applications in practical applications, but also use websocket long connections to keep data real-time and information push.
Key points of implementation of WebSocket
First: First you need to introduce the Java-websocket.jar package, here I use the version when 1.3.1.
The next step is that different businesses have different needs, and we encapsulate WebSocket requests based on business needs.
Instance WebSocket object in Runable, and build your business logic in four back-off functions,
The Send method is then defined for sending the request
, define an interface for receiving data returned by the server.
Other examples, such as Destory and threading methods, are not much more.
Second: Because we need to websocket to maintain the connection with the server, not only when the program is running, when the lock screen and the background of the program need to get the server push information, this time we need to run WebSocket on the service.
Service-to-program interaction is done by broadcasting. Register a broadcast receiver and broadcast in the service class. After the data is received, it is processed in the OnMessage method, and then the broadcast is sent to the client.
Next we need to define an intermediate broadcast class to receive the information returned by the service to the page, and an interface for the data acquisition of the page
To this overall idea and completion.
The use of the words is very simple, first instance an intermediate broadcast class object and then send the request, in implementing the interface
Used to process the corresponding business logic.
Android WebSocket App