WebSocket Protocol Introduction
The WebSocket protocol is a network protocol that allows two connected ends to have full-duplex message communication on a single TCP connection.
In the WebSocket scenario, the connection is established in a way that interacts with HTTP and websocket endpoints. The connected party sends a specially crafted HTTP request to launch the URL that contains the websocket it wants to connect to. It starts the activity and is called the Open stage handshake . If the server is willing to accept the connection, the server formulates a special HTTP response called the open phase handshake response and sends it to the client. The TCP connection is now established to guarantee the round-trip delivery of the websocket message. The connection remains until either side terminates the connection, or some external factors cause the connection to close.
The Java Web Socket API is a core feature of the recent JavaEE7 platform, so users who are not deployed with Tomcat should be aware of relying on two of packages
<dependency><groupid>org.apache.tomcat</groupid><artifactid>tomcat-websocket-api</ artifactid><version>7.0.47</version><scope>provided</scope></dependency>< Dependency><groupid>javax</groupid><artifactid>javaee-api</artifactid><version >7.0</version><scope>provided</scope></dependency>
Create the first WebSocket application, deployed in Tomcat7 and later
Service-side code: Echoserver.java
Package Org.mychat.websocket.server;import Javax.websocket.onmessage;import Javax.websocket.server.ServerEndpoint ;/** * @author Chensheng * Note @serverendpoint is a class-level annotation, which is used to tell the Java platform that the class it annotations is actually called a websocket endpoint */@ServerEndpoint ("/mychat.ws") public class Echoserver {@OnMessage public string echo (string incomingmessage) {System.out.println ("message Received:" + incoming Message); Return "I got this (" + Incomingmessage + "), so I am sending it back";}}
Client code Websocket_client.js + index.html
Var echo_websocket;function init () { output = document.getelementbyid ("Output");} Function send_echo () { var wsuri = "ws://127.0.0.1:8080/mychat/mychat.ws"; Writetoscreen ("connecting to " +wsuri); echo_websocket = new websocket (WsUri); echo_websocket.onopen = function (evt) { writetoscreen ("Connected"); Dosend (document.getElementById ("Textid"). Value); }; echo_websocket.onmessage = function ( EVT) { writetoscreen ("received message: " +evt.data); echo_websocket.close (); }; echo_websocket.onerror = function (evt) { writetoscreen ("<span style= ' Color:red; ' >ERROR:</span> " + evt.data"; echo_websocket.close (); };} Function dosend (message) { echo_websocket.send (message); writetoscreen ("send message: " + message);} Function wriTetoscreen (message) {// console.log ("received message:" +message); var pre = Document.createelement ("P"); pre.style.wordwrap = "Bread-word"; pre.innerhtml = Message; output.appendchild (pre);} Window.addeventlistener ("Load", init, false); <! doctype html>Operation Result:
650) this.width=650; "title=" run result. PNG "src=" Http://s4.51cto.com/wyfs02/M02/82/74/wKiom1dVnlagJXPcAADNFw95pWM133.png-wh_500x0-wm_3-wmp_4-s_ 1230434713.png "alt=" Wkiom1dvnlagjxpcaadnfw95pwm133.png-wh_50 "/>
Although very simple, however, we have already had close contact with WebSocket.
This article is from the "Ah Cool blog source" blog, please make sure to keep this source http://aku28907.blog.51cto.com/5668513/1786772
Java WebSocket Fundamentals