Use of Socket.io in node. js (node. JS Development Walkthrough a case study)

Source: Internet
Author: User
Tags emit

Ah, this is the book again. It's like there's an interactive diagram, uh ... The order of the message response seems to be different from my program.

In fact, the problem is not big, the operation instance, the console runs the file App.js open service:

var io=require (' Socket.io '). Listen (8080,{log:false}); Io.sockets.on (' Connection ', function (socket) {Socket.on (' msg ', function (data) {Console.log (data), if (data.state) {if (data.state== ' OK ') {socket.emit (' msg ', {' Me ': ' Very good '});} Else{socket.emit (' msg ', {other: ' That's ' all '});}} Else{socket.emit (' msg ', {me: ' That's None '})})

Then the browser opens the page index.html:

<! DOCTYPE html>

You can then see the corresponding response in both consoles, socket.io.js please download it on the website. Note the client's Console.log () statement is output on the browser side, and the server output is in the cmd window.

I'll change the example a little, let two events listen to each other to trigger a better understanding of the message delivery, the final picture:

Output order: {state: ' OK '}->{me: ' Very good '}->{connection: ' Good '}->{me: ' That's none '}-> ... And then loop. If the expression is wrong, welcome the great God.

Use of Socket.io in node. js (node. JS Development Walkthrough A case study)

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.