Commit 38cb22ac by Sheng

Added more functions to wssh

parent 4ae2a336
...@@ -30,12 +30,18 @@ jQuery(function($){ ...@@ -30,12 +30,18 @@ jQuery(function($){
} }
wssh.window_size = function() { wssh.window_geometry = function() {
var geo = current_geometry(); // for console use
console.log('Current window size: ' + geo.cols + ',' + geo.rows); var geometry = current_geometry();
console.log('Current window geometry: ' + JSON.stringify(geometry));
}; };
function format_geometry(cols, rows) {
return JSON.stringify({'cols': cols, 'rows': rows});
}
function callback(msg) { function callback(msg) {
// console.log(msg); // console.log(msg);
if (msg.status) { if (msg.status) {
...@@ -67,7 +73,32 @@ jQuery(function($){ ...@@ -67,7 +73,32 @@ jQuery(function($){
term.on_resize(geometry.cols, geometry.rows); term.on_resize(geometry.cols, geometry.rows);
} }
wssh.websocket_send = function (data) {
// for console use
if (!sock) {
console.log('Websocket was already closed');
return;
}
if (typeof data !== 'string') {
console.log('Only string is allowed');
return;
}
try {
JSON.parse(data);
sock.send(data);
} catch (SyntaxError) {
sock.send(JSON.stringify({'data': data}));
}
};
wssh.set_encoding = function (new_encoding) { wssh.set_encoding = function (new_encoding) {
// for console use
if (new_encoding === undefined) {
return;
}
try { try {
test_decoder = new window.TextDecoder(new_encoding); test_decoder = new window.TextDecoder(new_encoding);
} catch(TypeError) { } catch(TypeError) {
...@@ -82,12 +113,18 @@ jQuery(function($){ ...@@ -82,12 +113,18 @@ jQuery(function($){
}; };
wssh.reset_encoding = function () { wssh.reset_encoding = function () {
// for console use
encoding = msg.encoding; encoding = msg.encoding;
console.log('Reset encoding to ' + msg.encoding); console.log('Reset encoding to ' + msg.encoding);
}; };
wssh.resize_terminal = function (raw_cols, raw_rows) { wssh.resize_terminal = function (raw_cols, raw_rows) {
// for console use // for console use
if (term === undefined) {
console.log('Terminal was already destroryed');
return;
}
var cols = parseInt(raw_cols, 10), var cols = parseInt(raw_cols, 10),
rows = parseInt(raw_rows, 10), rows = parseInt(raw_rows, 10),
valid_args = false; valid_args = false;
...@@ -100,15 +137,16 @@ jQuery(function($){ ...@@ -100,15 +137,16 @@ jQuery(function($){
} }
if (!valid_args) { if (!valid_args) {
console.log('Invalid arguments: ' + raw_cols + ',' + raw_rows); console.log('Unable to resize terminal to geometry: ' + format_geometry(cols, rows));
} else { } else {
term.on_resize(cols, rows); term.on_resize(cols, rows);
} }
}; };
term.on_resize = function (cols, rows) { term.on_resize = function (cols, rows) {
if (cols !== this.geometry[0] || rows !== this.geometry[1]) { if (cols !== this.geometry[0] || rows !== this.geometry[1]) {
console.log('Resizing terminal size to: ' + cols + ',' + rows); console.log('Resizing terminal to geometry: ' + format_geometry(cols, rows));
this.resize(cols, rows); this.resize(cols, rows);
sock.send(JSON.stringify({'resize': [cols, rows]})); sock.send(JSON.stringify({'resize': [cols, rows]}));
} }
...@@ -132,11 +170,13 @@ jQuery(function($){ ...@@ -132,11 +170,13 @@ jQuery(function($){
var decoder = new window.TextDecoder(encoding); var decoder = new window.TextDecoder(encoding);
var text = decoder.decode(reader.result); var text = decoder.decode(reader.result);
// console.log(text); // console.log(text);
if (term) {
term.write(text); term.write(text);
if (!term.resized) { if (!term.resized) {
resize_terminal(term); resize_terminal(term);
term.resized = true; term.resized = true;
} }
}
}; };
reader.readAsArrayBuffer(msg.data); reader.readAsArrayBuffer(msg.data);
...@@ -149,13 +189,17 @@ jQuery(function($){ ...@@ -149,13 +189,17 @@ jQuery(function($){
sock.onclose = function(e) { sock.onclose = function(e) {
console.log(e); console.log(e);
term.destroy(); term.destroy();
term = undefined;
sock = undefined;
$('.container').show(); $('.container').show();
status.text(e.reason); status.text(e.reason);
btn.prop('disabled', false); btn.prop('disabled', false);
}; };
$(window).resize(function(){ $(window).resize(function(){
if (term) {
resize_terminal(term); resize_terminal(term);
}
}); });
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment