Node. js uses the socket. io method, node. jssocket. io

Source: Internet
Author: User

Node. js uses the socket. io method, node. jssocket. io

You can use socket. io to create a socket. io server. However, this server depends on a created http server.

After the http server is running, use the listen method to append a socket. io server to the http server.

Copy codeThe Code is as follows:
Var sio = require ("scoket. io ");
Var socket = sio. listen (server );

Socket is a socket. io server created on the server.

When a connection is established between the client and the server, the connection event of socket. io service is triggered.

Copy codeThe Code is as follows:
Socket. on ("connection", function (socket ){
});

The socket parameter in the callback function is the socket port object used to establish a connection between the server and the client.

When receiving a message sent by the client, the message event of the socket port object is sent.

Copy codeThe Code is as follows:
Socket. on ("message", function (msg ){
});

The callback function parameter is the message sent by the client.

You can use socket. send (msg) to send a message to the client.

The disconnect event is triggered when the client-side connection is disconnected on the server.

Copy codeThe Code is as follows:
Socket. on ("disconnect", funciton (){
});

This callback function does not apply to any parameters.

Server. js code:

Copy codeThe Code is as follows:
Var http = require ("http ");
Var sio = require ("socket. io ");
Var fs = require ("fs ");
Var server = http. createServer (function (req, res ){
Res. writehead( 200, {"Content-type": "text/html "});
Res. end (fs. readFileSync ("./index.html "));
});
Server. listen (1337 );
Var socket = sio. listen (server );
Socket. on ("connection", function (socket ){
Console. log ("Client Connection established ");
Socket. send ("hello ");
Socket. on ("message", function (msg ){
Console. log ("received a message:" + msg );
});
Socket. on ("disconnect", function (){
Console. log ("client disconnected .");
});
});

Create the client index.html code:

Copy codeThe Code is as follows:
<! DOCTYPE html>
<Html>
<Head lang = "en">
<Meta charset = "UTF-8">
<Title> </title>
<Script src = "/socket. io/socket. io. js"> </script>
<Script>
Var socket = io. connect ();
Socket. on ("message", function (data ){
Console. log (data );
Socket. send ("Message received .")
});
Socket. on ("disconnect", function (){
Console. log ("server disconnected .");
});
</Script>
</Head>
<Body>
</Body>
</Html>

This code:/socket. io/socket. io. js is provided by the socket. io class library on the server side. You do not need to place a socket. io. js file on the client.

In the script file, first use the io. connect () method to connect to the server socket. io server.

This method returns a client socket port object that establishes a connection with the server.

When receiving a message sent from the server, the message event of the client socket port object is triggered.

Copy codeThe Code is as follows:
Socket. on ("message", function (msg ){
});

Msg is the data sent by the server;

You can also use the send () method of the client's socket object to send data to the server.

Copy codeThe Code is as follows:
Socket. send (msg );

When the server is disconnected, the disconnect event of the client socket port object is triggered,

Copy codeThe Code is as follows:
Socket. on ("disconnect", function (){
})

This callback function does not use any parameters.

Note:

The message mechanism of the client is exactly the same as that of the server, because socket. io ensures that the client shares the same API with the server.

Result After running:

When the browser is closed, the connection to the server is disconnected. At this time, the server triggers the disconnect event and the client disconnects.

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.