How to start setinterval in server answer? - setinterval

For example we have code like this :
setInterval(function () { // Content value
counttime.value = "10000";
}, 2000);
we need it , when server accept our request
we dont want to send this command , before server acception
i need example about that

Related

nodejs wait response of tcp server

Probably this is already answered question but somehow the solutions that I found would not work. It is better to describe.
I have an embedded linux device which has a C based application running and nodejs is running as a web server. I use TCP socket for inter-process communication.
Main problem is that C application is designed to run in a syncronous way but not nodejs.
Web page sends requests to nodejs and nodejs asks to C application and wait for application result to send response to web page.
Usually this is not a problem since everything runs very fast. But in the embed side, now there are slow processes thus it makes nodejs wait.
var counterVal = 0;
app.post('/ajax.html', jsonParser, async function (req, res) {
var q = url.parse(req.url, true);
var formData = req.body;
var cMessage = { counter : counterVal };
var message = { ...q.query, ...formData, ...cMessage };
await remoteSocket.write(JSON.stringify(message), function ()
{
remoteSocket.on('data', function (e) {
try
{
remoteSocket._events.data = function (data) { };
var ret = JSON.parse(e);
if (message.counter == ret.counter)
{
res.send(e);
}
else
{
logError("received error : "+ message.counter + ": " + ret.counter);
}
}
catch (err) {
logError(err);
}
});
});
});
I use this code to handle web requests. Counter value is used as handshake. Some of the requests gets into logError because of mismatch.
But when i look into wireshark records packet by packet, there is no mismatch. ever request has correct response.
app.post is async so data listener is registered twice. Then response for a packet triggers two listeners but one listener is for next packet. Therefore is gives a mismatch.
What is a way to make app.post to run in sync mode?

Emitting multiple times

I've read through the site and googled, and can't seem to find an answer that will work for me.
I've set up a super super basic example of using Socket.IO until I can get my head around, all it does is passes a number to the back end, adds +1 and send it back to front end.
It does work however, each interval round, it emits more and more (which I can see using console.log on the server side.) I'm only connected using one computer to test.
Can someone help me please. I understand its probably because the emit is inside the connection but I just can't quite click in my head on how to overcome this.
I've tried moving bits around, moving the function out of the connection. I've tried multiple ideas from google, but nothing seems to solve it.
const io = require('socket.io')();
io.on('connection', (socket) => {
socket.on('subscribeToAddition', (additon,interval) => {
console.log('current number... ', additon);
setInterval(() => {
socket.emit('addition', additon+1);
}, interval);
});
});
const port = 8000;
io.listen(port);
console.log('listening on port ', port);
the interval variable is set as 5 seconds in my react component. I just want it to log/update once every five seconds instead of log once, then twice, then 4 times, then 8 times, etc
Given the symptoms, it's likely that you're sending the subscribeToAddition event more than once on the same socket and thus starting more than one timer for the same socket and thus you get duplicate messages.
Proper indentation of your code makes things a little clearer:
const io = require('socket.io')();
io.on('connection', (socket) => {
socket.on('subscribeToAddition', (additon, interval) => {
console.log('current number... ', additon);
setInterval(() => {
socket.emit('addition', additon + 1);
}, interval);
});
});
const port = 8000;
io.listen(port);
console.log('listening on port ', port);
I want to make sure you understand that setInterval() starts a repeating timer that goes forever until you call clearInterval() on the timerId that it returns.
Problems I see:
In every subscribeToAddition message, you're adding a new setInterval() timer. So, if you send the subscribeToAddition twice, you will have two setInterval() timers each going on the same socket.
Those setInterval() timers will accumulate and never go away because you have no way of the client stopping them and they don't go away even when the socket closes. They will just cause errors because socket.emit() won't work on a closed socket, but they will probably even prevent garbage collection of the old socket.
It is not clear exactly how you want this to work, but here's a cleaned up version:
const io = require('socket.io')();
io.on('connection', (socket) => {
function clearTimer() {
if (socket.myTimer) {
clearInterval(socket.myTimer);
delete socket.myTimer;
}
}
socket.on('subscribeToAddition', (additon, interval) => {
console.log('current number... ', additon);
// don't start a new interval timer if one is already running
if (!socket.mytimer) {
socket.mytimer = setInterval(() => {
socket.emit('addition', ++additon);
}, interval);
}
});
socket.on('unSubscribeToAddition', () => {
clearTimer();
});
socket.on('disconnect', () => {
clearTimer();
});
});
const port = 8000;
io.listen(port);
console.log('listening on port ', port);
This version makes the following modifications:
Keeps track of the timerID from setInterval() so we can stop it in the future. For convenience, we store it as a custom property on the socket object so that each connected socket has its own timer.
Adds an unsubscribeToAddition message so the client can stop the timer.
Adds a disconnect message handler so you can stop the timer when the socket disconnects.
Increments the additon variable on each tick of the timer.

socket.io data seems to be sent multiple times(nodejs and craftyjs)

I am following this tutorial on making HTML5 games. I wanted to try and mix node in to make it multiplayer. I am using node.js(v0.10.4) on server and crafty.js on front end.
I am using socket.io to send and receive messages. For now it's just me(not multiple clients). The weird thing that happens is that the message that comes from the server seems to be sent multiple times. I turned on debug mode in socket.io but it only seems to be sending the data once, yet on the front end the data seems to be coming in, in multiples. I set an incrementor on the data and it seems as if the incrementor is not incrementing multiple times but instead I am getting multiple copies of the same data.
here's node code:
var http = require('http').createServer(handler),
static = require('node-static'),
io = require('socket.io').listen(http);
io.set('log level', 3);
http.listen(80);
//attach the socket to our server
var file = new static.Server(); //Create a file object so we can server the files in the correct folder
function handler(req, res) {
req.addListener('end', function() {
file.serve(req, res);
}).resume();
}
io.sockets.on('connection', function (socket) { //listen for any sockets that will come from the client
socket.on('collected', function(data) {
/**** here's where the data is being sent back to the client *****/
socket.emit('messageFromServer', { data: data.number });
});
});
and here's front end code:
//messenger entity
Crafty.c('SendRecieveMessages',{
count: 0,
sendMessageToServer : function() {
console.log('got a village');
/**** Here's where we send the message to the server ****/
socket.emit('collected', { village : "The message went to the server and back. it's collected", number : this.count });
this.count++;
},
recieveMessageFromServer : function() {
socket.on('messageFromServer', function(data) {
/*** This data seems to be coming back or logging multiple times? ***/
console.log(data);
});
}
});
Lastly here's a screenshot of the debug in process. As you can see number is not always incrementing, it almost looks like the data is getting stored. Thanks!
http://cl.ly/image/0i3H0q2P1X0S
It looks like every time you call Crafty.c, recieveMessageFromServer() is getting called too. Every time recieveMessageFromServer is invoked, it attaches an additional event listener on the socket. That's why the first time data comes back you get one copy, then the second time you get two, the third time you get three, and so on.
You either need to prevent recieveMessageFromServer from being called multiple times, or use removeListener or removeAllListeners to remove the previously attached listeners.
Thanks to #Bret Copeland for helping me figure this one out. As he pointed out, every time socket.on() is called, it seems to add another listener. To prevent this...
I declared a global variable:
I declared a variable as a property in my Game object(in craftyjs, so use whatever you want in your setup)
Game = {
//lots of other code here...
//need this to use later for socket.io
send_message : true
}
then edited my recieveMessageFromServer() function to check whether its ok to send the message or not:
recieveMessageFromServer : function() {
console.log('does this show up multiple times?');
/* Check whether the send_message is true before sending */
if (Game.send_message) {
socket.on('messageFromServer', function(data) {
console.log(data);
Game.send_message = false;
});
}
}

Emulate server in Node.js - stream delimited file with 1-second pauses

I'm new to Node.js and I am writing a client to consume a text-based TCP stream from a server. For testing purposes, I want to simulate the server in Node so I can test with no other dependencies.
I have a file of captured data that looks like:
$X,... <-- broadcast every second
$A,...
$A,...
$B,...
$X,... <-- broadcast every second
$A,...
$A,...
$C,...
$X,... <-- broadcast every second
The server emits a line starting with $X every second. The other records are broadcast as events happen. How can I modify my network server below to broadcast this data and throttle it so it emits one line at a time and pauses for one second every time it encounters a line starting with $X?
Here is my code so far which reads in the data and broadcasts it over a port:
var http = require('http')
, fs = require('fs')
;
var server = http.createServer(function (req, res) {
var stream = fs.createReadStream(__dirname + '/data.txt');
stream.pipe(res);
});
server.listen(8000);
console.log('server running on 8000');
This works but obviously just streams out the whole file at warp speed. What I want is to spit out all of the lines from one $X to the next, pause for one second (close enough for testing purposes) and then continue to the next $X and so on like:
> telnet 127.0.0.1 8000
$X,...
$A,...
$A,...
$B,...
(output would pause for one second)
$X,...
$A,...
$A,...
$C,...
(output would pause for one second)
$X,...
...
In my example above, the broadcast always starts from the beginning of data.txt when I connect with a client. Ideally, this server would keep broadcasting this data in a loop, allowing clients to disconnect and reconnect at any time and start receiving data wherever the server simulator was currently at.
(PS - data.txt is a relatively small file, < 1MB in most cases)
UPDATE -
Thanks to Laurent's pointer, I was able to get it working with the following:
var net = require('net'),
fs = require('fs'),
async = require('async');
var server = net.createServer(function (socket) {
var lines = fs.readFileSync(__dirname + '/data-small.txt').toString().split(/\n+/);
async.whilst(
function () {
return lines.length > 0;
},
function (done) {
var line = lines.shift();
socket.write(line + '\r\n');
setTimeout(done, /^\$X,/.test(line) ? 1000 : 0);
},
function (err) {
// no more lines present
socket.end();
});
});
server.listen(8000);
console.log('server running on 8000');
I'm now getting a blast of lines until an $X, a 1s pause, and then it continues! Thanks!
Drilling into my 2nd part: is there a way to synchronize output of this faux server so all clients see the same output regardless of when they connect?
If you want to keep all clients in sync, you need to do something entirely different. Here's a starting point. Also, it seems like the net module would be a better fit.
var net = require('net'),
fs = require('fs'),
_ = require('underscore');
var current = 0,
sockets = [];
// dirty parser for blocs
var data = fs.readFileSync(__dirname + '/data.txt').toString(),
blocs = _.chain(data.split(/\$X,/)).compact().map(function (bloc) {
return '$X,' + bloc;
}).value();
function streamBloc() {
console.log('writing bloc #' + current + ' to ' + sockets.length + ' sockets');
_(sockets).each(function (socket) {
socket.write(blocs[current]);
});
current = (current + 1) % blocs.length;
setTimeout(streamBloc, 1000);
}
var server = net.createServer(function (socket) {
console.log('incoming connection');
// immediately write current bloc
socket.write(blocs[current]);
// add to sockets so that it receive future blocs
sockets.push(socket);
// cleanup when the client leaves
socket.on('end', function () {
sockets = _(sockets).without(socket);
});
}).listen(8000, function () {
console.log('server listening on port 8000');
});
streamBloc();

How to emit an event and pass a function as a parameter with socket.io

I am using socket.io and I am trying to emit an event from my server and pass an object with a function as a parameter. Here is my code:
socket.emit('customEvent', {
name : "Test".
myFunc : function() {
//some logic here
}
});
and then in the client (my app in the browser) I am able to access 'name' property but when I try to access 'myFunc' but I get 'undefined' for it. Here is my code
socket.on('customEvent', function(data){
data.myFunc();
});
What is the correct approach for this (if it is possible at all)?
The data is transmitted as JSON, so it can't contain functions. Maybe you're looking for what's called 'acknowledgments' in socket.io's documentation?
// server
socket.on('customEvent', function (data, fn) {
fn(data.x)
})
// client
socket.emit('customEvent', { x: 1 }, function(){
// ...
})
You can serialize your function, maybe it's a dangerous way for some functions. But socket.io transfers only strings as pure strings or JSON. You can try to eval string function when receive it from server side.
NOTE: Code not tested below:
function hello() {
return "Hello Cruel World";
}
socket.emit('send-function', { myFunc : hello.toString() });
...
on server side:
socket.on('send-function', data) {
console.log(eval(data.myFunc));
}
Try on this code and give us a feedback if it works.

Resources