1. Work Flow
(1) The Java server creates the WebSocket instance and maintains and waits in the Tomcat container.
(2) A client WebSocket instance is created in the browser, then the server is connected and if the connection succeeds, the server triggers the OnOpen event, and the client triggers the OnOpen event.
(3) The WebSocket instance in the browser sends a message, websocket.send (' * * * * * * *);
(4) The server OnMessage event trigger, parse the message content, and carry out the corresponding logical operation, return the result to the browser session.getbasicremote (). SendText ("Server Received Stop message!"); 。
(5) The browser OnMessage event trigger, parse the message content, carry on the logical operation.
(6) cycle (3) ~ (5);
(7) Close the connection.
2. WebSocket interface
Enum Binarytype {"blob", "Arraybuffer"}; [Constructor (domstring URL, optional (domstring or domstring[]) protocols), Exposed=window,worker]interface WebSocket: eventtarget {readonly attribute domstring url;//ready state: Status Code const unsigned short connecting = 0;const unsigned short OPEN = 1;const unsigned short CLOSING = 2;const unsigned short CLOSED = 3;readonly attribute unsigned short readystate;re adonly attribute unsigned long bufferedamount;//networking: Network operation attribute EventHandler OnOpen; When the browser and Websocketserver are connected successfully, the OnOpen message attribute EventHandler onerror is triggered; If the connection fails, sending, receiving data fails, or processing data error, browser will trigger OnError message attribute EventHandler OnClose; OnClose message is triggered when browser receives a close connection request sent by the Websocketserver side readonly attribute domstring extensions;readonly attribute Domstring protocol;void Close ([Clamp] Optional unsigned short code, optional domstring reason); Close connection//Messagingattribute EventHandler onmessage; The onmessage message is triggered when the browser receives the data sent by Websocketserver, and the parameter evt contains the data transmitted by the server atTribute binarytype Binarytype;void Send (domstring data); Send string data void Send (Blob data); Send binary large object void Send (ArrayBuffer data); Arraybuffer is used to denote a string of bytes:http://technet.microsoft.com/zh-cn/ie/br212474void send (Arraybufferview data); Binary types in JavaScript such as BLOBs, Arraybuffer, and Arraybufferview};
http://dev.w3.org/html5/websockets/
3. Java Service side
Choose the right tool:
Apache Tomcat Version 7.0.57, with a focus on Websocket-api.jar
Java Version "1.7.0_71"
Example, reference: http://blog.csdn.net/javascriptcoder/article/details/42913561
Java EE & HTML5 websocket