diff --git a/lib/server/socket.js b/lib/server/socket.js index cd792a00..7f4e6569 100644 --- a/lib/server/socket.js +++ b/lib/server/socket.js @@ -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); - } })();