Custom events
Service side:
emit
: Used to launch an event or trigger an event, the first parameter is the event name, the second parameter is the data to be sent, the third parameter is a callback function (generally omitted, if the other party receives the information immediately after confirmation, you need to use the callback function).
on
: Used to listen for an event that emit fired, the first parameter is the event name to listen to, the second parameter is an anonymous function to receive the data sent by the other, the first parameter of the anonymous function is the data received, if there is a second argument, it is the function to return.
- On is the retrieval event, emit is the firing event
Example:
Server.js
Io.on (' Connection ', function (socket) {
Socket.emit ("new", {hello: "World"});
Socket.on ("My Other Event", function (data) {
Console.log (data);//{test: "555"}
});
})
Client.js
var _socket = io.connect (' ws://100.84.92.125:3000 ');
_socket.on ("New", function (data) {
Console.log (data);//{Hello:world}
_socket.emit ("My Other event", {"Test", "555"});
})
Socket with notes