Use socket.io and nodejs to build a websocket server
socket.io can not only build client websocket services, but also support nodejs server-side websockets.
Let me introduce how to install and configure nodejs.
Enter http://nodejs.org/#download to download the msi file. Click next to install. Finally, the file will be automatically installed in the C:/nodejs directory.
After the installation is completed, the environment environment variables will be automatically configured. If there is no automatic configuration, add;C:/nodejs/ to the path yourself.
After the installation is completed, npm needs to be configured to manage node.js modules.
Installing npm under window requires git to be installed.
After installing git, open gitbush. Perform the following steps:
git config --system http.sslcainfo /bin/curl-ca-bundle.crtgit clone --recursive git://github.com/isaacs/npm.gitcd npmnode cli.js install npm -gf
The first is that there will be no prompts for setting up. The second step will go to github to download npm and will have download files and progress. The fourth step is that installing npm to node.js will copy several files cmd files and mode_modules folder to nodejs directory.
This way, npm is configured.
If you need to install any module, enter npm install *** directly.
If you don’t have npm or Windows users can use github to download socket.io and put it into the node_modules folder. For specific configuration, please refer to the article: "Nodejs Tutorial: Configuring the Windows Directory Structure of Nodejs.exe"
nodejs installation socket.io
Use the node plug-in management package, run the following command to successfully install socket.io
npm install socket.io
An example of implementing using socket.io
Client code:
<html> <head> <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) {//Receive the data named 'new' console.log(data.hello);//Data is the data sent by the server. socket.emit('my new event', { my:'new data' });//Send data to the server to realize two-way data transmission}); socket.on('other', function (data) {//Receive another data named 'other', console.log(data.hello); socket.emit('event1', { my:'other data' }); }); } </script> </head> <body> <button id='btn' onclick="doit()">click me</button> </body> </html>socket.io.client.js can be downloaded locally at https://github.com/LearnBoost/socket.io-client, and point to the native js library in <script src="..">.
Server is implemented using nodejs
server2.js
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, data is sent, the first parameter 'new' is the data name, and the second parameter is the data socket.on('my other event', function (data) {//Capture the client sends the name 'my other event's data 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 can be displayed normally by the client
The server side displays the results:
C:/java/Nodejs>node server2.js
Note: The code should be in the same directory as npm_module. Otherwise, the socket.io module will not be found.