everyone.now.broadcast message in setInterval broadcasting for every new connection

34 views
Skip to first unread message

Filipe Deschamps

unread,
Apr 21, 2012, 4:47:49 PM4/21/12
to nowjs
Hi guys!

I set a setInterval() in the root of the server.js code to send some
automatic test messages and I expected nowjs to only send 1 message
for everyone with the specified interval. But the end result is, for
every connection, it creates a new setInterval and sends duplicated,
triplicated, etc. messages/broadcasts. The code is really simple, can
you identify what I'm missing?

http = require("http");
nowjs = require("now");

http_server = http.createServer(function(req,res){
html = require("fs").readFileSync(__dirname + "/index.html");
res.end(html);
});

http_server.listen(8080);

var everyone = nowjs.initialize(http_server);

everyone.now.broadcastMessage = function(message){
everyone.now.receiveMessage(message);
};

setInterval(everyone.now.broadcastMessage,2000,"Test message");

-----

So, if I have one browser opened, the server broadcast 1 message for
every 2 seconds. If I have two browsers opened, it broadcasts 2
messages for every 2 seconds. 3 browsers, 3 messages every 2 seconds,
etc.
Reply all
Reply to author
Forward
0 new messages