Android uses WebSocket to implement group chat and message push (without using WebView) _android

Source: Internet
Author: User
Tags rfc java web

WebSocket protocol is HTML5 a new protocol. It implements the browser and server Full-duplex communication (Full-duplex). WebSocket is a new product of the Web2.0 era, used to make up for some deficiencies in the HTTP protocol, but the real relationship between them is a brotherly relationship, is the further encapsulation of the socket, its current most intuitive performance is the server push and chat function. More knowledge Reference: How to understand the relationship between TCP/IP, SPDY, WebSocket?

Today's focus is on how to get rid of WebView using WebSocket in Android, not in a Web browser, and if you're using it in a Web browser, there are too many tutorials, frameworks, and demos on the Internet that you don't need to talk about.

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

First, Android client creation (using Java-websocket Library):

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

   

2, specifies the ip/domain name and the port connection server, when the server side has the notification to be able to callback OnMessage method

    

3, and then call the Connect method for the connection

   

4, after the connection can send a message, send a message is also very simple, in addition to support string sent also support byte send, OK, the client is so happy to write (detailed code see behind packaged demo).

   

Second, the creation of the service side:

1-1, Java Application Server creation (using the Java-websocket Library), in fact, is also very simple, inherit 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 the push, it is too simple.

   

2-1, Java Web (Tomcat) server-side creation, not using the Java-websocket library, directly using the Java API Javax.websocket package WebSocket related classes (note that the Java API only implements the standard RFC 6455 (JSR256), if you have to choose other early drafts to be implemented with Java-websocket, in Java-websocket, the Connection Agreement "DRAFT_17" is the standard RFC 6455 (JSR256), in addition to the use of Java API Javax.websocket package in the WebSocket related class requirements JDK7 and above, Tomcat 7.0.49 and above):

2-2, and then start Tomcat can be happy to use the Android client to connect to chat, receive the push.

  Third, related screenshots:

1-1, Java background:

  

1-2, Java background corresponding to the Android client

  

2-1. Java Web Background:

  

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

  

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

  

The above through an illustrated way to introduce the Android using WebSocket to achieve group chat and message push function (do not use webview), I hope to help you!

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.