Tuesday 15 February 2011

javascript - emit to all after receiving a broadcast -


I have found a small app that generates a code and stores it in mongodb (My chrome browser). Another user (my Firefox browser) enters the given code and tells my chrome that it is there.

Now I want my Chrome browser to automatically and to my Firefox browser to make a compromise so that they both get parsed by the same task as the emitted moment of the agreement.

The thing is that I only get 1 console log in my terminal, from which I think only Chrome (or Firefox, which I suspect) is

  io.sockets.on ('connection', function (socket) {socket.on ('code_game', function (Data) {if (codeToUse == data.code) {// Receive the receiving end from the proper code console.log (word.secondUser + 'is wordordon via code:' + data.code); // To tell all parties to amitus that there is a connection socket . Emit ( 'agreement', {UserOne: {name: "Taimn 'code: codeToUse}, userTwo: {name E: data.secondUser, code: data.code}});}});});   

and the JS file is being considered in my opinion: (sendToFirstUser is Firefox in this case)

  var gettingUsersCode = false; Var user name = false; Var socket = io.connect ('http: // localhost'); Socket.on ('contract', function (data) {console.log ("hurray");}); Function set resaving data (code, username) {recipients code = code; Received User Name = Username; Ding = 'dry'; $ ('# New_game') CSS ('display', 'block'); $ ('. TheCode') is the receivingUsersCode. } Function sendToFirstUser (code, username) {socket.emit ('code_game', {code: code, secondUser: username}); }    

I'm not sure what I really think you're asking . But it seems that you are asking why both your Chrome and Firefox browsers are not emitting an 'agreement'. If so, then I think you have answered your question:

"Another user (my Firefox browser) enters the given code and tells my Chrome that He is there. "

  // Other Clients Iio.sockets.on ('Connection', Function (Socket) {socket.broadcast.emit ('code_game', {code: req.bds. Code, secondUser: req.body.secondUser})}}   

Your Firefox browser only sends other clients (your Chrome browser) through socket.broadcast.emit. Therefore, The Chrome browser only receives the 'code_game' event on the browser side. But in your browser side code, the client exits 'contract' event upon receiving the 'code_game' event:

  socket.on ('code_game', function (data) {if usersUsersCode == data.code} {console.log (data.secondUser + 'is connected via the code: '+ DataCode); ListenFormuConnectation (); socket.mit (' contract ', {userOne: {name: obtained user name, code: user code}, user two: {name: data.secondUser, code: data.code }});}});   

Only Chrome browser is receiving the 'code_game' event, so it is only emitting a single 'contract' event.

No comments:

Post a Comment