javascript - Giving different number variables to different browsers in Node.js -


i trying give different numbers each of clients. when click button on first browser must usernumber 1 , when in second browser expect usernumber 2. says usernumber 2 when press button twice or more.

after press button on second browser, open first browser , press button, says usernumber 2 , when press 2 or more times says usernumber 1.

so not stable @ , have press button keep in correct value.

if run files see mean better. use google chrome, express version 3.0.0 , socket.io version 0.9.16.


here app.js , index.html files:

index.html

<!doctype html> <html>  <head> <meta charset="utf-8" /> <title> client number </title>  <script src="http://code.jquery.com/jquery-latest.min.js"></script> <script src="/socket.io/socket.io.js"></script> <script> var ax; jquery(function($) {   var socket = io.connect();    socket.emit('getbrowser1', {});   socket.on('getbrowser1f', function(data) {     ax = data;   });    var butondene1 = document.getelementbyid("butondene1");   butondene1.addeventlistener('click', function() {     socket.emit('getbrowser1', {});     document.getelementbyid("playerwho").innerhtml = "usernumber " + ax;   }); }); </script> </head>  <body>  <button id="butondene1">butondeneme1</button> <text  id="playerwho">player ? </text> </body> </html> 

app.js

var express = require('express'),   app = express(),   server = require('http').createserver(app),   io = require('socket.io').listen(server);  var usernumm = 0; var usernum1 = [];  app.use(express.static(__dirname + '/'));  server.listen((process.env.port || 5000));  io.sockets.on('connection', function(socket) {   var endpoint = socket.manager.handshaken[socket.id].address;    console.log('***new connection ' + endpoint.address + ':' + endpoint.port);    usernumm++;   usernum1[usernumm] = endpoint.port;    console.log('usernum' + usernumm + 'geldi' + findusernum());    socket.on('disconnect', function() {     usernumm--;      console.log('***client disconnected');   });     socket.on('getbrowser1', function(data) {     var = findusernum();     io.sockets.emit('getbrowser1f', a);   });    //finds number of online users    function findusernum() {     (var = 0; < 4; i++) {       if (usernum1[i] == endpoint.port) {         return i;       } else {}     }   }  }); 

i cannot reproduce described behaviour, endpoint.port not different different clients. might want use uuid instead.

beside have diffent logical problem within code.

when connection happens write:

usernumm++; usernum1[usernumm] = endpoint.port; 

and on disconnect have:

usernumm--; 

lets assume have following connection order:

usera connects (usernumm++ => 1) userb connects (usernumm++ => 2)  usera disconnects (usernumm-- => 1) userc connects (usernumm++ => 2) << here overwrite data of userb 

beside have:

for (var = 0; < 4; i++) {    if (usernum1[i] == endpoint.port) {      return i;    } else {} } 

the problem here start loop @ 0 index 0 of our usernum1 undefined because use usernumm after increment. not problem here, makes code hard read because assume there @ maximum 4 users can have 3.

but problem instead of socket.manager.handshaken[socket.id].address can either use incrementing number (depends on how long service run) or uuid or random number not used connected user.


Comments

Popular posts from this blog

php - Wordpress website dashboard page or post editor content is not showing but front end data is showing properly -

javascript - Get parameter of GET request -

javascript - Twitter Bootstrap - how to add some more margin between tooltip popup and element -