I have code like this:
for (var index = 1; index < args.length; index++) {
var shell = args[index];
listener.of('/' + shell.namespace || 'shotgun')
.on('connection', function (socket) {
socket.on('execute', function (cmdStr, context, options) {
console.log('Received: ' + cmdStr);
var result = shell.execute(cmdStr, context, options);
socket.emit('result', result);
});
});
}
I'm using socket.io and I'm creating multiple namespaces. The socket.io namespaces are working great, but in the execute
callback the reference to shell
is always the last object created in the for loop. I need each iteration of the loop to keep reference to the shell
from that iteration.