From eeff2ab215f63e0254630bb5850ef2bd2d505b1b Mon Sep 17 00:00:00 2001 From: liuzheng712 Date: Thu, 3 Mar 2016 16:34:22 +0800 Subject: [PATCH 1/6] fix(webterminal): test --- static/js/webterminal.js | 101 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 101 insertions(+) create mode 100644 static/js/webterminal.js diff --git a/static/js/webterminal.js b/static/js/webterminal.js new file mode 100644 index 000000000..5b146e918 --- /dev/null +++ b/static/js/webterminal.js @@ -0,0 +1,101 @@ +/** + * Created by liuzheng on 3/3/16. + */ +var rowHeight = 1; + var colWidth = 1; + function WSSHClient() { + } + + WSSHClient.prototype.connect = function(options) { + var endpoint = '{{ web_terminal_url }}'; + + 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) { + var client = new WSSHClient(); + var term = new Terminal(80, 24, function(key) { + client.send(key); + }); + term.open(); + $('.terminal').detach().appendTo('#term'); + term.resize(80, 24); + term.write('Connecting...'); + 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); + } + })); + rowHeight = 0.0 + 1.00 * $('.terminal').height() / 24 ; + colWidth = 0.0 + 1.00 * $('.terminal').width() / 80; + return {'term': term, 'client': client}; + } + + 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; + + return {rows: rows, cols: cols}; + } + + $(document).ready(function() { + var options = { + }; + + $('#ssh').show(); + var term_client = openTerminal(options); + console.log(rowHeight); + + window.onresize = function(){ + var geom = resize(); + console.log(geom); + term_client.term.resize(geom.cols, geom.rows); + term_client.client.send({'resize': {'rows': geom.rows, 'cols': geom.cols}}); + $('#ssh').show(); + } + + }); \ No newline at end of file From b1f0297e8204035f39bf762d16d9c08619967988 Mon Sep 17 00:00:00 2001 From: liuzheng712 Date: Thu, 3 Mar 2016 16:35:48 +0800 Subject: [PATCH 2/6] fix(web terminal): test --- templates/jlog/web_terminal.html | 105 +------------------------------ 1 file changed, 1 insertion(+), 104 deletions(-) diff --git a/templates/jlog/web_terminal.html b/templates/jlog/web_terminal.html index 7dec79102..deb9fb4c9 100644 --- a/templates/jlog/web_terminal.html +++ b/templates/jlog/web_terminal.html @@ -36,109 +36,6 @@ - - - + From 0e7a8c1a627435da863b8637dabdbe9f0433f6a2 Mon Sep 17 00:00:00 2001 From: liuzheng712 Date: Thu, 3 Mar 2016 16:39:14 +0800 Subject: [PATCH 3/6] fix(webterminal): test --- static/js/webterminal.js | 177 ++++++++++++++++++++------------------- 1 file changed, 93 insertions(+), 84 deletions(-) diff --git a/static/js/webterminal.js b/static/js/webterminal.js index 5b146e918..6647ca8e6 100644 --- a/static/js/webterminal.js +++ b/static/js/webterminal.js @@ -2,100 +2,109 @@ * Created by liuzheng on 3/3/16. */ var rowHeight = 1; - var colWidth = 1; - function WSSHClient() { - } +var colWidth = 1; +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 = '{{ web_terminal_url }}'; + var endpoint = protocol + window.location.host + ':8080' + '/terminal'; + 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 ; - } + 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.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.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(); - }; - }; + this._connection.onclose = function (evt) { + options.onClose(); + }; +}; - WSSHClient.prototype.send = function(data) { - this._connection.send(JSON.stringify({'data': data})); - }; +WSSHClient.prototype.send = function (data) { + this._connection.send(JSON.stringify({'data': data})); +}; - function openTerminal(options) { - var client = new WSSHClient(); - var term = new Terminal(80, 24, function(key) { - client.send(key); - }); - term.open(); - $('.terminal').detach().appendTo('#term'); - term.resize(80, 24); - term.write('Connecting...'); - 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); - } - })); - rowHeight = 0.0 + 1.00 * $('.terminal').height() / 24 ; - colWidth = 0.0 + 1.00 * $('.terminal').width() / 80; - return {'term': term, 'client': client}; - } +function openTerminal(options) { + var client = new WSSHClient(); + var term = new Terminal(80, 24, function (key) { + client.send(key); + }); + term.open(); + $('.terminal').detach().appendTo('#term'); + term.resize(80, 24); + term.write('Connecting...'); + 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); + } + })); + rowHeight = 0.0 + 1.00 * $('.terminal').height() / 24; + colWidth = 0.0 + 1.00 * $('.terminal').width() / 80; + return {'term': term, 'client': client}; +} - 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; +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; - return {rows: rows, cols: cols}; - } + return {rows: rows, cols: cols}; +} - $(document).ready(function() { - var options = { - }; +$(document).ready(function () { + var options = {}; - $('#ssh').show(); - var term_client = openTerminal(options); - console.log(rowHeight); + $('#ssh').show(); + var term_client = openTerminal(options); + console.log(rowHeight); - window.onresize = function(){ - var geom = resize(); - console.log(geom); - term_client.term.resize(geom.cols, geom.rows); - term_client.client.send({'resize': {'rows': geom.rows, 'cols': geom.cols}}); - $('#ssh').show(); - } + window.onresize = function () { + var geom = resize(); + console.log(geom); + term_client.term.resize(geom.cols, geom.rows); + term_client.client.send({'resize': {'rows': geom.rows, 'cols': geom.cols}}); + $('#ssh').show(); + } - }); \ No newline at end of file +}); \ No newline at end of file From dbb4904513e1829a4c59f49a15d5aa738ad712ec Mon Sep 17 00:00:00 2001 From: liuzheng712 Date: Thu, 3 Mar 2016 16:41:41 +0800 Subject: [PATCH 4/6] fix(webterminal): test --- static/js/webterminal.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/static/js/webterminal.js b/static/js/webterminal.js index 6647ca8e6..ca16cd8e8 100644 --- a/static/js/webterminal.js +++ b/static/js/webterminal.js @@ -13,7 +13,7 @@ WSSHClient.prototype._generateEndpoint = function (options) { var protocol = 'ws://'; } - var endpoint = protocol + window.location.host + ':8080' + '/terminal'; + var endpoint = protocol + document.URL.match(RegExp('//(.*?)/'))[1] + '/ws/terminal'; return endpoint; }; WSSHClient.prototype.connect = function (options) { From 32519f8eaeaf35d72eccdef8dbf4a9bb28e52b00 Mon Sep 17 00:00:00 2001 From: liuzheng712 Date: Thu, 3 Mar 2016 16:50:49 +0800 Subject: [PATCH 5/6] fix: --- static/js/webterminal.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/static/js/webterminal.js b/static/js/webterminal.js index ca16cd8e8..fd25d1257 100644 --- a/static/js/webterminal.js +++ b/static/js/webterminal.js @@ -13,7 +13,7 @@ WSSHClient.prototype._generateEndpoint = function (options) { var protocol = 'ws://'; } - var endpoint = protocol + document.URL.match(RegExp('//(.*?)/'))[1] + '/ws/terminal'; + var endpoint = protocol + document.URL.match(RegExp('//(.*?)/'))[1] + '/ws/terminal'+document.URL.match(/(\?.*)/); return endpoint; }; WSSHClient.prototype.connect = function (options) { From a816a7b149ea83be928ea6f80bb7f7d9bd00ec5f Mon Sep 17 00:00:00 2001 From: liuzheng712 Date: Thu, 3 Mar 2016 17:03:47 +0800 Subject: [PATCH 6/6] fix(webterminal): bug fix test --- static/js/webterminal.js | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/static/js/webterminal.js b/static/js/webterminal.js index fd25d1257..8d6942d9b 100644 --- a/static/js/webterminal.js +++ b/static/js/webterminal.js @@ -13,7 +13,7 @@ WSSHClient.prototype._generateEndpoint = function (options) { var protocol = 'ws://'; } - var endpoint = protocol + document.URL.match(RegExp('//(.*?)/'))[1] + '/ws/terminal'+document.URL.match(/(\?.*)/); + var endpoint = protocol + document.URL.match(RegExp('//(.*?)/'))[1] + '/ws/terminal' + document.URL.match(/(\?.*)/); return endpoint; }; WSSHClient.prototype.connect = function (options) { @@ -55,10 +55,16 @@ WSSHClient.prototype.send = function (data) { function openTerminal(options) { var client = new WSSHClient(); - var term = new Terminal(80, 24, function (key) { - client.send(key); + var term = new Terminal({ + rows: rowHeight, + cols: colWidth, + useStyle: true, + screenKeys: true }); term.open(); + term.on('data', function (data) { + client.send(data) + }); $('.terminal').detach().appendTo('#term'); term.resize(80, 24); term.write('Connecting...');