mirror of
https://github.com/coderaiser/cloudcmd.git
synced 2026-01-23 18:55:26 +00:00
added ability to work with terminal to any number of clients
This commit is contained in:
parent
fe0bfc49cd
commit
9bf746cf90
1 changed files with 50 additions and 28 deletions
|
|
@ -1,9 +1,10 @@
|
|||
/* module make possible connectoin thrue socket.io on a server */
|
||||
|
||||
var io = require('socket.io'),
|
||||
exec = require('child_process').exec,
|
||||
Socket = {},
|
||||
Win32_b = process.platform === 'win32';
|
||||
var io = require('socket.io'),
|
||||
exec = require('child_process').exec,
|
||||
ClientFuncs = [],
|
||||
OnMessageFuncs = [],
|
||||
Win32_b = process.platform === 'win32';
|
||||
|
||||
/**
|
||||
* Function listen on servers port
|
||||
|
|
@ -12,45 +13,66 @@ var io = require('socket.io'),
|
|||
exports.listen = function(pServer){
|
||||
io = io.listen(pServer);
|
||||
|
||||
/* number of connections */
|
||||
var lConnNum = 0;
|
||||
io.sockets.on('connection', function (socket) {
|
||||
Socket = socket;
|
||||
|
||||
++lConnNum;
|
||||
socket.send('{"stdout":"client connected"}');
|
||||
|
||||
console.log('server connected');
|
||||
|
||||
socket.on('message', function(pCommand) {
|
||||
if(!OnMessageFuncs[lConnNum])
|
||||
OnMessageFuncs[lConnNum] = onMessage(lConnNum, socket);
|
||||
|
||||
var lConn_func = OnMessageFuncs[lConnNum];
|
||||
|
||||
socket.on('message', lConn_func);
|
||||
|
||||
});
|
||||
};
|
||||
|
||||
/**
|
||||
* function gets onMessage function
|
||||
* that execute needed command
|
||||
*
|
||||
* @param pConnNum, pSocket
|
||||
*/
|
||||
function onMessage(pConnNum, pSocket){
|
||||
return function(pCommand) {
|
||||
console.log(pCommand);
|
||||
|
||||
/* change code page to unicode */
|
||||
if(Win32_b)
|
||||
pCommand = 'chcp 65001 |' + pCommand;
|
||||
|
||||
exec(pCommand, getExec);
|
||||
});
|
||||
|
||||
});
|
||||
};
|
||||
if(!ClientFuncs[pConnNum])
|
||||
ClientFuncs[pConnNum] = getExec(pSocket);
|
||||
|
||||
var lExec_func = ClientFuncs[pConnNum];
|
||||
|
||||
exec(pCommand, lExec_func);
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* function send result of command to client
|
||||
* @param error
|
||||
* @param stdout
|
||||
* @param stderr
|
||||
* @param pSocket
|
||||
*/
|
||||
function getExec(pError, pStdout, pStderr) {
|
||||
if (pError !== null) {
|
||||
console.log('exec error: ' + pError);
|
||||
}
|
||||
|
||||
var lExec = {
|
||||
stdout : pStdout,
|
||||
stderr : pStderr || pError
|
||||
};
|
||||
function getExec(pSocket){
|
||||
return function(pError, pStdout, pStderr) {
|
||||
if (pError !== null) {
|
||||
console.log('exec error: ' + pError);
|
||||
}
|
||||
|
||||
var lExec_str = JSON.stringify(lExec);
|
||||
|
||||
Socket.send(lExec_str);
|
||||
console.log(lExec);
|
||||
var lExec = {
|
||||
stdout : pStdout,
|
||||
stderr : pStderr || pError
|
||||
};
|
||||
|
||||
var lExec_str = JSON.stringify(lExec);
|
||||
|
||||
pSocket.send(lExec_str);
|
||||
console.log(lExec);
|
||||
};
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue