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.