feature(socket) add spawn

This commit is contained in:
coderaiser 2013-10-24 08:28:55 +00:00
parent a0a064e985
commit 7bda84f98f

View file

@ -120,7 +120,7 @@
function onMessage(pConnNum, pSocket) {
return function(pCommand) {
var lMsg, lJSON, lWinCommand, lExec_func, lDir,
lHome, lError, lRet;
lHome, lError, lRet, lExecSymbols;
Util.log('#' + pConnNum + ': ' + pCommand);
@ -193,7 +193,12 @@
lExec_func = ClientFuncs[pConnNum];
exec(pCommand, lExec_func);
lExecSymbols = ['{', '}', '|', '\'', '"'];
if (Util.isContainStr(pCommand, lExecSymbols))
exec(pCommand, lExec_func);
else
getSpawn(pSocket, pConnNum, pCommand);
}
};
}
@ -220,7 +225,7 @@
stderr : lError
};
lExecStr = JSON.stringify(lExec);
lExecStr = JSON.stringify(lExec);
log(pConnNum, pError, 'error');
log(pConnNum, pStderr, 'stderror');
@ -229,6 +234,40 @@
};
}
function getSpawn(pSocket, pConnNum, pCommand) {
var send, cmd, spawn,
args = pCommand.split(' ');
pCommand = args.shift();
spawn = main.child_process.spawn;
cmd = spawn(pCommand, args);
send = function(data, isError) {
var lExecStr,
lExec = {},
msg = data.toString();
if (isError)
lExec.stderr = msg;
else
lExec.stdout = msg;
lExecStr = JSON.stringify(lExec);
pSocket.send(lExecStr);
Util.log(msg);
};
cmd.stdout.on('data', send);
cmd.stderr.on('data', function(data) {
send(data, true);
});
cmd.on('error', function(data) {
send(data, true);
});
}
function log(pConnNum, pStr, pType) {
var lRet, lType = ' ';