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 ?