WebSocket Getting Started

Source: Internet
Author: User
Tags event listener

A:websocket introduction

WebSocket is a natural, full-duplex, bidirectional, single socket connection. With WebSocket, your HTTP request becomes a single request to open the WebSocket connection (websocket or websocket over TLS (Transport layer Security, Transport layer safety, formerly "SSL"), and reuse the same connection from the client to the server and from the server to the client.

The WebSocket API is the next generation of client-server asynchronous communication methods. This communication replaces a single TCP socket, using the WS or WSS protocol, which can be used with any client and server program. WebSocket is currently standardized by the consortium. WebSocket has been supported by browsers such as Firefox 4, Chrome 4, Opera 10.70, and Safari 5.
The great thing about the WebSocket API is that servers and clients can push information to each other at any point within a given time frame. WebSocket is not limited to communicating in Ajax (or XHR), because Ajax technology requires client-initiated requests, and WebSocket servers and clients can push information to each other, xhr are constrained by domains, and WebSocket allows cross-domain communication. Ajax is a clever thing to do without designing the way to use it. WebSocket is created for the specified target and is used for two-way push messages.

WebSocket reduces latency because once the websocket connection is established, the server can send messages as they become available. For example, unlike polling, WebSocket only makes one request. The server does not need to wait for requests from the client. Similarly, clients can send messages to the server at any time. A single request greatly reduces latency when a request is sent every other time, regardless of whether a message is available or not.

The polling and websocket scenarios are compared.



second, the use of WebSocket API
focus only on the client's API, because each server-side language has its own API. The following code snippet is to open a connection, create an event listener for the connection, disconnect, message time, send a message back to the server, close the connection.
//Create a Socket instance
var socket = new WebSocket (' ws://localhost:8080 ');
//Open socket
Socket.onopen = function (event) {
//Send an initialization message
socket.send (' I am the client and i\ ' M listening! ');
//Listening messages
socket.onmessage = function (event) {
Console.log (' Client received a message ', event);
  };
//Monitor the closing of the socket
socket.onclose = function (event) {
Console.log (' Client notified socket has closed ', event);
  };
//Close socket ....
//socket.close ()
};
Let's take a look at the initialization fragment above. The parameter Url,ws represents the WebSocket protocol. The OnOpen, OnClose, and OnMessage methods connect events to the socket instance. Each method provides an event to indicate the status of the socket.

WebSocket Getting Started

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.