refactor(socket) onMessage: pSocket -> callback

This commit is contained in:
coderaiser 2014-01-27 08:43:16 -05:00
parent 48e570c99e
commit 8e8a0618e8

View file

@ -85,6 +85,11 @@
return ret;
};
function jsonSend(socket, json) {
var msg = Util.stringifyJSON(json);
socket.send(msg);
}
function onConnection(socket) {
var msg, onDisconect, onConnect;
@ -98,7 +103,9 @@
});
Clients[ConNum] = true;
onConnect = onMessage(ConNum, socket);
onConnect = onMessage(ConNum, function(json) {
jsonSend(socket, json);
});
onDisconect = Util.retFunc(disconnect, ConNum);
socket.on('message', onConnect);
@ -128,9 +135,10 @@
* function gets onMessage function
* that execute needed command
*
* @param pConnNum, pSocket
* @param pConnNum
* @param callback
*/
function onMessage(pConnNum, pSocket) {
function onMessage(pConnNum, callback) {
return function(pCommand) {
var lMsg, lWinCommand, lExec_func, firstChar,
connName,
@ -146,9 +154,7 @@
Util.log(connName + pCommand);
if (equalPart(pCommand, CLOUDCMD))
lRet = onCloudCmd(pCommand, function(json) {
jsonSend(pSocket, json);
});
lRet = onCloudCmd(pCommand, Util.retExec(callback, json));
else if (equalPart(pCommand, 'cd ')) {
lRet = true;
@ -157,7 +163,7 @@
stdout = json.stdout;
if (error)
jsonSend(pSocket, json);
Util.exec(callback, json);
else
ClientDirs[pConnNum] = stdout;
});
@ -180,7 +186,7 @@
log(pConnNum, pError, 'error');
log(pConnNum, pStderr, 'stderror');
jsonSend(pSocket, json);
Util.exec(callback, json);
});
lExec_func = ClientFuncs[pConnNum];
@ -202,9 +208,7 @@
} else if (WIN32 || firstChar === ' ' || isContain)
exec(pCommand, options, lExec_func);
else
getSpawn(pCommand, options, function(json) {
jsonSend(pSocket, json);
});
getSpawn(pCommand, options, Util.retExec(callback));
}
};
}
@ -212,7 +216,7 @@
/**
* function send result of command to client
* @param pSocket
* @param callback
*/
function getExec(callback) {
return function(pError, pStdout, pStderr) {
@ -344,9 +348,4 @@
return lRet;
}
function jsonSend(socket, json) {
var msg = Util.stringifyJSON(json);
socket.send(msg);
}
})();