Use Nodejs to build a server and client, simulate Quiz wizard

Source: Internet
Author: User
Tags get ip

Hee Hee Recently playing Nodejs, in my study and understanding of the NODEJS implementation environment is mainly divided into three parts, first there will be a global object, and then in this global object contains a core module and file module (can be understood as the user to write their own files).

And Nodejs has two characteristics, that is 1, is a non-blocking I/O model, that is, Nodejs operation is asynchronous. 2, is based on the event-driven program design ideas.

Today is mainly through their own server and client, let the communication between the two, simulating a similar question and Answer Wizard an effect. Before that, it was necessary to understand how the server interacts with the browser, in fact, both the server side and the browser side have a thing called the socket, it is responsible for receiving and sending messages. This means that the browser will send the request through the socket, and the server will respond to the browser via its own socket.

In this small case, we mainly use a Nodejs core module is "NET Core module", through the method of this module we can create a server and establish a server and client connection and other functions.

OK, let's go straight to the code.

Server-side code (SERVER.JS):

1 //first use strict mode to standardize the file2"Use Strict";3 //introducing the net Core module4Const NET = require ("NET");5 //Create a Server socket object6Let Socketserver =net.createserver ();7 8 //turn on the server9Socketserver.on ("Connection", (socket) = ={TenConsole.log ("There are customers connected up");//as a test if there is a customer connection One     //output The following text when the connection is created ASocket.write ("Master, Xiao Ya at any time waiting, to provide you with the best quality service!" "); -  -     //when the client has data sent over, the following event is triggered theSocket.on ("Data", (content) ={ -         //process the information sent by the user -         varmsg =content.tostring (). Trim (); -         //determine what the user has entered +         if(msg! = ""){ -             Switch(msg) { +                  CaseHello ": ASocket.write ("Hello, Master!" Is there anything I can do to help you? "); at                     Break; -                  Case"Good morning!" ": -Socket.write ("Good morning! "); -                     Break; -                  Case"You are stupid!" ": -Socket.write ("Master, I allow you to say I am stupid, but you can not insult my IQ.") "); in                     Break; -                  Case"Aren't you stupid?" ": toSocket.write ("My master said I was foolish. But, master, do you have the heart to say I'm stupid? "); +                     Break; -                  Case"What do you have for breakfast?" ": theSocket.write ("Master, you like Yo!") But remember to eat a nutritious breakfast! "); *                     Break; $                  Case"What do you have for lunch?" ":Panax NotoginsengSocket.write ("Master, you can choose to eat the Yo." "); -                     Break; the                  Case"What do you have for dinner?" ": +Socket.write ("Hello, master! "); A                     Break; the                 default: +Socket.write ("I don't know what you're talking about.")); -                     Break; $             }    $         } -     }); -     //Handling Exceptions theSocket.on ("Error", () ={ -Console.log ("Customer Drop");Wuyi     }); the }); -  Wu //to monitor -Socketserver.listen (8088, ' 127.0.0.1 ', () ={ AboutConsole.log ("Server already started"); $})

The following is the code for the Client (client.js):

1"Use Strict";2Const NET = require ("NET");3 4 //Get IP and Port5Const IP = "127.0.0.1";6Const PORT = 8088;7 //Establish a connection8 varSocket = Net.createconnection (Port,ip, () ={9    //Console.log ("connected to Server");Ten }); One //listening to server data ASocket.on ("Data", (content) ={ - console.log (' Little Ya: ${content} '); - }); the  - //add an input event to the client -Process.stdin.on ("readable", () ={ -     varmsg =Process.stdin.read (); +     if(msg! =NULL){ -         //Send the information you entered to the server + socket.write (msg); A     } at})

When running servers and clients separately in PowerShell, you can achieve the same effect as a quiz wizard.

Note: To turn on the server first, and then turn on the client to operate the nest.

The effect is shown in the following picture:

Use Nodejs to build a server and client, simulate Quiz wizard

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.