Node socket. io event Usage Details, nodesocket. io

Source: Internet
Author: User

Node socket. io event Usage Details, nodesocket. io

The socket. io class library can not only send messages to each other, but also send events to each other through the emit method of the socket port object.

In the previous event, emit said that it was used to manually trigger the event.

Copy codeThe Code is as follows:
Socket. emit (event, data, function (data1, data2 ......){
});

When you use the emit method to send events, you can use the on method of the socket port object on the other end to listen once.

Copy codeThe Code is as follows:
Socket. on (event, function (data, fn ){
});
Socket. once (event, function (data, fn ){
})

The parameter data in the preceding callback function: the data carried by the event sent by the other party,

Fn: the callback function specified by the other party when sending the event.

Case 1: After the server and the client are connected, a news event is sent to the client. The event carries an object whose hello attribute value is "hello ". when receiving the my other event from the client, output "server receives data" + data contained in the event sent by the client in the console.

Server code, server. js

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 ){
Socket. emit ("news", {hello: "hello "});
Socket. on ("my other event", function (data ){
Console. log ("server received information % j", data );
});
});

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 ("news", function (data ){
Console. log (data. hello );
Socket. emit ("my other event", {my: "data "});
});
</Script>
</Head>
<Body>
 
</Body>

Running result :,
 


We can find that the execution is always on the listener rather than the manual execution.

Case 2: Specify the callback function when you manually trigger the event.

After the client and the server are connected, the setName event is sent to the client. The event carries "Zhang San". When the event is triggered, a callback function is specified. The callback function outputs two parameter values to the console.

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 ){
Socket. emit ("setName", "Zhang San", function (data1, data2 ){
Console. log (data1 );
Console. log (data2 );
});
});

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 ("setName", function (name, fn ){
Console. log (name );
Fn ("Li Si", "Wang Wu ");
});
</Script>
</Head>
<Body>
 
</Body>
</Html>

Execution result:

The callback function is actually executed by the trigger side.

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.