2016-03-03 08:34:22 +00:00
|
|
|
/**
|
|
|
|
* Created by liuzheng on 3/3/16.
|
|
|
|
*/
|
2016-03-04 12:09:05 +00:00
|
|
|
var rowHeight = 1;
|
|
|
|
var colWidth = 1;
|
2016-03-04 12:07:05 +00:00
|
|
|
//function WSSHClient() {
|
|
|
|
//}
|
|
|
|
//WSSHClient.prototype._generateEndpoint = function (options) {
|
|
|
|
// console.log(options);
|
|
|
|
// if (window.location.protocol == 'https:') {
|
|
|
|
// var protocol = 'wss://';
|
|
|
|
// } else {
|
|
|
|
// var protocol = 'ws://';
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// var endpoint = protocol + document.URL.match(RegExp('//(.*?)/'))[1] + '/ws/terminal' + document.URL.match(/(\?.*)/);
|
|
|
|
// return endpoint;
|
|
|
|
//};
|
|
|
|
//WSSHClient.prototype.connect = function (options) {
|
|
|
|
// var endpoint = this._generateEndpoint(options);
|
|
|
|
//
|
|
|
|
// if (window.WebSocket) {
|
|
|
|
// this._connection = new WebSocket(endpoint);
|
|
|
|
// }
|
|
|
|
// else if (window.MozWebSocket) {
|
|
|
|
// this._connection = MozWebSocket(endpoint);
|
|
|
|
// }
|
|
|
|
// else {
|
|
|
|
// options.onError('WebSocket Not Supported');
|
|
|
|
// return;
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// this._connection.onopen = function () {
|
|
|
|
// options.onConnect();
|
|
|
|
// };
|
|
|
|
//
|
|
|
|
// this._connection.onmessage = function (evt) {
|
|
|
|
// var data = JSON.parse(evt.data.toString());
|
|
|
|
// if (data.error !== undefined) {
|
|
|
|
// options.onError(data.error);
|
|
|
|
// }
|
|
|
|
// else {
|
|
|
|
// options.onData(data.data);
|
|
|
|
// }
|
|
|
|
// };
|
|
|
|
//
|
|
|
|
// this._connection.onclose = function (evt) {
|
|
|
|
// options.onClose();
|
|
|
|
// };
|
|
|
|
//};
|
|
|
|
//
|
|
|
|
//WSSHClient.prototype.send = function (data) {
|
|
|
|
// this._connection.send(JSON.stringify({'data': data}));
|
|
|
|
//};
|
|
|
|
|
|
|
|
|
|
|
|
function openTerminal(options) {
|
2016-03-03 08:39:14 +00:00
|
|
|
if (window.location.protocol == 'https:') {
|
|
|
|
var protocol = 'wss://';
|
|
|
|
} else {
|
|
|
|
var protocol = 'ws://';
|
|
|
|
}
|
2016-03-03 08:34:22 +00:00
|
|
|
|
2016-03-04 12:21:20 +00:00
|
|
|
var endpoint = protocol + document.URL.match(RegExp('//(.*?)/'))[1] + '/ws/terminal' + document.URL.match(/(\?.*)/)[1];
|
2016-03-04 12:07:05 +00:00
|
|
|
var sock = new WebSocket(endpoint);
|
|
|
|
//var client = new WSSHClient();
|
2016-03-03 09:03:47 +00:00
|
|
|
var term = new Terminal({
|
|
|
|
rows: rowHeight,
|
|
|
|
cols: colWidth,
|
|
|
|
useStyle: true,
|
|
|
|
screenKeys: true
|
2016-03-03 08:39:14 +00:00
|
|
|
});
|
|
|
|
term.open();
|
2016-03-03 09:03:47 +00:00
|
|
|
term.on('data', function (data) {
|
2016-03-04 12:12:09 +00:00
|
|
|
sock.send(JSON.stringify({'data': data}))
|
2016-03-03 09:03:47 +00:00
|
|
|
});
|
2016-03-03 08:39:14 +00:00
|
|
|
$('.terminal').detach().appendTo('#term');
|
|
|
|
term.resize(80, 24);
|
|
|
|
term.write('Connecting...');
|
2016-03-04 12:07:05 +00:00
|
|
|
sock.onopen = function () {
|
|
|
|
// Erase our connecting message
|
|
|
|
term.write('\r');
|
|
|
|
};
|
|
|
|
sock.onclose = function () {
|
|
|
|
term.write('Connection Reset By Peer');
|
|
|
|
};
|
|
|
|
sock.onmessage= function (data) {
|
2016-03-04 12:29:54 +00:00
|
|
|
term.write(data.data.data);
|
2016-03-04 12:07:05 +00:00
|
|
|
};
|
|
|
|
sock.onerror= function () {
|
|
|
|
term.write('Connection Reset By Peer');
|
|
|
|
};
|
|
|
|
//client.connect($.extend(options, {
|
|
|
|
// onError: function (error) {
|
|
|
|
// term.write('Error: ' + error + '\r\n');
|
|
|
|
// },
|
|
|
|
// onConnect: function () {
|
|
|
|
// // Erase our connecting message
|
|
|
|
// term.write('\r');
|
|
|
|
// },
|
|
|
|
// onClose: function () {
|
|
|
|
// term.write('Connection Reset By Peer');
|
|
|
|
// },
|
|
|
|
// onData: function (data) {
|
|
|
|
// term.write(data);
|
|
|
|
// }
|
|
|
|
//}));
|
2016-03-03 08:39:14 +00:00
|
|
|
rowHeight = 0.0 + 1.00 * $('.terminal').height() / 24;
|
|
|
|
colWidth = 0.0 + 1.00 * $('.terminal').width() / 80;
|
2016-03-04 12:07:05 +00:00
|
|
|
return {'term': term, 'client': sock};
|
2016-03-03 08:39:14 +00:00
|
|
|
}
|
2016-03-03 08:34:22 +00:00
|
|
|
|
2016-03-03 08:39:14 +00:00
|
|
|
function resize() {
|
|
|
|
$('.terminal').css('width', window.innerWidth - 25);
|
|
|
|
console.log(window.innerWidth);
|
|
|
|
console.log(window.innerWidth - 10);
|
|
|
|
var rows = Math.floor(window.innerHeight / rowHeight) - 2;
|
|
|
|
var cols = Math.floor(window.innerWidth / colWidth) - 1;
|
2016-03-03 08:34:22 +00:00
|
|
|
|
2016-03-03 08:39:14 +00:00
|
|
|
return {rows: rows, cols: cols};
|
|
|
|
}
|
2016-03-03 08:34:22 +00:00
|
|
|
|
2016-03-03 08:39:14 +00:00
|
|
|
$(document).ready(function () {
|
|
|
|
var options = {};
|
2016-03-03 08:34:22 +00:00
|
|
|
|
2016-03-03 08:39:14 +00:00
|
|
|
$('#ssh').show();
|
|
|
|
var term_client = openTerminal(options);
|
|
|
|
console.log(rowHeight);
|
2016-03-03 08:34:22 +00:00
|
|
|
|
2016-03-03 08:39:14 +00:00
|
|
|
window.onresize = function () {
|
|
|
|
var geom = resize();
|
|
|
|
console.log(geom);
|
|
|
|
term_client.term.resize(geom.cols, geom.rows);
|
2016-03-04 12:22:46 +00:00
|
|
|
term_client.client.send(JSON.stringify({'data':{'resize': {'rows': geom.rows, 'cols': geom.cols}}}));
|
2016-03-03 08:39:14 +00:00
|
|
|
$('#ssh').show();
|
|
|
|
}
|
2016-03-03 08:34:22 +00:00
|
|
|
|
2016-03-03 08:39:14 +00:00
|
|
|
});
|