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

Popular posts from this blog

c# - Binding a comma separated list to a List<int> in asp.net web api -

Delphi 7 and decode UTF-8 base64 -

html - Is there any way to exclude a single element from the style? (Bootstrap) -