JavaEE & amp; HTML5 WebSocket

Source: Internet
Author: User

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

 

 

 

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.