简体   繁体   中英

Nodejs server-client chat system with tls/ssl encryption

I have written a TLS server-client chat code and there is no error code, but it's not working properly.

My npm version is 7.24.2 and node version is 12.22.7.

When I connect to the server everything is fine, but when I try to send a message the server get it only once. BUT when I send a message back to the client I get the messages. And after that the client get the message only once too and if I want to get the server other messages I need to send a message back.

index.html:

<!DOCTYPE html>
<html>
<head>
   <meta charset="UTF-8">
   <title>Server-Client system</title>
   <meta http-equiv="Content-Security-Policy" content="script-src 'self' 'unsafe-inline';" />
</head>
<body>
   <input type="text" placeholder="ip" id="ip"/>
   <input type="text" placeholder="port" style="width: 40px" id="port"/>
   <input type="button" value="Host a server" id="host" onclick="hostsrv()" /><input type="button" value="Connect" id="connect" onclick="connSrv()"/>
   <br><input type="button" value="Stop server" id="stopsrv" onclick="stopSrv()" /><br>
   <input type="text" placeholder="message" id="msg"/>
   <input type="button" value="Send message" onclick="sendMsg()"/>
   <script src="./index.js" ></script>
</body>
</html>

index.js:

var tls = require('tls'), fs = require('fs'), ipp = require("ip");

var ip = document.getElementById("ip");
var port = document.getElementById("port");
var msg = document.getElementById("msg");
var connBtn = document.getElementById("connect");
var hostBtn = document.getElementById("host");
var stopsrvBtn = document.getElementById("stopsrv");
var srv = null;
var client = null;

ip.value = ipp.address();
port.value = "21";

stopsrvBtn.disabled= true;

//Csatlakozás a szerverhez
async function connSrv(){
  connBtn.disabled = true;
  var _ip = ip.value, _port = port.value;

  var options = {
    rejectUnauthorized: false,
    key: fs.readFileSync('./scs/private-key.pem'), 
    cert: fs.readFileSync('./scs/public-cert.pem'),
  };

  var conn = tls.connect(_port, _ip, options, function() {
      if (conn.authorized) {
          console.log("Connection authorized by a Certificate Authority.");
      } else {
          console.log("Connection not authorized: " + conn.authorizationError);
      }
      console.log();
  },);
  conn.setEncoding('utf-8');
    
  conn.on("data", function (data) {
      console.log("SERVER> msg:\""+ data.toString()+"\"\n");
  });

  conn.on("end", function(){
    console.log("Connection end!");
    connBtn.disabled = false;
    hostBtn.disabled = false;
    client = null;
    return false;
  });
  
  while(conn.connecting){
    await new Promise(r => setTimeout(r, 200));
  }
  if(!conn.authorized && conn.authorizationError == "DEPTH_ZERO_SELF_SIGNED_CERT" || conn.authorized){
    connBtn.disabled = true;
    hostBtn.disabled = true;
    console.log("Connected");
    client = conn;
  }else{
    console.log("Connection error!");
  }
}

var cc = new Array(); //The connected clients list

//Send message
function sendMsg(){
  
  if(srv != null){ //If I am the server
    if(msg.value.trim() != ""){
      for(c of cc){
        c.write(msg.value);
        console.log("ME> " + msg.value);
      }
    }
  }
  if(client != null){ //If I am a client
    if(msg.value.trim() != ""){
      client.write(msg.value);
      console.log("ME> " + msg.value);
    }
  }
  msg.value = "";
}


//Stop the server
function stopSrv(){
  if(srv !=null){
    stopsrvBtn.disabled= true;
    srv.close();
    cc.forEach(client =>{
      client.destroy(); //Kick the clients
    });
    cc = new Array();
    srv = null;
  }
}


//When a client connected
onConnection = socket => {
  console.log("Connection");
  socket.on('data', (data) => {
    console.log("CLIENT> RMSG: " + data.toString());
  });
  socket.on('end', () => {
    const index = cc.indexOf(socket);
    if (index > -1) {
        cc.splice(index, 1);
    }
  });
  socket.setEncoding('utf-8');
  cc.push(socket);
};



//Host the server
async function hostsrv(){
  var options = {
      key: fs.readFileSync('./scs/private-key.pem'),
      cert: fs.readFileSync('./scs/public-cert.pem'),
  };
  var error = false;
  srv = tls.createServer(options, onConnection).listen(port.value, () =>{ });
  srv.on('error', err => { console.log(' error: ', err); srv.close(); error = true; });

  while(!srv.listening && !error){
      await new Promise(r => setTimeout(r, 200));
  }
  if(error) return false;
  else {
    console.log("Listening!");
    stopsrvBtn.disabled= false;
    connBtn.disabled = true;
    hostBtn.disabled = true;
  }
}

//Enter key
msg.addEventListener("keyup", function(event){
  if(event.keyCode == 13){
    event.preventDefault(); 
    sendMsg();
  }
});

main.js:

const { app, BrowserWindow } = require('electron')
const path = require('path')

function createWindow () {
  const win = new BrowserWindow({
    width: 1200,
    height: 700,
    webPreferences: {
      nodeIntegration: true,
      contextIsolation: false,
      enableRemoteModule: true
    }
  })

  win.loadFile('./index.html');
  win.setMenu(null)
  win.webContents.openDevTools()
}

app.whenReady().then(() => {
  createWindow()

  app.on('activate', () => {
    if (BrowserWindow.getAllWindows().length === 0) {
      createWindow()
    }
  })
})

app.on('window-all-closed', () => {
  if (process.platform !== 'darwin') {
    app.quit()
  }
})

I found the answer.
example.js:

socket.pause(); //Pauses the reading of data. That is, 'data' events will not be emitted. Useful to throttle back an upload.
socket.write("MESSAGE"); //Write the message
socket.resume(); //Resumes reading after a call to socket.pause()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM