Js--websocket Getting Started

Source: Internet
Author: User

Websocket

1.websocket What is it?

WebSocket is the technology that is generated to address real-time communication between the client and the server. The essence is to create a TCP connection for exchanging data after shaking hands with the HTTP/HTTPS protocol.

The server then communicates with the client in real time through this TCP connection.

2.websocket The advantages

In the past, we implemented push technology, using polling, in the feature of the time interval has the browser automatically make the request, the server's message pull back actively, in this case, we need to constantly send requests to the server, however, the header of the HTTP request is very long, The data contained in it may be just a small value, which can consume a lot of bandwidth and server resources. Will consume a lot of bandwidth and server resources.

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. After the connection is established, the server can proactively transmit data to the client.

In addition, the header information exchanged between the server and the client is small.

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;

For a detailed introduction of Ajax,comet,websocket, and the introduction of WebSocket message, you can see http://www.shaoqun.com/a/54588.aspx web design]ajax, Comet and WebSocket ,

I would have written it if I had the time.

3. How to use websocket

Client

In a browser that supports WebSocket, after the socket is created. You can respond to a socket with onopen,onmessage,onclose, or onerror four events

A simple is an example

var New WebSocket ("WS://localhost:8080");  function() {  console.log ("open");   function (evt) {  function(evt) {  console.log ("websocketclosed!  function (evt) {  console.log ("Websocketerror!");};

1.var ws = New WebSocket ("ws://localhost:8080");

To request a WebSocket object, the parameter is the server-side address that needs to be connected, as with the HTTP protocol, the URL of the WebSocket protocol starts with ws://, and the security WebSocket protocol uses wss://.

Ws.send ("Hello");
Used to send messages to the server
2.ws.onopen = function () {Console.log ("open")};

The OnOpen event is triggered when WebSocket is created successfully
3.ws.onmessage = function (evt) {Console.log (evt.data)};
The OnMessage event is triggered when the client receives a message from the service side, and the parameter Evt.data contains the data transmitted by the server.
4.ws.onclose = function (evt) {console.log ("websocketclosed!");};

OnClose event is triggered when a client receives a request to close a connection sent by the server
5.ws.onerror = function (evt) {console.log ("websocketerror!");};
OnError event is triggered when a connection, processing, receiving, sending data fails
We can see that all actions are triggered by events, so that the UI is not blocked and the UI has a faster response time and a better user experience.

Turn https://www.cnblogs.com/lxtblogs/p/4947898.html

Js--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.