mirror of
https://github.com/coderaiser/cloudcmd.git
synced 2026-01-23 18:55:26 +00:00
feature(terminal) add room /terminal
This commit is contained in:
parent
270ae38b89
commit
cc7bbb4a57
3 changed files with 46 additions and 54 deletions
|
|
@ -15,6 +15,7 @@ var CloudCmd, Util, DOM, io;
|
|||
DISCONNECTED = 'terminal: socket disconnected\n';
|
||||
|
||||
Socket.on = addListener;
|
||||
Socket.connect = connect;
|
||||
Socket.addListener = addListener;
|
||||
Socket.removeListener = removeListener;
|
||||
Socket.send = send;
|
||||
|
|
@ -26,9 +27,6 @@ var CloudCmd, Util, DOM, io;
|
|||
function init(callback) {
|
||||
DOM.loadSocket(function() {
|
||||
Util.exec(callback);
|
||||
|
||||
if (!socket)
|
||||
connect();
|
||||
});
|
||||
}
|
||||
|
||||
|
|
@ -63,25 +61,22 @@ var CloudCmd, Util, DOM, io;
|
|||
});
|
||||
}
|
||||
|
||||
function connect() {
|
||||
var FIVE_SECONDS = 5000;
|
||||
function connect(room) {
|
||||
var href = location.origin,
|
||||
FIVE_SECONDS = 5000;
|
||||
|
||||
socket = io.connect({
|
||||
if (room)
|
||||
href += '/' + room;
|
||||
|
||||
socket = io.connect(href, {
|
||||
'max reconnection attempts' : Math.pow(2, 32),
|
||||
'reconnection limit' : FIVE_SECONDS
|
||||
});
|
||||
|
||||
if (socket.connected && AllListeners.connect)
|
||||
AllListeners.connect.forEach(function(func) {
|
||||
func();
|
||||
});
|
||||
|
||||
socket.on('connect', function () {
|
||||
log(CONNECTED);
|
||||
});
|
||||
|
||||
setListeners(AllListeners, socket);
|
||||
|
||||
socket.on('disconnect', function () {
|
||||
log(DISCONNECTED);
|
||||
});
|
||||
|
|
@ -91,8 +86,6 @@ var CloudCmd, Util, DOM, io;
|
|||
});
|
||||
}
|
||||
|
||||
init(callback);
|
||||
|
||||
return Socket;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
var CloudCmd, Util, DOM, CloudFunc, Terminal;
|
||||
var CloudCmd, Util, DOM, CloudFunc, Terminal, io;
|
||||
(function(CloudCmd, Util, DOM, CloudFunc) {
|
||||
'use strict';
|
||||
|
||||
|
|
@ -10,7 +10,6 @@ var CloudCmd, Util, DOM, CloudFunc, Terminal;
|
|||
Element,
|
||||
Term,
|
||||
Cell,
|
||||
Socket = CloudCmd.Socket,
|
||||
Images = DOM.Images,
|
||||
Size = {
|
||||
cols: 0,
|
||||
|
|
@ -29,13 +28,7 @@ var CloudCmd, Util, DOM, CloudFunc, Terminal;
|
|||
DOM.loadJquery,
|
||||
CloudCmd.View,
|
||||
load,
|
||||
CloudCmd.Socket,
|
||||
/* rm view keys, it ruin terminal */
|
||||
function(callback) {
|
||||
Socket = CloudCmd.Socket;
|
||||
|
||||
Util.exec(callback);
|
||||
},
|
||||
DOM.loadSocket,
|
||||
CloudTerm.show,
|
||||
addListeners
|
||||
]);
|
||||
|
|
@ -73,7 +66,7 @@ var CloudCmd, Util, DOM, CloudFunc, Terminal;
|
|||
|
||||
DOM.load.style({
|
||||
id : 'terminal-css',
|
||||
inner : '.view {' +
|
||||
inner : '.view, .terminal {' +
|
||||
'height' + ': 100%;' +
|
||||
'}' +
|
||||
'.terminal-cursor {' +
|
||||
|
|
@ -101,28 +94,34 @@ var CloudCmd, Util, DOM, CloudFunc, Terminal;
|
|||
}
|
||||
|
||||
function addListeners(callback) {
|
||||
var options = {
|
||||
'connect': function() {
|
||||
write(Socket.CONNECTED + '\r');
|
||||
},
|
||||
'disconnect': function() {
|
||||
write(Socket.DISCONNECTED +'\r');
|
||||
},
|
||||
};
|
||||
var href = location.origin,
|
||||
FIVE_SECONDS = 5000,
|
||||
|
||||
options[CHANNEL] = write;
|
||||
options[CHANNEL_RESIZE] = function(size) {
|
||||
socket = io.connect(href + '/terminal', {
|
||||
'max reconnection attempts' : Math.pow(2, 32),
|
||||
'reconnection limit' : FIVE_SECONDS
|
||||
});
|
||||
|
||||
socket.on('connect', function() {
|
||||
write('socket connected' + '\r');
|
||||
});
|
||||
|
||||
socket.on('disconnect', function() {
|
||||
write('socket disconnected' +'\r');
|
||||
});
|
||||
|
||||
socket.on(CHANNEL, write);
|
||||
|
||||
socket.on(CHANNEL_RESIZE, function(size) {
|
||||
Term.resize(size.cols, size.rows);
|
||||
};
|
||||
|
||||
Socket.on(options);
|
||||
});
|
||||
|
||||
Term.on('data', function(data) {
|
||||
Socket.emit(CHANNEL, data);
|
||||
socket.emit(CHANNEL, data);
|
||||
});
|
||||
|
||||
Term.on('resize', function(size) {
|
||||
Socket.emit(CHANNEL_RESIZE, size);
|
||||
socket.emit(CHANNEL_RESIZE, size);
|
||||
});
|
||||
|
||||
Util.exec(callback);
|
||||
|
|
|
|||
|
|
@ -3,8 +3,6 @@
|
|||
|
||||
var DIR = __dirname + '/../../',
|
||||
DIR_LIB = DIR + 'lib/',
|
||||
|
||||
socket = require('./socket'),
|
||||
|
||||
Util = require(DIR_LIB + 'util'),
|
||||
CloudFunc = require(DIR_LIB + 'cloudfunc'),
|
||||
|
|
@ -22,18 +20,20 @@
|
|||
|
||||
ConNum = 0;
|
||||
|
||||
module.exports = function(io) {
|
||||
var makePty = function(clientSocket) {
|
||||
onConnection(clientSocket, function(channel, data) {
|
||||
socket.emit(channel, data, clientSocket);
|
||||
module.exports = function(socket) {
|
||||
var makePty = function(socket) {
|
||||
onConnection(socket, function(channel, data) {
|
||||
socket.emit(channel, data);
|
||||
});
|
||||
};
|
||||
|
||||
if (pty)
|
||||
socket.on('connection', io, makePty);
|
||||
socket
|
||||
.of('/terminal')
|
||||
.on('connection', makePty);
|
||||
};
|
||||
|
||||
function onConnection(clientSocket, callback) {
|
||||
function onConnection(socket, callback) {
|
||||
var onDisconnect, resizeFunc, dataFunc, term;
|
||||
|
||||
++ConNum;
|
||||
|
|
@ -50,16 +50,16 @@
|
|||
|
||||
log(conNum, 'terminal disconnected');
|
||||
|
||||
socket.removeListener(CHANNEL, clientSocket, dataFunc);
|
||||
socket.removeListener(CHANNEL_RESIZE, clientSocket, resizeFunc);
|
||||
socket.removeListener('disconnect', clientSocket, onDisconnect);
|
||||
socket.removeListener(CHANNEL, dataFunc);
|
||||
socket.removeListener(CHANNEL_RESIZE, resizeFunc);
|
||||
socket.removeListener('disconnect', onDisconnect);
|
||||
|
||||
term.destroy();
|
||||
}.bind(null, ConNum, term);
|
||||
|
||||
socket.on(CHANNEL, clientSocket, dataFunc);
|
||||
socket.on(CHANNEL_RESIZE, clientSocket, resizeFunc);
|
||||
socket.on('disconnect', clientSocket, onDisconnect);
|
||||
socket.on(CHANNEL, dataFunc);
|
||||
socket.on(CHANNEL_RESIZE, resizeFunc);
|
||||
socket.on('disconnect', onDisconnect);
|
||||
} else {
|
||||
log(ConNum, ' in use. Reconnecting...\n');
|
||||
socket.disconnect();
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue