1

I'm using nodejs, and one of the reasons why I switched from a php socket server to nodejs is because of the threading ability. (Essentially, I wanted my monsters in the gameserver to auto attack players).

Let's say in my sever.js file for node I put:

setInterval(function(){
        console.log('Hello');
}, 1000);

And I login and authenticate my character on one browser, then look at the console I can see 'Hello' being outputted every second. That's fine, but then I load up a new browser, authenticate another user and then look at the console.. It's actually outputting twice as fast, which is not really the correct way to do this right?

Edit: I'm using https://github.com/websockets/ws and the setInterval function is just under the

socket.on('message', function(Message, flags) {
~~~gameserver authentication /blah mysql blah ~~
setInterval(function(){
        console.log('Hello');
}, 1000);
})

Hope this helps, sorry for not being specific enough.

NiCk Newman
  • 1,716
  • 7
  • 23
  • 48
  • `Let's say in my sever.js file for node ` - this is not true .. put entire file with ( req, res ) functionality. What kind of browsers, what is set up for your testing. **EDIT QUESTION TO PROVIDE MORE INFORMATION** – Piotr Dajlido Mar 19 '15 at 08:11
  • @Urahara I edited, hope this helps lmk if you need anything else – NiCk Newman Mar 19 '15 at 08:15

1 Answers1

1

Your script is run for each user (since that is the server). You can listen and emit to a specific user of course. You need to generate an emit for each one, or write the emit in such a way it sends data only to the desired clients.

This may help you: socket.io and node.js to send message to particular client

Edit after comment:

No, the script will be run for each user so you start an interval for each. If you want to start only one you can:

  1. Name your interval and if it is defined not start it again.
  2. Start the interval on a separate script that you run from console or something like that and it is never accessed by clients.
Community
  • 1
  • 1
zozo
  • 8,230
  • 19
  • 79
  • 134
  • Yeah, I can send data to specific clients on the server already. I store them in a temporary users object. But, the setInterval function seems to be additive upon each new connection. TLDR: Is it possible to set a setInterval function a specific object, (or user), so it would only run if that user is connect and wouldn't overlap other setIntervals (that's when it speeds up and not correct)? Think of it as adding an addEventListener setInterval but to a specific ID in an Object O_O Hmmmm..... – NiCk Newman Mar 19 '15 at 08:21
  • Ok, read your edit. With that said, I can assign my setIntervals to dynamic variables then, current (different users have diff ids, all unique)? Then I can just assign that setInterval to their object id right? If so, I'll mark u as answer in 2min. – NiCk Newman Mar 19 '15 at 08:26
  • 1
    Yes you can, but now I'm thinking if you have access to a specific variable defined at a client from the script ran from another client... (need to check this and can't do it until 18:00 GMT + 2 -> I'm at work atm :) ). – zozo Mar 19 '15 at 08:28
  • Wow, didn't know that about setInterval. Just made the monster AI that much more powerful, awesome! Thank you! Nodejs is a monster. – NiCk Newman Mar 19 '15 at 08:30
  • 1
    It worked? If yes won't need to check it at home any more :). – zozo Mar 19 '15 at 08:31
  • Yeah, don't sweat it. You helped me more than enough and I appreciate it. – NiCk Newman Mar 19 '15 at 08:32
  • 1
    My pleasure. Have fun then. – zozo Mar 19 '15 at 08:33