|
|
|
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;
|
|
|
|
|
|
|
|
this.callback_close = null;
|
|
|
|
this.run();
|
|
|
|
}
|
|
|
|
|
|
|
|
Terms_WebSocketIO.prototype = {
|
|
|
|
|
|
|
|
registerCloseCallBack:function(callback){
|
|
|
|
this.callback_close = callback;
|
|
|
|
},
|
|
|
|
|
|
|
|
connectWs: function (callback) {
|
|
|
|
if(!this.ws){
|
|
|
|
this.ws = io.connect();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
connectSsh:function(){
|
|
|
|
this.send(this.ssh_info);
|
|
|
|
this.send('\n');
|
|
|
|
},
|
|
|
|
|
|
|
|
close:function(){
|
|
|
|
this.ws.disconnect();
|
|
|
|
this.ws.close();
|
|
|
|
},
|
|
|
|
|
|
|
|
on_message: function (ws_event) {
|
|
|
|
this.term.write(ws_event.data);
|
|
|
|
console.log(ws_event.data);
|
|
|
|
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();
|
|
|
|
this.ws.disconnect();
|
|
|
|
this.ws.close();
|
|
|
|
clearInterval(this.term_timer);
|
|
|
|
|
|
|
|
if (this.callback_close){
|
|
|
|
this.callback_close();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
},
|
|
|
|
run: function (ssh_info) {
|
|
|
|
this.connectWs();
|
|
|
|
|
|
|
|
var that = this;
|
|
|
|
var termCols = 83;
|
|
|
|
var termRows = 21;
|
|
|
|
this.term = new Terminal({fontSize: this.fontSize,screenKeys: true, useStyle: true});
|
|
|
|
|
|
|
|
this.term.open($('#'+this.id)[0]);
|
|
|
|
this.term.setOption('cursorBlink', true);
|
|
|
|
this.ws.on('server_response', function (ev) { that.on_message(ev)});
|
|
|
|
this.ws.on('connect', function (ev) { that.on_connect(ev)});
|
|
|
|
this.ws.on('exit', function (ev) { that.on_exit(ev)});
|
|
|
|
|
|
|
|
if (this.ws) {
|
|
|
|
that.send('');
|
|
|
|
this.term_timer = setInterval(function () {
|
|
|
|
that.send('');
|
|
|
|
}, 600);
|
|
|
|
}
|
|
|
|
|
|
|
|
this.term.on('data', function (data) {
|
|
|
|
try {
|
|
|
|
that.send(data)
|
|
|
|
} catch (e) {
|
|
|
|
that.term.write('\r\n连接丢失,正在尝试重新连接!\r\n');
|
|
|
|
that.connectSsh();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
that.term.write('\r\n请稍等,正在链接中...\r\n');
|
|
|
|
|
|
|
|
this.connectSsh();
|
|
|
|
this.term.focus();
|
|
|
|
}
|
|
|
|
}
|