JavaEE & amp; HTML5 WebSocket
1. Workflow
(1) create a WebSocket instance on the Java Server and maintain and wait in the Tomcat container.
(2) create a client WebSocket instance in the browser, and then connect to the server. If the connection is successful, 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) when the Server onMessage event is triggered, parse the Message content and perform corresponding logical operations, return the result to the browser session. getBasicRemote (). sendText (Server Received Ed Stop Message !); .
(5) trigger the browser onmessage event, parse the message content, and perform logical operations.
(6) loop (3 )~ (5 );
(7) Close the connection.
2. W3C 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; readonly attribute unsigned long bufferedAmount; // networking: Network Operation attribute EventHandler onopen; // when the Browser and WebSocketServer are connected successfully, the onopen message attribute EventHandler onerror will be triggered; // if the connection fails, the sending or receiving data fails, or an error occurs during data processing, the browser will trigger the onerror message attribute EventHandler onclose; // when Browser receives a closed connection request sent by WebSocketServer, The onclose message readonly attribute DOMString extensions; readonly attribute DOMString protocol is triggered; void close ([Clamp] optional unsigned short code, optional DOMString reason); // close the connection // messagingattribute EventHandler onmessage; // when the Browser receives the data sent by WebSocketServer, the onmessage will be triggered. The evt parameter contains the attribute BinaryType binaryType transmitted by the server; void send (DOMString data); // send the string data void send (Blob data ); // send Binary Large Object void send (ArrayBuffer data); // ArrayBuffer is used to represent a string of bytes: http://technet.microsoft.com/zh-cn/ie/br212474void send (ArrayBufferView data ); // binary types in JavaScript, such as Blob, ArrayBuffer, and ArrayBufferView };
3. Java Server
Select an appropriate tool:
Apache Tomcat Version 7.0.57, focusing on websocket-api.jar
Java version 1.7.0 _ 71