fix(webterminal): test

pull/112/head
liuzheng712 2016-03-03 16:39:14 +08:00
parent b1f0297e82
commit 0e7a8c1a62
1 changed files with 93 additions and 84 deletions

View File

@ -2,100 +2,109 @@
* Created by liuzheng on 3/3/16. * Created by liuzheng on 3/3/16.
*/ */
var rowHeight = 1; var rowHeight = 1;
var colWidth = 1; var colWidth = 1;
function WSSHClient() { function WSSHClient() {
} }
WSSHClient.prototype._generateEndpoint = function (options) {
console.log(options);
if (window.location.protocol == 'https:') {
var protocol = 'wss://';
} else {
var protocol = 'ws://';
}
WSSHClient.prototype.connect = function(options) { var endpoint = protocol + window.location.host + ':8080' + '/terminal';
var endpoint = '{{ web_terminal_url }}'; return endpoint;
};
WSSHClient.prototype.connect = function (options) {
var endpoint = this._generateEndpoint(options);
if (window.WebSocket) { if (window.WebSocket) {
this._connection = new WebSocket(endpoint); this._connection = new WebSocket(endpoint);
} }
else if (window.MozWebSocket) { else if (window.MozWebSocket) {
this._connection = MozWebSocket(endpoint); this._connection = MozWebSocket(endpoint);
} }
else { else {
options.onError('WebSocket Not Supported'); options.onError('WebSocket Not Supported');
return ; return;
} }
this._connection.onopen = function() { this._connection.onopen = function () {
options.onConnect(); options.onConnect();
}; };
this._connection.onmessage = function (evt) { this._connection.onmessage = function (evt) {
var data = JSON.parse(evt.data.toString()); var data = JSON.parse(evt.data.toString());
if (data.error !== undefined) { if (data.error !== undefined) {
options.onError(data.error); options.onError(data.error);
} }
else { else {
options.onData(data.data); options.onData(data.data);
} }
}; };
this._connection.onclose = function(evt) { this._connection.onclose = function (evt) {
options.onClose(); options.onClose();
}; };
}; };
WSSHClient.prototype.send = function(data) { WSSHClient.prototype.send = function (data) {
this._connection.send(JSON.stringify({'data': data})); this._connection.send(JSON.stringify({'data': data}));
}; };
function openTerminal(options) { function openTerminal(options) {
var client = new WSSHClient(); var client = new WSSHClient();
var term = new Terminal(80, 24, function(key) { var term = new Terminal(80, 24, function (key) {
client.send(key); client.send(key);
}); });
term.open(); term.open();
$('.terminal').detach().appendTo('#term'); $('.terminal').detach().appendTo('#term');
term.resize(80, 24); term.resize(80, 24);
term.write('Connecting...'); term.write('Connecting...');
client.connect($.extend(options, { client.connect($.extend(options, {
onError: function(error) { onError: function (error) {
term.write('Error: ' + error + '\r\n'); term.write('Error: ' + error + '\r\n');
}, },
onConnect: function() { onConnect: function () {
// Erase our connecting message // Erase our connecting message
term.write('\r'); term.write('\r');
}, },
onClose: function() { onClose: function () {
term.write('Connection Reset By Peer'); term.write('Connection Reset By Peer');
}, },
onData: function(data) { onData: function (data) {
term.write(data); term.write(data);
} }
})); }));
rowHeight = 0.0 + 1.00 * $('.terminal').height() / 24 ; rowHeight = 0.0 + 1.00 * $('.terminal').height() / 24;
colWidth = 0.0 + 1.00 * $('.terminal').width() / 80; colWidth = 0.0 + 1.00 * $('.terminal').width() / 80;
return {'term': term, 'client': client}; return {'term': term, 'client': client};
} }
function resize(){ function resize() {
$('.terminal').css('width', window.innerWidth-25); $('.terminal').css('width', window.innerWidth - 25);
console.log(window.innerWidth); console.log(window.innerWidth);
console.log(window.innerWidth-10); console.log(window.innerWidth - 10);
var rows = Math.floor(window.innerHeight/rowHeight) - 2; var rows = Math.floor(window.innerHeight / rowHeight) - 2;
var cols = Math.floor(window.innerWidth/colWidth) - 1; var cols = Math.floor(window.innerWidth / colWidth) - 1;
return {rows: rows, cols: cols}; return {rows: rows, cols: cols};
} }
$(document).ready(function() { $(document).ready(function () {
var options = { var options = {};
};
$('#ssh').show(); $('#ssh').show();
var term_client = openTerminal(options); var term_client = openTerminal(options);
console.log(rowHeight); console.log(rowHeight);
window.onresize = function(){ window.onresize = function () {
var geom = resize(); var geom = resize();
console.log(geom); console.log(geom);
term_client.term.resize(geom.cols, geom.rows); term_client.term.resize(geom.cols, geom.rows);
term_client.client.send({'resize': {'rows': geom.rows, 'cols': geom.cols}}); term_client.client.send({'resize': {'rows': geom.rows, 'cols': geom.cols}});
$('#ssh').show(); $('#ssh').show();
} }
}); });