Simple Linux Panel
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
mdserver-web/route/static/js/term-websocketio.js

114 lines
2.9 KiB

2 years ago
function Terms_WebSocketIO (el, config) {
if (typeof config == "undefined") {
config = {};
}
this.el = el;
this.id = config.ssh_info.id || '';
this.ws = null; //websocket对象
this.route = 'webssh_websocketio'; // 访问的方法
this.term = null; //term对象
this.info = null; // 请求数据
this.last_body = null;
this.fontSize = 14; //终端字体大小
this.ssh_info = config.ssh_info;
this.term_timer = null;
2 years ago
this.callback_close = null;
2 years ago
this.run();
}
Terms_WebSocketIO.prototype = {
2 years ago
registerCloseCallBack:function(callback){
this.callback_close = callback;
},
2 years ago
connectWs: function (callback) {
2 years ago
if(!this.ws){
this.ws = io.connect();
}
},
2 years ago
connectSsh:function(){
this.send(this.ssh_info);
this.send('\n');
},
close:function(){
this.ws.disconnect();
this.ws.close();
2 years ago
},
on_message: function (ws_event) {
this.term.write(ws_event.data);
2 years ago
console.log(ws_event.data);
2 years ago
if (ws_event.data == '\r\n登出\r\n' || ws_event.data == '登出\r\n' ||
ws_event.data == '\r\nlogout\r\n' || ws_event.data == 'logout\r\n'||
ws_event.data == '\r\nexit\r\n' || ws_event.data == 'exit\r\n') {
this.term.destroy();
2 years ago
this.ws.disconnect();
this.ws.close();
2 years ago
clearInterval(this.term_timer);
2 years ago
2 years ago
if (this.callback_close){
this.callback_close();
}
2 years ago
}
},
2 years ago
on_connect:function(ws_event){
},
on_exit:function(ws_event){
},
send:function(data){
this.ws.emit(this.route, data);
},
resize: function (size) {
if (this.ws) {
size['resize'] = 1;
this.send(size)
}
2 years ago
},
run: function (ssh_info) {
2 years ago
this.connectWs();
2 years ago
var that = this;
var termCols = 83;
var termRows = 21;
2 years ago
this.term = new Terminal({fontSize: this.fontSize,screenKeys: true, useStyle: true});
2 years ago
this.term.open($('#'+this.id)[0]);
this.term.setOption('cursorBlink', true);
this.ws.on('server_response', function (ev) { that.on_message(ev)});
2 years ago
this.ws.on('connect', function (ev) { that.on_connect(ev)});
this.ws.on('exit', function (ev) { that.on_exit(ev)});
2 years ago
if (this.ws) {
2 years ago
that.send('');
2 years ago
this.term_timer = setInterval(function () {
2 years ago
that.send('');
}, 600);
2 years ago
}
this.term.on('data', function (data) {
2 years ago
try {
that.send(data)
} catch (e) {
that.term.write('\r\n连接丢失,正在尝试重新连接!\r\n');
that.connectSsh();
}
2 years ago
});
2 years ago
that.term.write('\r\n请稍等,正在链接中...\r\n');
this.connectSsh();
2 years ago
this.term.focus();
}
}