Android off WebView using WebSocket for group chat and push features

Source: Internet
Author: User
Tags rfc java web

WebSocket is a new product of the Web2.0 era, used to compensate for some of the shortcomings of the HTTP protocol, but the real relationship between them is a sibling relationship, is a further encapsulation of the socket, its most intuitive performance is the server push and chat functions. For more information: How to understand the relationship between TCP/IP, SPDY, WebSocket?

Today's focus is on how to get rid of WebView in Android using WebSocket, instead of using it in a Web browser, if it is used in a Web browser, there are too many tutorials, frameworks and demos on the Internet, no need to speak.

So far I personally think that Android more useful websocketclient are: Autobahn, Androidasync, Java-websocket. Not easy to use actually need to see the actual demand, here I choose Java-websocket.

One, the creation of the Android client (using the Java-websocket Library):

1, in fact, only need to Master a class, websocketclient can

    

2. Specify ip/domain name and port connection server, callback OnMessage method when server side has notification

    

3. Then call the Connect method to connect

    

4, after the connection can send the message, send the message is also very simple, in addition to support string sent also support byte send, well, the client is so happy to write (detailed code see the following package of demo).

    

Second, the creation of the service side:

1-1,Java Application Server creation (using Java-websocket Library), in fact, is also very simple, inheriting a class Websocketserver:

      

1-2, and then in the main method to open the server, now you can use the Android client to connect to chat, receive push, it is too simple.
    

2-1,Java Web (Tomcat) server creation, here do not use the Java-websocket library , directly using the Java API Javax.websocket package WebSocket related classes (Note the Java API only implements the standard RFC 6455 (JSR256), if you want to select other early drafts you need to use Java-websocket to implement, in Java-websocket connection protocol "DRAFT_17" is the standard RFC 6455 (JSR256), in addition to using the Java API Javax.websocket package WebSocket related classes require JDK7 and above, Tomcat 7.0.49 and above):

    

2-2, then launch Tomcat can happily use the Android client to connect to chat, receive push.

Third, Related:

1-1. Java background:

  

1-2, the Java background corresponding to the Android client

  

2-1. Java Web Backend:

  

2-2, the Java Web background corresponding to the Android client

   

2-3. HTML front-end (browser):

  

  

  Demo code Download: WebSocket Chat test Demo.zip

Original essays, reproduced annotated source.

Reprint Note Address: http://www.chengxuyuans.com/Android/97798.html

Ask--One-click Programmer to answer the question artifact, one-man service for cattle, developer Programming Essentials official website: www.wenaaa.com

QQ group 290551701 gathers a lot of Internet elite, technical director, architect, project Manager! Open source technology research, Welcome to the industry, Daniel and beginners are interested in engaging in IT industry personnel to enter!

Android off WebView using WebSocket for group chat and push features

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.