HTML WebSocket Notes

Source: Internet
Author: User

WebSocket Introduction

WebSocket is a protocol that HTML5 begins to provide full-duplex communication on a single TCP connection.

In the WebSocket API, the browser and server only need to do a handshake, and then a fast channel is formed between the browser and the server. The data can be transmitted to each other directly between the two.

The browser sends a request to the server to establish a WebSocket connection through JavaScript, and after the connection is established, the client and server can exchange data directly via a TCP connection.

When you get a Web Socket connection, you can send data to the server via the Send () method and receive the data returned by the server through the OnMessage event.

Service-side Open source project

Websockets/ws

Nodejs project, test pass.

Tootallnate/java-websocket

Java project, test pass.

Theturtle32/websocket-node

dependent on the Python/vs C + + development environment, the test run passed.

WebSocket API parsing

WebSocket Property

WebSocket Events

WebSocket method

/** * websocket Test * http://www.runoob.com/html/html5-websocket.html*/varws;/** * Connect websocket server*/functionWebsocketconnect () {if("WebSocket"inchwindow) {ws=NewWebSocket ("ws://localhost:9003"); Ws.onopen=function() {Console.log ("WebSocket Server connected ...")        }; Ws.onmessage=function(res) {varmsg =Res.data;        Console.log (msg);        }; Ws.onerror=function() {Console.log ("Connect error ...");        }; Ws.onclose=function() {Console.log ("Disconnect ...");    }; }}/** * Send data*/functionsendmsg () {if(WS) {ws.send ("0xaa,0xbb"); }}/** * Close connection*/functionclosews () {if(WS) {ws.close (); WS=NULL; Console.log ("Closed ..."); }}

Reference:

HTML5 WebSocket

WebSocket Introduction (ii)-websocket API

WebSocket Study (iii)--build a server with Nodejs

HTML WebSocket Notes

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.