Socket. Io is a websocket library that includes both the client's JS and the server-side Nodejs. Official address: Http://socket.io
use Socket.io and Nodejs to build WebSocket server side
Socket.io can not only build the client's WebSocket service, but also support Nodejs server-side websocket.
let me explain how to install configuration Nodejs.
Enter the http://nodejs.org/#download下载msi文件. Always click Next to install. The final file is automatically installed in the C:\nodejs directory.
When the installation is complete, the environment environment variables are automatically configured. If there is no automatic configuration, you manually add the path; C:\nodejs\.
After the installation is complete, NPM needs to be configured to manage node. JS modules.
Installing NPM under Windows requires Git to be installed.
When you're done installing git, open gitbush. Perform the following steps:
git config--system http.sslcainfo/bin/curl-ca-bundle.crt
git clone--recursive git://github.com/isaacs/npm.git
CD NPM
Node Cli.js install NPM-GF
The first is that the settings will not have any hint, the second step will be downloaded to github on the download file and progress, the fourth step is to install NPM to node. JS will copy several file cmd files and Mode_modules folder to the Nodejs directory.
This configures NPM.
If you need to install what module directly enter NPM Install * * *.
No NPM or Windows users can use GitHub to download Socket.io and put it into the Node_modules folder, specific configuration can refer to the article: "Nodejs Tutorial: Configuring the Nodejs.exe Windows directory Structure"
Nodejs Installation Socket.io
Using the node plugin Management Pack, run the following command to install the success Socket.io
NPM Install Socket.io
An example of implementation with Socket.io
Client code:
HTML Code Collection code
<title></title>
<script src= ". /js/socket.io.client.js "></script>
<script type= "Text/javascript" >
function doit () {
var socket = io.connect (' http://localhost ');
Socket.on (' news '), function (data) {//received the data sent by the server called ' new '
Console.log (Data.hello);//data is the data sent over the server.
Socket.emit (' My new event ', {my: ' New Data '});//Send data to the server for two-way data transfer
});
Socket.on (' Other ', function (data) {//receives another data called ' other '),
Console.log (Data.hello);
Socket.emit (' Event1 ', {my: ' Other data '});
});
}
</script>
<body>
<button id= ' btn ' onclick= "doit ()" >click me</button>
</body>
Socket.io.client.js can https://github.com/LearnBoost/socket.io-client download to local, <script src= "..." > JS library pointing to this machine.
server implemented with Nodejs
Server2.js
JavaScript code
var http= require (' http '), io= require (' Socket.io '), express= require (' Express ');
var app = Express.createserver (), io = Io.listen (APP);
App.listen (80);
Io.sockets.on (' Connection ', function (socket) {
Socket.emit (' News ', {hello: ' world ');//Listen, once the client is connected, that is, the first parameter ' new ' is the data name, and the second parameter is both a data
Socket.on (' My other event ', function (data) {//Capture client sends data named ' My other event '
Console.log (data.my);
});
Socket.emit (' other ', {hello: ' other World '});//Send another data
Socket.on (' Evnet1 ', function (data) {//Capture another data
Console.log (data.my);
});
});
Test results, the client can display normally
Server-side Display results:
C:\java\nodejs>node Server2.js
Note: The code should be in the same directory as the Npm_module. Otherwise, you may not find the Socket.io module error.