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