1

I have 2 players in a multiplayer quiz.

Each client has true answers and false answers.

True answers : socket.true

False answers : socket.false

I'm increasing each variables answer by answer and finally, after 5 answers, i'm calling finishgame function.

I want to send a data that contains count of true answers and false answers to each client in finishgame function.

How can I do this ?

If I use socket.emit or socket.broadcast.to(socket.room).emit it sends only one socket's data.

For example :

Send joe -> joe has 3 true , 2false answers;
Send tim -> tim has 1 true , 4 false answers.

But it doesn't happen.

Oleg
  • 9,341
  • 2
  • 43
  • 58
monur00006
  • 47
  • 1
  • 6

1 Answers1

0

Create a new object called answers now when a client starts the quiz you create a new answers object with the socket.id as index, if you need to add a true or false answer you just edit/create a property for that answers object (example answers[socket.id]['trueAnswers']++). Now when you want to send true and false answers to the client you can easily read the answers object with a supplied socket.id as index from the client you want the data from.

Example:

        // Create the object
        Answers = new Object();

        // Client starts the quiz:
        Answers[socket.id] = {
            trueAnswers: 0,
            falseAnswers: 0
        };

        // Client made a correct answer:
        Answers[socket.id].trueAnswers++;

        // Send answers data back that is owned by this socket.id
        io.sockets.socket(socket.id).emit(Answers[socket.id]); // Shows 'Object {trueAnswers: 1, falseAnswers: 0}'

Edit: If you want to send this via a forEach way like you describe I belief this might be what you are looking for:

  • Please note that this function is not available anymore in socket.io versions higher then 1.0, it is recommended to keep a array of your socket.id's so you can iterate over them if need be.

        io.sockets.clients().forEach(function (socket) {
                    // This will go trough all connected sockets
        });
    
GiveMeAllYourCats
  • 890
  • 18
  • 23
  • i need send this info in same time to different clients so i need a function like foreach in php or .each() in jquery... – monur00006 Jul 09 '14 at 14:01
  • it says typeError: Object # has no method 'clients' @MDG – monur00006 Jul 09 '14 at 14:17
  • @monur00006 I see, they stripped it from socket.io 1.0 version! Maybe there are some (better?) alternatives now. Might be worth researching. If I find anything i'll let you know. – GiveMeAllYourCats Jul 09 '14 at 14:44
  • 1
    @monur00006 Okay, seems you need to use the `connected` namespace, check this comment http://stackoverflow.com/questions/6563885/socket-io-how-do-i-get-a-list-of-connected-sockets-clients#comment35016975_6967755 not sure if it will work with forEach, seems they changed alot in 1.0 – GiveMeAllYourCats Jul 09 '14 at 14:50
  • things getting complicated.. i don't get it but thanks. – monur00006 Jul 09 '14 at 14:54
  • @monur00006 no problem, remember you can still downgrade your node.js version to like 0.8 or something and use the above code (version 1.0 is still new and there are not a lot of examples at the moment) – GiveMeAllYourCats Jul 09 '14 at 15:01
  • i'll try to downgrade. I'm using windows. is there any problem with that ? and thanks for patiance @MDG – monur00006 Jul 09 '14 at 15:06
  • @monur00006 not at all, just use a version manager and you should be fine. For windows and node.js you have for example: https://github.com/hakobera/nvmw and anytime! – GiveMeAllYourCats Jul 09 '14 at 15:19
  • hey @MDG, install node.js v0.8.0 and still same problem :( namespace has no method clients. – monur00006 Jul 09 '14 at 16:12
  • have you made sure to check current node version with `node --version` ? – GiveMeAllYourCats Jul 09 '14 at 16:39
  • yeah i'm sure. tried 5 different versions and each one has another problem.. Which version should i install ? @MDG – monur00006 Jul 10 '14 at 06:56
  • @monur00006 The alternative method seems to be to keep a array of socket.id an iterate over them as need be. See this answer http://stackoverflow.com/a/23945018/1531822 – GiveMeAllYourCats Jul 29 '14 at 13:47