node.js - NodeJS & Socket.io : issue with function killed by other user calls -
hello need socket.io,
in app, user needs connect socket.io server, server launches process , sends updates progress client. code looks :
var myprocess = require( './myprocess.js' ); var websocket = require( './class.websocket.js' ); var w = new websocket(8080); w.on('connexion', function(socket){ console.log('new client connected'); (...) // client launch process socket.on('process', function () { processaction( socket); }); }); function processaction( socket ) { var f = new myprocess(); f.doprocess(function(percent){ // progress received, (100 calls during 2-3mins) socket.emit("onprogress", percent); },function(){ // process completed (only 1 call) socket.emit("oncomplete"); }); }
it works great 1 user. problem when second user connects, function processaction() seems killed or overrided. second user receives progress, first 1 doesn't receive data anymore.
[edit] each user needs receive progress of process only. it's nodejs not handle 2 processaction() running @ same time. calling second time killing previous call , local variables inside well.
can me figure 1 out ? thank much. anthony
so solution (thanks jfriend00) doing :
var onprogress; function myprocess(){} myprocess.prototype.doprocess=function( ponprogress) { onprogress = ponprogress; _doprocess(); } function _doprocess() { console.log("tick"); onprogress(); settimeout(_doprocess, 1000); } module.exports = myprocess;
instead of :
function myprocess(){} myprocess.prototype.doprocess=function( onprogress) { _doprocess(onprogress); } function _doprocess(onprogress) { console.log("tick"); onprogress(); settimeout(_doprocess, 1000, onprogress); } module.exports = myprocess;
or :
function myprocess(){} myprocess.prototype.doprocess = function( onprogress) { this.onprogress = onprogress; _doprocess(this); } function _doprocess(scope) { console.log("tick"); scope.onprogress(); settimeout(_doprocess, 1000, scope); } module.exports = myprocess;
Comments
Post a Comment