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!