WebSocket and node. js

Source: Internet
Author: User

The development of web technology over the past few days can be described as a revolutionary development, not only limited to the ajax asynchronous refresh layer of web. I personally think that the technologies that can bring revolutionary significance after HTML5 mainly lie in the support of WebGL and local operations. However, the development of web not only exists with clients, but also changes in server technologies, such as Websocket and node. js represents it. The real-time and asynchronous web services bring about a revolution no less than a client revolution.

The general web model is based on get and post, and the starting point of all actions is only web pages.

What if the server needs to actively push data to the client? There are two methods: client polling and persistent connection.

Client polling is implemented by frequent page swiping before ajax is widely used. The performance and user experience are naturally much inferior. Fortunately, js asynchronous response is prompted after ajax, however, the client needs a special scheduled event to capture server data, and the performance is not very good. The persistent connection mode requires the collaboration between the server and the client. The client initiates a persistent connection, and the server cannot be disconnected. The communication channel is maintained. However, because of the existence of this channel, the request cannot end, the page is always in the request State and cannot end. Therefore, a hidden frame is added to maintain the connection without affecting the loading process of the home page, but it is subject to cross-origin restrictions. This cannot fundamentally solve the problem. The technology of websocket came into being. Of course, browser support is required first. Websocket is a technology for asynchronous communication between the client and the server. It is built on the http protocol. In client programming, you don't need to do too many operations, you only need to use WebSocket objects. It includes three types of events: Opening a connection, reaching a message, closing a connection, and sending data. As follows:

Var socket = new WebSocket ("ws: // localhost: 80") // Note: websocket uses ws instead of httpsocket. onopen (event) socket. send () socket. onmessage () socket. onclose ()

Client programming is very easy. What about the server?

The websocket protocol requires a handshake process:

Client: GET/call HTTP/1.1 Upgrade: WebSocketConnection: UpgradeHost: localhost: 8080 Origin: http: // localhostCookie: somenterCookie server: HTTP/1.1 101 Web Socket Protocol HandshakeUpgrade: WebSocketConnection: upgradeWebSocket-Origin: http: // localhostWebSocket-Location: ws: // localhost: 8080/call

The number of communications starts with "\ x00" and ends with "\ xFF. The specific data sending and receiving process is no different from the general communication process.

These standards make it necessary for us to redo some work on the server. Many people have implemented this and provided it for your use.

Why do we mention node. js? Node. js is a server-side javascript interpreter. In addition to its simple language and easy event-driven implementation, its real advantage lies in its easy implementation of Asynchronization. He runs on the basis of a V8 engine. Theoretically, this model can be implemented in any language and has already been implemented in C ++ and C.

The following is a node. js helloworld:

var sys = require("sys"),    http = require("http");http.createServer(function(request, response) {    response.sendHeader(200, {"Content-Type": "text/html"});    response.write("Hello World!");    response.close();}).listen(8080);sys.puts("running...");

The development of web is still very good.

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.