Introduction to the WebSocket Communication Protocol API

Source: Internet
Author: User

WebSocket is a new communication protocol HTML5, the current popular browser supports this protocol, such as Chrome,safari,firefox,opera,ie, and so on, the earliest support for the protocol should be Chrome, Since the CHROME12 has started to support, as the draft agreement changes, the implementation of the various browsers to the protocol is constantly updated. The agreement is still a draft, not a standard, but to become the standard should be only a matter of time, from the proposed websocket to now have more than 10 versions, the latest is version 17, the corresponding version number of the protocol is 13, the current support for the most comprehensive browser is chrome, after all The WebSocket Draft agreement is also published by Google.

1. Introduction to WebSocket API

First look at a simple JavaScript code that calls the WebSockets API.

var ws = new WebSocket ("WS://echo.websocket.org");

Ws.onopen = function () {ws.send ("test!");};

Ws.onmessage = function (evt) {console.log (evt.data); Ws.close ();};

Ws.onclose = function (evt) {console.log ("websocketclosed!");};

Ws.onerror = function (evt) {console.log ("websocketerror!");};

This code is only 5 lines in total, and now briefly outlines the meaning of these 5 lines of code.

The first line of code is to request a WebSocket object, the parameter is the server-side address that needs to be connected, the same as the HTTP protocol use//start, the URL of the WebSocket protocol uses ws://beginning, and the other secure WebSocket protocol uses wss:// Beginning.

The second line to the fifth behavior WebSocket object Registration Message handler function, WebSocket object supports altogether four messages OnOpen, OnMessage, OnClose and OnError, when browser and Websocketserver are connected successfully , the OnOpen message is triggered, and if the connection fails, the send, receive data fails, or the processing data error occurs, browser triggers the onerror message, and when browser receives the data sent by Websocketserver, the onmessage message is triggered. , the parameter evt contains the data transmitted by the server, and the OnClose message is triggered when browser receives a close connection request sent by the Websocketserver side. We can see that all actions are triggered by the message, so that the UI is not blocked, and the UI has a faster response time and a better user experience.

Introduction to the WebSocket Communication Protocol API

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.