2

I have two socket.io servers listening to two different ports on my node.js project. I was running them as a http servers like that.

my server file :

var  server = require('http').createServer(app);

//server port are 5000
var io = require('socket.io').listen(server);

var io3 = require('socket.io').listen(5132);

and on my first client side page /usrlist

var socket = io.connect('http://localhost:5000',{
    reconnection: true,
    reconnectionDelay: 5000,
    reconnectionAttempts: 12
});

and on my client sides page /playingroom

var socket = io.connect('http://localhost:5132');

then i decided to making my connection secure using https so i changed my code to this

my server file :

const cors = require('cors');
app.use(cors({credentials: true, origin: true}));

var httpsOptions = {
    key: fs.readFileSync('./app/certsandkeys/nwcertandkey/my-prvkey.pem'),
    cert: fs.readFileSync('./app/certsandkeys/nwcertandkey/my-pubcert.pem')
};    
var sctio = require('https').createServer(httpsOptions, app);
sctio.listen(443);

var sctio3 = require('https').createServer(httpsOptions, app);
sctio3.listen(5132);

//using the default port 443 instead of the port 5000

var io = require('socket.io').listen(sctio);    
var io3 = require('socket.io').listen(sctio3);

and on my first client side page /usrlist

var socket = io.connect('https://localhost',{
    secure: true,
    reconnection: true,
    reconnectionDelay: 5000,
    reconnectionAttempts: 12
});

and on my client sides page /playingroom

var socket = io.connect('https://localhost:5132',{secure: true});

then after running my code my javascript code start to giving me error that saying i have undefined var but my javascript code was working very good before the changes from http to https and i tested that hundreds of time and im sure all my included files urls edited from http to https and my page can reading it perfectly but i don't know why one of my var just giving me undefined with out changing anything except my socket.io code

UPDATE:

after runing my code agian today the code worked fine for one time then the error come back agian then the error start to came and go in random way ????

the error massage :

> jQuery.Deferred exception: $usercolor is not defined
> MakeMove@https://localhost/cssFiles/js/makemove.js:170:4
> CheckResult@https://localhost/cssFiles/js/guiMultiPlayer.js:62:9
> CheckAndSet@https://localhost/cssFiles/js/guiMultiPlayer.js:183:6
> NewGame@https://localhost/cssFiles/js/guiMultiPlayer.js:764:3
> @https://localhost/cssFiles/js/main.js:9:2
> e@https://localhost/js/jquery.min.js:2:29453
> l/</t<@https://localhost/js/jquery.min.js:2:29755  undefined
> jquery.min.js:2:31008

my post code part on server :

app.post('/getcolor', function (req, res, next) {
    var lCol="";    

    if(req.body.MoveString !== null){
        Move.setMoveUserId(req.user.id);
        a.getColor(req.user.id,function(col){
            res.json({"msg": col, "loggedin": "true"}); // <=== here you have a defined lCol             
        });
    } else {
        var output = {"msg": lCol, "loggedin": "true"}; // <=== here lCol always equals ""
        res.json(output);
    }
});

the javascript post code :

function UserColor() { // Is used to check which color the opponent has
    var MoveString = BoardToFen();
    $.ajax({
        type:'POST',
        url:'/getcolor',
        data:{MoveString:MoveString},
        dataType:'json',
        cache: false,
        success:function(data){
            if (data.msg == "white"){
                $usercolor=0;
                console.log("usercolorusercolor",$usercolor);
            } else{
                console.log("thrusercolorusercolor",$usercolor);
                $usercolor=1;
            }
        }
    });
    return $usercolor;
}

I hope from anyone who will answering to explain why my code was working ok before converting my connection from http socket.io to https secure connection but after converting i started to get this error ?

chiragchavda.ks
  • 532
  • 7
  • 25
shar
  • 1,233
  • 6
  • 20
  • 30
  • 1
    Which var is undefined and in which line of which code? – Kaddath Sep 02 '19 at 07:31
  • Can you open the dev tools and check if some js resources failed to load? – Roland Starke Sep 02 '19 at 07:31
  • @RolandStarke i chacked all my linkes and they all giving me 200 stats and there are zero 404 loads – shar Sep 02 '19 at 07:38
  • @Kaddath it's a var that giving me a value from post code – shar Sep 02 '19 at 07:39
  • Then can you please add this part in the question? – Kaddath Sep 02 '19 at 08:00
  • @Kaddath i updated my post – shar Sep 02 '19 at 21:02
  • 2
    Hi @Kaddath .. in case the `data.msg` is not equal `white` you are logging out a string with an undefined variable `$usercolor` (you first call `console.log(.., $usercolor)` and then you define `$usercolor =1`. Please, check what does it mean when writing asynchronous code and ways how to synchronize it - eg callbacks, promises, async/await – Jan Jůna Sep 02 '19 at 21:10
  • @JanJůna i can seeing the console output result right put still getting the undefined error ???? – shar Sep 02 '19 at 21:31

2 Answers2

0

According to the text of the error, the problem is not in socket.io. The mistake is that you define variable $usercolor without var/let/const statement. By default js code executes in “strict mode”, so you need to use var/let/const statement

0

As already explained in the comments, you have a problem setting the variable $usercolor.

But if you say the issue appeared after switching from http to https, the problem is likely that the 'success' callback in your ajax call is not triggered anymore.

Ajax success event not working

Edit

The UserColor function with support of async and an 'error' callback in the ajax call to log network errors.

function UserColor(callback) { // Is used to check which color the opponent has
  var MoveString = BoardToFen();
  $.ajax({
    type:'POST',
    url:'/getcolor',
    data:{MoveString:MoveString},
    dataType:'json',
    cache: false,
    success:function(data){
      if (data.msg == "white"){
        $usercolor=0;
      } else{
        $usercolor=1;
      }
      console.log("usercolorusercolor",$usercolor);
      callback(null,$usercolor);
    },
    error:function(err) {
      console.error(err);
      callback(err);
    }
  }); 
}
Guillaume Adam
  • 191
  • 2
  • 10