Use socket. io and node. js to build a timely communication system, socket. ionode. js

Source: Internet
Author: User
Tags emit

Use socket. io and node. js to build a timely communication system, socket. ionode. js
Socket. IO is a websocket library, including the client js and the server nodejs. Official Address: http://socket.io

Use socket. io and nodejs to build a websocket Server

Socket. io can not only build the websocket service of the client, but also support the websocket of the nodejs server.

Next let me introduce how to install and configure nodejs.

Go to http://nodejs.org/javasdownloadto download the msifile. Always click next to install. The final file is automatically installed in the C: \ nodejs directory.

After the installation is complete, environment variables are automatically configured. If no automatic configuration is available, manually add C: \ nodejs \ in the path \.

After the installation is complete, you need to configure npm to manage the node. js module.

Install git to install npm in window.

After 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 one is that there is no prompt for the settings. The second step will go to github to download npm files and progress, and the fourth step is to install npm to node. js will copy several cmd files and the mode_modules folder to the nodejs directory.

In this way, npm is configured.

If you need to install any modules, enter npm install ***.

If there is no npm or windows users can use github to download socket. io and put it into the node_modules folder. For detailed configuration, refer to: nodejs Tutorial: configure the windows directory structure of nodejs.exe.

Install socket. io in nodejs

Use the node plug-in management package and run the following command to install socket. io successfully.

Npm install socket. io

An example of socket. io implementation

Client code:
Add Html code to favorites
<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 ('new', function (data) {// receives the data sent by the server named 'new '.
Console. log (data. hello); // data is the data sent from the server.
Socket. emit ('My new event', {my: 'new data'}); // send data to the server for Bidirectional data Transmission
});
Socket. on ('other', function (data) {// receives 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 to the local js library at https://github.com/learnboost/socket.io-client, pointing to the local js library in <script src = "..">.

The server is implemented using 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'}); // listener. Once the client is connected, data is sent. The first parameter 'new' is the data name, the second parameter is Data
Socket. on ('My other event', function (data) {// capture the data sent by the client 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 );
});
});

The test result is displayed on the client.


Result displayed on the server:

C: \ java \ Nodejs> node server2.js


Note: The code must be in the same directory as npm_module. Otherwise, the socket. io module cannot be found.



Nodejs socketio server-to-Server Communication

When the server receives the message, it can use emit to send it to the socket. io client?

Nodejs socketio serves as a server. How to Use C language as a client to communicate with the server

Socket. io and socket are not the same thing. You can forward them on the server, either using tcp or udp.
 

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.