Socket with notes

Source: Internet
Author: User
Tags emit

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

Related Article

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.