2

I'm trying to make a node.js server.
The following error can appear when a user disconect himself or when he refresh his page :

enter image description here
This is the code of my server :

var http    = require("http");
var ws      = require("C:/Program Files/nodejs/node_modules/npm/node_modules/nodejs-websocket/");
var colors  = require('C:/Program Files/nodejs/node_modules/npm/node_modules/colors');
var fs      = require("fs");
var request = "C:/Program Files/nodejs/node_modules/npm/node_modules/request";

http.createServer(function (req, res) {
 fs.createReadStream("index.php").pipe(res)
}).listen(8080)

// ###################################################################################################################################
// ######################################################## CLASSE UTILISATEUR #######################################################
// ###################################################################################################################################
var utilisateur = function(){
}
// ###################################################################################################################################
// ########################################################## CLASSE SERVER ##########################################################
// ###################################################################################################################################
var tableauUtilisateur = new Array();

var server = ws.createServer(function (connection){
 connection.trigramme = null
 connection.on("text", function (str){
  if (connection.trigramme === null){
   newUtilisateur = new utilisateur();
   connection.utilisateur = newUtilisateur;
   connection.trigramme = str;
   tableauUtilisateur.push(newUtilisateur);
  }
  else{
   var code = str.substring(4,6);
   var reste = str.substring(6,str.length);
   switch(code){
    case "01": // L'UTILISATEUR DEMANDE A SE CONNECTER
     var postData={data:reste};
     require(request).post({
      uri:"http://localhost/php/seConnecter.php",
      headers:{'content-type': 'application/x-www-form-urlencoded'},
      body:require('querystring').stringify(postData)
      },function(err,res,body){
       if(body != "non"){
        var infos = body.split("-1-2-3-4-5-6-7-8-9-");
        connection.utilisateur.id        = infos[1];
        connection.utilisateur.trigramme = infos[2];
        connection.utilisateur.nom       = infos[3];
        connection.utilisateur.prenom    = infos[4];
        connection.utilisateur.admin     = infos[6];
        connection.trigramme             = infos[2];
        console.log(("> "+infos[2]+" S\'EST CONNECTE AU SERVEUR AVEC SES IDENTIFIANTS.").grey);
        connection.sendText("type01"+infos[0]+",,,,,"+infos[2]+",,,,,"+infos[3]+",,,,,"+infos[4]+",,,,,"+infos[5]+",,,,,"+infos[6]);
        broadcastExcept("type07"+infos[2]+"("+infos[3]+" "+infos[4]+")",connection.utilisateur.id);
       }
       else
        connection.sendText("type01non");
      });
     break;
    case "02": // L'UTILISATEUR DEMANDE A CHANGER D'ANIMATION
     reste += ",,,,,"+connection.utilisateur.id;
     var postData={data:reste};
     require(request).post({
      uri:"http://localhost/php/modifierAnimation.php",
      headers:{'content-type': 'application/x-www-form-urlencoded'},
      body:require('querystring').stringify(postData)
      },function(err,res,body){});
     break;
    case "03": // L'UTILISATEUR DEMANDE A SUPPRIMER UN COMPTE
     if(connection.utilisateur.admin == 1){
      var postData={data:reste};
      require(request).post({
       uri:"http://localhost/php/supprimerCompte.php",
       headers:{'content-type': 'application/x-www-form-urlencoded'},
       body:require('querystring').stringify(postData)
       },function(err,res,body){
        connection.sendText("type03"+body);
        if(body == "oui")
         console.log(("> "+connection.trigramme+" A SUPPRIME UN COMPTE DONT L'ID EST : "+reste).grey);
      });
     }
     else
      connection.sendText("type03non");
     break;
    case "04": // L'UTILISATEUR DEMANDE A CREER UN COMPTE
     if(connection.utilisateur.admin == 1){
      var postData={data:reste};
      require(request).post({
       uri:"http://localhost/php/creerCompte.php",
       headers:{'content-type': 'application/x-www-form-urlencoded'},
       body:require('querystring').stringify(postData)
       },function(err,res,body){
        connection.sendText("type04"+body);
        if(body == "oui"){
         console.log(("> "+connection.trigramme+" A CREE UN COMPTE DONT LE TRIGRAMME EST : "+reste.split(",,,,,")[0]).grey);
         connection.sendText("type04"+body);
        }
      });
     }
     else
      connection.sendText("type04non");
     break;
    case "05": // L'UTILISATEUR DEMANDE A CHANGER LE TYPE D'UN COMPTE
     if(connection.utilisateur.admin == 1){
      var postData={data:reste};
      require(request).post({
       uri:"http://localhost/php/changerTypeCompte.php",
       headers:{'content-type': 'application/x-www-form-urlencoded'},
       body:require('querystring').stringify(postData)
       },function(err,res,body){
        connection.sendText("type05"+body);
        var valeur = reste.split(",,,,,")[1];
        for(var cpt=0;cpt<tableauUtilisateur.length;cpt++){
         if(tableauUtilisateur.id == reste.split(",,,,,")[0]){
          tableauUtilisateur.admin = valeur;
         }
         if(valeur == 1)
          console.log(("> "+connection.trigramme+" A DONNE LES DROITS ADMINISTRTEURS A "+reste.split(",,,,,")[2]).grey);
         else
          console.log(("> "+connection.trigramme+" A RETIRE LES DROITS ADMINISTRTEURS A "+reste.split(",,,,,")[2]).grey);
        }
       });
     }
     else
      connection.sendText("type03non");
     break;
    case "06": // L'UTILISATEUR DEMANDE A SE CONNECTER AVEC SES COOKIES
     var postData={data:reste};
     require(request).post({
      uri:"http://localhost/php/seConnecterCookies.php",
      headers:{'content-type': 'application/x-www-form-urlencoded'},
      body:require('querystring').stringify(postData)
      },function(err,res,body){
       if(body != "non"){
        var infos = body.split("-1-2-3-4-5-6-7-8-9-");
        connection.utilisateur.id        = infos[1];
        connection.utilisateur.trigramme = infos[2];
        connection.utilisateur.nom       = infos[3];
        connection.utilisateur.prenom    = infos[4];
        connection.utilisateur.admin     = infos[6];
        connection.trigramme             = infos[2];
        console.log(("> "+infos[2]+" S\'EST CONNECTE AU SERVEUR AVEC SON ").grey+("COOKIE.").yellow);
        connection.sendText("type01"+infos[0]+",,,,,"+infos[2]+",,,,,"+infos[3]+",,,,,"+infos[4]+",,,,,"+infos[5]+",,,,,"+infos[6]);
        broadcastExcept("type07"+infos[2]+"("+infos[3]+" "+infos[4]+")",connection.utilisateur.id);
       }
       else
        connection.sendText("type01non");
      });
     break;
    case "08": // L'UTILISATEUR DEMANDE A MODIFIER SON MOT DE PASSE
     reste += ",,,,,"+connection.utilisateur.id;
     var postData={data:reste};
     require(request).post({
      uri:"http://localhost/php/modifierMotDePasse.php",
      headers:{'content-type': 'application/x-www-form-urlencoded'},
      body:require('querystring').stringify(postData)
      },function(err,res,body){
       if(body != "non"){
        connection.sendText("type08"+body);
       }
       else
        connection.sendText("type08non");
      });
     break;
   }
  }
 })
 connection.on("close", function (){
  for(var cpt=0;cpt<tableauUtilisateur.length;cpt++){
   if(tableauUtilisateur[cpt].trigramme == connection.trigramme){
    console.log(("> "+connection.trigramme+" A QUITTE LE SERVEUR.").grey);
    tableauUtilisateur.splice(cpt, 1);
   }
  }
 })
})
server.listen(8081)
function broadcast(str){
 server.connections.forEach(function (connection){
  connection.sendText(str)
 })
}
function broadcastExcept(str,except){
 server.connections.forEach(function (connection){
  if(connection.utilisateur.id != except)
   connection.sendText(str)
 })
}

The switch is here to distinct the type of the message (to connect, to talk, etc ...)
I looked at this link, but i don't understand or maybe it's not the same error :/

PS : i don't think the eror is in connection.on("close", function (){ because i tried to remove it and it was the same.

Community
  • 1
  • 1
Antoine Duval
  • 342
  • 3
  • 20

0 Answers0