Consultation on websocket + node. js real-time chat system problems, websocketnode. js

Source: Internet
Author: User

Consultation on websocket + node. js real-time chat system problems, websocketnode. js

1. I recently learned about websocket. A Real-time chat. Services built with Node. js: serevr. js. Two Communication pages: client.html and server.html

But there are a lot of problems. I want to ask anyone to help me:

I first paste the Code:

Server. js:

Var ws = require ("nodejs-websocket "); Console. log ("start to establish a connection ..."); Var str1 = null, str2 = null, clientReady = false, serverReady = false; Var server = ws. createServer (function (conn ){ Conn. on ('text', function (str ){ Console. log (str ); /** * User light rain first connection */ If (str = "light rain "){ Str1 = conn; ClientReady = true; Str1.sendText ("welcome" + str ); } /** * User Xiaoqiao first connects */ If (str = "Xiaoqiao "){ Str2 = conn; ServerReady = true; Str2.sendText ("welcome" + str ); } /** * When there is a second user connection. */ If (clientReady & serverReady ){ Str2.sendText (str ); Str1.sendText (str ); } }) Conn. on ("close", function (code, reason ){ Console. log ("close connection "); }) Conn. on ("error", function (code, reason ){ Console. log ("Disable exceptions ") }); }). Listen (8082 ); Console. log ("websocket Connection completed ")  Client.html:<! DOCTYPE html>

Originally, the server only needs to return a light rain greeting, and then output one below.

After connecting server.html Xiaoqiao, I also came out with a Xiaoqiao. It is reasonable to welcome Xiaoqiao. Then he told Xiaoqiao that XiaoYu was online.

2. The two pages have the same code, but they cannot be changed to only one page.

3. The logic of server. js is a bit problematic and cannot be understood.

 

 

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.