Talking about WebSocket

Source: Internet
Author: User

WebSocket is a method proposed by H5 to resolve client and server duplex communication. When we communicate using the HTTP protocol, the client actively sends a request to the server to get the server-side data. To be consistent with server-side data, there are two common ways of polling and long polling. Polling is the periodic request of data to the server at intervals, most of which are useless requests that consume serious network resources. A long poll is a request that is sent until the server returns the updated data before disconnecting the connection. For frequent server-side updates, the performance of long polling is similar to polling.

WebSocket a bit is, do not wait for the client to request, whenever the server has data updates, it will actively send the data to the client. It uses the WebSocket protocol and the network address is ws://, not http://. Since it is the technology proposed by H5, there must be a compatibility problem. IE10 above support. A simple detection browser supports the way that the technology is not supported by judging window. Websocket=undefined or not. Here is a relatively good article:http://blog.csdn.net/neco2011/article/details/52766082?hmsr=toutiao.io&utm_medium= Toutiao.io&utm_source=toutiao.io . This article describes how to use the browser's own interface:

if (window. WebSocket) {

var socket = new WebSocket ("ws://localhost:6677"); Establish a connection

Socket.onopen = function () {}//Connection establishment Event

Socket.close ()//Disconnect

Socket.onclose = function () {}//connection Shutdown Event

Socket.send ("xxxxx")//Send Message

Socket.onmessage = function () {}//Event receiving message

}

The Socket.readystate =0,1,2,3 respectively corresponds to the connection being connected, the connection is successful, shutting down, and has been closed in four states.

WebSocket can completely replace Ajax. Send is the Ajax sending request, and OnMessage is receiving the response.

Server-side use of WebSocket also requires server mates. Node provides WS and Socket.io are more commonly used. Both libraries actually provide the server and client methods. Especially Socket.io, the browser compatibility has been processed, for browsers that do not support WebSocket, can automatically downgrade to Ajax.

It took about two days to implement a simple chat room application, which was to use the Socket.io module. The biggest experience is that websocket eliminates the hassle of sending periodic requests from the front end, whenever there is an update, you can actively send updates from the background (for example, the user in which space and spaces, etc.). And Socket.io provides the namespace and the concept of the room is very practical, you can easily solve the different spaces or rooms of the user chat non-interference problems. Finally, the project address is attached, please advise. Https://github.com/yinggeABC/ChatGroup

Talking about WebSocket

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.