Socket. IO is a powerful framework that helps you build real-time applications across browsers Based on WebSocket. Supports mainstream browsers, multiple platforms, and multiple transmission modes. You can also combine the Exppress framework to build complex real-time applications with various functions ....,. Socket. IO is a powerful framework that helps you build real-time applications across browsers Based on WebSocket. Supports mainstream browsers, multiple platforms, and multiple transmission modes. You can also combine the Exppress framework to build complex real-time applications with various functions.
Example
1. Use the Node HTTP Server
Server Sample Code:
Var app = require ('HTTP '). createServer (handler)
, Io = require ('socket. io '). listen (app)
, Fs = require ('fs ')
App. listen (80 );
Function handler (req, res ){
Fs. readFile (_ dirname + '/index.html ',
Function (err, data ){
If (err ){
Res. writeHead (500 );
Return res. end ('error loading index.html ');
}
Res. writeHead (200 );
Res. end (data );
});
}
Io. sockets. on ('connection', function (socket ){
Socket. emit ('new', {hello: 'World '});
Socket. on ('My other event', function (data ){
Console. log (data );
});
});
Client sample code: