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.
emit said in the previous event that now says: emit is used to manually trigger events.
The code copy is as follows:
socket.emit(event,data,function(data1,data2......){
});
When sending events using the emit method, you can use the on method of the socket port object on the other end to listen on the once method.
The code copy is as follows:
socket.on(event,function(data,fn){
});
socket.once(event,function(data,fn){
})
The parameter data in the above callback function: the data carried in the event sent by the other party,
fn: The callback callback function specified by the other party when sending an event.
Case 1: After the server and the client are connected, a news event is sent to the client, and an object is carried in the event, and the hello attribute value of the object is "Hello". When the client sends my other event event, the "server receives data" is output in the console + the data carried in the client sends the event.
Server side code, server.js
The code copy 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("The server accepts information %j",data);
});
});
Client index.html code:
The code copy 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>
Operation results:
One thing you can find is that execution is always on the listening side, not on the manual side.
Case 2: When the other party's event is manually triggered, specify the callback function.
When 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, which outputs 2 parameter values to the console.
The code copy 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);
});
});
The code copy 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 results:
The callback function is actually executed at the trigger end.