feature(terminal) add

This commit is contained in:
coderaiser 2014-02-03 08:52:56 -05:00
parent 8a0d695d0a
commit a1159774a9
9 changed files with 6137 additions and 2 deletions

View file

@ -6,6 +6,7 @@
"config",
"contact",
"socket",
"terminal",
"console", {
"name": "storage",
"data": [{

View file

@ -160,7 +160,7 @@ var CloudCmd, Util, DOM;
}
function switchKey(pEvent) {
var i, n, id,
var i, n, id, obj,
current = Info.element,
panel = Info.panel,
path = Info.path,
@ -284,7 +284,13 @@ var CloudCmd, Util, DOM;
case Key.TRA:
DOM.Images.showLoad({top: true});
Util.exec(CloudCmd.Console.show);
if (shift)
obj = CloudCmd.Terminal;
else
obj = CloudCmd.Console;
Util.exec(obj.show);
DOM.preventDefault(pEvent);
break;

View file

@ -21,6 +21,7 @@ var CloudCmd, Util, DOM, CloudFunc, io;
Socket.addListener = addListener;
Socket.removeListener = removeListener;
Socket.send = send;
Socket.emit = emit;
Socket.CONNECTED = CONNECTED;
Socket.DISCONNECTED = DISCONNECTED;
@ -50,6 +51,11 @@ var CloudCmd, Util, DOM, CloudFunc, io;
socket.send(data);
}
function emit(channel, data) {
if (socket)
socket.emit(channel, data);
}
function setListeners(all, socket) {
var i, n, name, func, listeners;

143
lib/client/terminal.js Normal file
View file

@ -0,0 +1,143 @@
var CloudCmd, Util, DOM, CloudFunc, Terminal;
(function(CloudCmd, Util, DOM, CloudFunc) {
'use strict';
CloudCmd.Terminal = TerminalProto;
function TerminalProto(CallBack) {
var Name = 'Terminal',
Loading,
Element,
MouseBinded,
Term,
Key = CloudCmd.Key,
Images = DOM.Images,
Notify = DOM.Notify,
CloudTerm = this;
function init() {
Loading = true;
Util.loadOnLoad([
DOM.jqueryLoad,
CloudCmd.View,
load,
CloudCmd.Socket,
CloudTerm.show,
addListeners
]);
}
CloudTerm.show = show;
CloudTerm.write = write;
function show(callback) {
var socket = CloudCmd.Socket;
if (!Loading) {
Images.showLoad({top:true});
if (!Element) {
Element = DOM.anyload({
name : 'div',
id : 'terminal',
style : 'height :100%'
});
DOM.cssSet({
id : 'terminal-css',
inner : '#terminal, .terminal, #view {' +
'height' + ': 100%;' +
'}'
});
Term = new Terminal({
screenKeys: true,
cursorBlink: false,
cols: 80,
rows: 25
});
Term.open(Element);
}
CloudCmd.View.show(Element, function() {
Term.element.focus();
Terminal.brokenBold = true;
Util.exec(callback);
});
}
}
function write(data) {
Term.write(data);
}
function listener(event) {
var keyCode = event.keyCode,
ESC = Key.ESC;
if (keyCode === ESC)
CloudCmd.View.hide();
}
function addListeners(callback) {
var socket = CloudCmd.Socket,
size = {
cols: 80,
rows: 25
};
socket.on({
'terminal-data' : write
});
Term.on('keydown', listener);
Term.on('data', function(data) {
socket.emit('terminal-data', data);
});
//Term.resize(size.cols, size.rows);
socket.emit('terminal-resize', size);
Util.exec(callback);
}
function maxSize() {
var w = Term.element.clientWidth - (Term.offsetWidth - Term.clientWidth),
h = Term.element.clientHeight - (Term.offsetHeight - Term.clientHeight),
cols = Math.max(Math.floor(w), 10),
rows = Math.max(Math.floor(h), 10),
size = {
cols: cols,
rows: rows
};
return size;
}
function load(pCallBack) {
var dir = CloudCmd.LIBDIRCLIENT + 'terminal/',
path = dir + 'term.js';
Util.time(Name + ' load');
DOM.jsload(path, function() {
Util.timeEnd(Name + ' load');
Loading = false;
Util.exec(pCallBack);
});
}
init();
}
})(CloudCmd, Util, DOM, CloudFunc);

5866
lib/client/terminal/term.js Normal file

File diff suppressed because it is too large Load diff

View file

@ -24,6 +24,7 @@
AppCache = main.appcache,
Socket = main.socket,
Console = main.console,
Terminal = main.terminal,
zlib = main.zlib,
http = main.http,
@ -85,6 +86,7 @@
if (listen) {
status = 'on';
Console.init();
Terminal.init();
}
}

View file

@ -105,6 +105,7 @@
exports.pipe = pipe = srvrequire('pipe'),
exports.socket = srvrequire('socket'),
exports.console = srvrequire('console'),
exports.terminal = srvrequire('terminal'),
exports.express = srvrequire('express'),
exports.auth = srvrequire('auth').auth,
exports.appcache = srvrequire('appcache'),

View file

@ -13,6 +13,7 @@
exports.addListener = addListener;
exports.removeListener = removeListener;
exports.send = send;
exports.emit = emit;
exports.listen = listen;
function addListener(name, func, socket) {
@ -33,6 +34,11 @@
clientSocket.send(msg);
}
function emit(channel, message, clientSocket) {
if (clientSocket)
clientSocket.emit(channel, message);
}
/**
* function listen on servers port
* @pServer {Object} started server object

104
lib/server/terminal.js Normal file
View file

@ -0,0 +1,104 @@
(function() {
'use strict';
var main = global.cloudcmd.main,
socket = main.socket,
spawn = main.child_process.spawn,
pty = main.require('pty.js'),
Util = main.util,
path = main.path,
mainpackage = main.mainpackage,
CLOUDCMD = mainpackage.name,
ClientDirs = [],
Clients = [],
WIN32 = main.WIN32,
ConNum = 0;
/**
* function listen on servers port
* @pServer {Object} started server object
*/
exports.init = function() {
var ret;
if (pty)
ret = socket.on('connection', function(clientSocket) {
onConnection(clientSocket, function(data) {
socket.emit('terminal-data', data, clientSocket);
});
});
return ret;
};
function onConnection(clientSocket, callback) {
var msg, onDisconnect, resizeFunc, dataFunc, term;
++ConNum;
if (!Clients[ConNum]) {
log(ConNum, 'terminal connected');
term = getTerm(callback);
dataFunc = onData.bind(null, term);
resizeFunc = onResize.bind(null, term);
onDisconnect = function(conNum, term) {
Clients[conNum] = null;
log(conNum, 'terminal disconnected');
socket.removeListener('terminal-data', dataFunc, clientSocket);
socket.removeListener('terminal-resize', resizeFunc, clientSocket);
socket.removeListener('disconnect', onDisconnect, clientSocket);
}.bind(ConNum, term);
socket.on('terminal-data', dataFunc, clientSocket);
socket.on('terminal-resize', resizeFunc, clientSocket);
socket.on('disconnect', onDisconnect, clientSocket);
} else {
log(ConNum, ' in use. Reconnecting...\n');
socket.disconnect();
}
}
function onResize(term, size) {
term.resize(size.cols, size.rows);
}
function onData(term, data) {
term.write(data);
}
function getTerm(callback) {
var term = pty.spawn('bash', [], {
name: 'xterm-color',
cols: 80,
rows: 25,
cwd : process.env.HOME,
env : process.env
});
term.on('data', Util.exec.bind(Util, callback));
return term;
}
function log(pConnNum, pStr, pType) {
var lRet,
lType = ' ';
if (pStr) {
if (pType)
lType += pType + ':';
lRet = 'client #' + pConnNum + lType + pStr;
Util.log(lRet);
}
return lRet;
}
})();