webSocket agreement and Socket.IO

A, Http can not easily achieve real-time applications:

● HTTP protocol is stateless, the server will respond to requests from the client, but does not have a continuous connection between itself and the client. 
● We can very easily capture events occurring on a browser (such as the user clicks on the box), this event can easily generate data exchange with the server (such as Ajax). 
 However, the reverse is impossible: the occurrence of an event server, the server can not be real-time information about the event proactively notify its clients.
 Only the client query the current status of the server when the information before the event takes place from the server to the client.

Second, long polling, long connection

● long polling: every client a very short time, the server will send the request to see if there are new messages in rotation as long as the speed is fast enough, for example, one second, it can give the impression caused by the interaction is in real time. 
 This approach is quite upset, in fact, the server, the client both sides have caused a great deal of performance wasted. ● long connection: client requests only once, but the server keeps the connection does not return a result (imagine that we did not write res.end (), the browser has been to turn a small chrysanthemums).
 With the new data server, the data will be sent back, there has been new data, the data will be sent back, but remain pending. This approach has also caused a lot of wasted performance.

Three, WebSocket protocol

● latest HTML5 protocol, developed WebSocket protocol standard that allows clients and servers to communicate in full duplex manner. 
● WebSocket principle is very simple: the use of HTTP requests generated handshake after handshake, the two turn to communicate (QQ protocol) using TCP protocol. 
● Use WebSocket protocol, you need a browser and server support can be used. 
● support WebSocket protocol browsers: Chrome 4 , Firefox 4, IE10, Safari5 
● support WebSocket protocol server has: the Node 0, Apach7.0.2, Nginx1.3

Four, Socket.IO

  Official website address: https://socket.io/docs/rooms-and-namespaces/

● Node.js from the date of birth, support WebSocket protocol. However, to build a Socket server from the ground step by step very hard (imagine Node write a static file services are so hard). Therefore, a large God help us write a library Socket.IO. 
● Socket.IO is the industry's conscience, novice gospel. It shields the bottom of all the details, so the top-level call is very simple. And also does not support the browser WebSocket protocol, provides a transparent mechanism simulation long polling. 
● Node single-threaded, non-blocking the I / O, event-driven mechanism, making it ideal for Socket server.

Five, Socket.IO configuration

  1, the server-side configuration

● After creating out server, the statement
 var io = the require ( 'Socket.IO' ) (Server); 
can instantiate an object io, then the URL /socket.io/ socket.io.js default will be a static file js service. 
● Listener: 
io.on ( "Connection", function (Socket) { 
}); 
● Socket method and emit on a subject methods. emit method for sending a custom event, on the method used to customize an event listener from the server sent.

  2, the client configuration

● HTML page must be running on the server, not local. 
● HTML page needs to reference /socket.io/ socket.io.js file, and then execute io () function, and get socket object. 
● There emit methods and on methods socket object. emit method for sending a custom event, on the method used to customize an event listener from the server sent.

    3, express and Socket.IO

● Express frame can be used with Socket.IO, but not as a listening with the usual image app.listen Express program, instead of using a fixed pattern: 
    var = Express the require ( 'Express' ); 
    var App = Express (); 
    var = the require HTTP ( 'HTTP' ) the .server (App);  var = IO the require ( 'Socket.IO' ) (HTTP); 
 http.listen (3000);

 

Guess you like

Origin www.cnblogs.com/angelatian/p/11076855.html