Reputation: 5195
this is the code I have in an index.html (client). Im wondering why the number is not being incremented. I expect on each time a socket connect I give it a unique name.
var number = 0;
socket.on("connect",function(){
number = number+1;
socket.emit("add user","user"+ number )
})
but when i have this server
io.on("connection", function(socket){
console.log("io.onconnection")
socket.on("add user", function(data){
console.log(data)
})
});
on each connection i get logged user1
, why isn't the second connection user2
? thanks
Upvotes: 0
Views: 79
Reputation: 843
Because javascript is a client side, and number will always be 0 every time the page refresh/load.
You can count the number client connected in your websocket, server then pass it to js and then you can increment.
Upvotes: 1