This commit is contained in:
yumoqing 2025-02-06 16:44:10 +08:00
parent faca3f338d
commit 688e751091

View File

@ -6,17 +6,25 @@ bricks.Wterm = class extends bricks.JsWidget {
/* /*
{ {
ws_url: ws_url:
ping_timeout:19
} }
*/ */
constructor(opts){ constructor(opts){
super(opts); super(opts);
this.socket = null; this.socket = null;
this.ping_timeout = opts.ping_timeout || 19;
schedule_once(this.open.bind(this), 0.5); schedule_once(this.open.bind(this), 0.5);
} }
charsize_sizing(){ charsize_sizing(){
var cs = bricks.app.charsize; var cs = bricks.app.charsize;
this.term.setOption('fontSize', cs); this.term.setOption('fontSize', cs);
} }
heartbeat(){
if (this.socket.readyState != 1) return;
this.socket.send('_#_heartbeat_#_');
this.heartbeat_task = schedule_once(this.heartbeat.bind(this),
this.ping_timeout);
}
async open(){ async open(){
var term_options = this.term_options || {}; var term_options = this.term_options || {};
var term = new Terminal(term_options); var term = new Terminal(term_options);
@ -30,14 +38,25 @@ bricks.Wterm = class extends bricks.JsWidget {
this.fitAddon.fit(); this.fitAddon.fit();
this.charsize_sizing(); this.charsize_sizing();
this.bind('resize', this.term_resize.bind(this)) this.bind('resize', this.term_resize.bind(this))
ws.onmessage = msg => { ws.onmessage = event => {
term.write(JSON.parse(msg.data).data); var msg = JSON.parse(event.data);
console.log('ws msg=', msg);
if (msg.data == '_#_heartbeat_#_'){
console.log('connection alive');
} else {
term.write(msg.data);
}
}; };
ws.onclose = (event) => { ws.onclose = (event) => {
console.log('websocket closed:', event.code, '--', event.reason); console.log('websocket closed:', event.code, '--', event.reason);
} if (this.heartbeat_task) {
ws.onopen = function(){ this.heartbeat_task.cancel();
term.paste('ls -l\n') this.heartbeat_task = null;
}
};
ws.onopen = () => {
this.heartbeat_task = schedule_once(this.heartbeat.bind(this),
this.ping_timeout);
}; };
term.onData(function(key) { term.onData(function(key) {
//Enter //Enter