Use of emit and on in socket. Io]

Source: Internet
Author: User
Tags emit

Socket. emit ('action'); indicates that an action command is sent, which is a string and can be written as follows when receiving the command from the other end: socket. on ('action', function (){...});
Socket. emit ('action', data); indicates that an action command and data are sent. When receiving data from the other end, you can write it as follows: socket. on ('action', function (data ){...});
Socket. emit (action, arg1, arg2); indicates that an action command is sent and there are two other data. When receiving the data from the other end, you can write it as follows: socket. on ('action', function (arg1, arg2 ){...});
The emit method contains callback functions, such:
Socket. emit ('action', Data, function (arg1, arg2 ){...}); there is a callback function that can be called at the other end, and the other end can be written as follows: socket. on ('action', function (data, FN) {fn ('A', 'B ');});
The data above can contain 0 or more data, and the number of parameters in the function can be changed at the other end. The number and sequence of parameters in the function should be consistent with that in sending.
The above FN indicates the parameter passed by another end. It is a function. If you write FN ('A', 'B'), the callback function is executed. Multiple callbacks cannot be written for a single sending. Otherwise, only the last callback takes effect. The callback should be used as the last parameter.

Use of emit and on in socket. Io]

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.