Consultation on websocket + node. js Implementation of Real-time chat system problems, websocketnode. js

Source: Internet
Author: User

Consultation on websocket + node. js Implementation of 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);/*** the first connection of user light rain */if (str = "light rain") {str1 = conn; clientReady = true; str1.sendText ("welcome" + str);}/*** user Xiaoqiao first connection */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 (" ")});}). listen (8082); console. log ("websocket Connection completed" )client.html: <! DOCTYPE html> 

The server.html page is the same as the code of client.html, that is, replace the username with Xiaoqiao.

The following is the problem:

1. Run the interface:

After client.html is connected:

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.

The preceding section describes websocket + node. if you have any questions about how to implement the real-time chat system in js, please leave a message and I will reply to you in a timely manner. Thank you very much for your support for the help House website!

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.