diff --git a/lib/server/socket.js b/lib/server/socket.js index de862e58..341e261c 100644 --- a/lib/server/socket.js +++ b/lib/server/socket.js @@ -12,7 +12,6 @@ Util = main.util, mainpackage = main.mainpackage, CLOUDCMD = mainpackage.name, - lListen, ClientFuncs = [], OnMessageFuncs = [], INFO_LOG_LEVEL = 2, @@ -43,10 +42,10 @@ var lRet, lConnNum, lMsg, lConn_func; if (io) { - lListen = io.listen(pServer), + io = io.listen(pServer), lConnNum = 0; - lListen.set('log level', INFO_LOG_LEVEL); + io.set('log level', INFO_LOG_LEVEL); /* * on Win7 application is crashing, @@ -56,30 +55,27 @@ * */ if (!WIN32) { - lListen.enable('browser client minification'); - lListen.enable('browser client gzip'); - lListen.enable('browser client etag'); + io.enable('browser client minification'); + io.enable('browser client gzip'); + io.enable('browser client etag'); } - lListen.set('transports', [ + io.set('transports', [ 'websocket', 'htmlfile', 'xhr-polling', 'jsonp-polling' ]); - lRet = lListen.sockets.on('connection', function (socket) { - var lJSON; + lRet = io.sockets.on('connection', function (socket) { ++lConnNum; if(!OnMessageFuncs[lConnNum]) { lMsg = log(lConnNum, 'connected\n'); - lJSON = { - stdout : lMsg - }; - lMsg = Util.stringifyJSON(lJSON); - socket.send(lMsg); + jsonSend(socket, { + stdout : lMsg + }); OnMessageFuncs[lConnNum] = onMessage(lConnNum, socket); lConn_func = OnMessageFuncs[lConnNum]; @@ -88,7 +84,11 @@ socket.on('disconnect', Util.call(disconnect, lConnNum)); } else { lMsg = log(lConnNum, ' in use. Reconnecting...'); - socket.send('{"stdout":"' + lMsg + '"}'); + + jsonSend(socket, { + stdout: lMsg + }); + socket.disconnect(); } @@ -119,7 +119,7 @@ */ function onMessage(pConnNum, pSocket) { return function(pCommand) { - var lMsg, lJSON, lWinCommand, lExec_func, lDir, firstChar, + var lMsg, lWinCommand, lExec_func, lDir, firstChar, connName, lHome, lError, lRet, lExecSymbols, isContain; @@ -142,14 +142,11 @@ pCommand = 'kill -9 ' + process.pid; } else { - lJSON = { + jsonSend(pSocket, { stdout : CLOUDCMD + ' exit \n' + CLOUDCMD + ' update \n', stderr : null - }; - - lMsg = Util.stringifyJSON( lJSON ); - pSocket.send(lMsg); + }); Util.log('received from client: ' + pCommand); Util.log('sended to client: ' + lMsg); @@ -168,13 +165,11 @@ if (!lError) lRet = true; else { - lJSON = { - stderr: Util.stringifyJSON( lError ) - }; + lError = Util.stringifyJSON(lError); - lMsg = Util.stringifyJSON( lJSON ); - - pSocket.send(lMsg); + jsonSend(pSocket, { + stderr: lError + }); } } @@ -200,15 +195,14 @@ if (firstChar === '#') { pCommand = pCommand.slice(1); - pCommand = connName + pCommand +'\n'; + pCommand = connName + pCommand; + pCommand = Util.addNewLine(pCommand); - lJSON = { + lMsg = Util.stringifyJSON({ stdout: pCommand - }; + }); - lMsg = Util.stringifyJSON(lJSON); - - lListen.sockets.emit('message', lMsg); + io.sockets.emit('message', lMsg); } else if (firstChar === ' ' || isContain) exec(pCommand, lExec_func); else @@ -240,13 +234,11 @@ stdout : pStdout, stderr : lErrorStr || lError }; - - lExecStr = Util.stringifyJSON(lExec); log(pConnNum, pError, 'error'); log(pConnNum, pStderr, 'stderror'); - pSocket.send(lExecStr); + jsonSend(pSocket, lExec); }; } @@ -259,8 +251,7 @@ spawn = main.child_process.spawn; cmd = spawn(pCommand, args); send = function(data, isError) { - var lExecStr, - lExec = {}, + var lExec = {}, msg = data.toString(); if (isError) @@ -268,8 +259,7 @@ else lExec.stdout = msg; - lExecStr = JSON.stringify(lExec); - pSocket.send(lExecStr); + jsonSend(pSocket, lExec); }; cmd.stdout.on('data', send); @@ -296,4 +286,10 @@ return lRet; } + + function jsonSend(socket, json) { + var msg = Util.stringifyJSON(json); + console.log(msg); + socket.send(msg); + } })();