Android WebSocket App

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.