Commit 10a202c6 authored by ibuler's avatar ibuler

[Update] 修改支持neffos ws 重连

parent fd36ddf8
...@@ -4,7 +4,7 @@ import * as io from 'socket.io-client'; ...@@ -4,7 +4,7 @@ import * as io from 'socket.io-client';
import * as neffos from 'neffos.js'; import * as neffos from 'neffos.js';
import {Terminal} from 'xterm'; import {Terminal} from 'xterm';
// const abc = io.connect('/ssh'); // const abc = io.connect('/ssh');
import {getWsSock, Socket} from './utils/socket'; import {Socket} from './utils/socket';
const scheme = document.location.protocol === 'https:' ? 'wss' : 'ws'; const scheme = document.location.protocol === 'https:' ? 'wss' : 'ws';
const port = document.location.port ? ':' + document.location.port : ''; const port = document.location.port ? ':' + document.location.port : '';
...@@ -139,21 +139,17 @@ export let Browser: { ...@@ -139,21 +139,17 @@ export let Browser: {
vendor: navigator.vendor, vendor: navigator.vendor,
}; };
export let wsEvent: {
event: string;
data: any;
};
export const i18n = new Map(); export const i18n = new Map();
export async function getWsSocket() { export async function getWsSocket() {
if (TermWS) { if (TermWS) {
return TermWS; return TermWS;
} }
TermWS = await getWsSock(wsURL, 'ssh'); TermWS = new Socket(wsURL, 'ssh');
if (!TermWS) { const nsConn = await TermWS.connect();
if (!nsConn) {
console.log('Try to using socket.io protocol'); console.log('Try to using socket.io protocol');
TermWS = io.connect('/ssh'); TermWS = io.connect('/ssh', {reconnectionAttempts: 10});
} }
DataStore.socket = TermWS; DataStore.socket = TermWS;
return TermWS; return TermWS;
......
import {EventEmitter} from 'events/events'; import {EventEmitter} from 'events/events';
import {NSConn, marshal} from 'neffos.js'; import {Conn, NSConn, marshal} from 'neffos.js';
import * as neffos from 'neffos.js'; import * as neffos from 'neffos.js';
export class Socket { export class Socket {
conn: NSConn; conn: Conn;
nsConn: NSConn;
emitter: EventEmitter; emitter: EventEmitter;
url: string;
namespace: string;
constructor(conn: NSConn, emitter: EventEmitter) { constructor(url: string, namespace: string) {
this.conn = conn; this.url = url;
this.emitter = emitter; this.namespace = namespace;
}
emit(type: string, obj: any) {
const msg = marshal(obj);
this.conn.emit(type, msg);
}
on(type: string, fn: Function, opt_scope?: any, opt_oneshot?: boolean) {
this.emitter.on(type, fn, opt_scope, opt_oneshot);
} }
}
export async function getWsSock(url: string, namespace: string): Promise<Socket> { async connect() {
const emitter = new EventEmitter(); const emitter = new EventEmitter();
this.emitter = emitter;
const events = { const events = {
}; };
let interval; let interval = null;
events[namespace] = { events[this.namespace] = {
_OnNamespaceConnected: function (ns, msg) { _OnNamespaceConnected: (ns, msg) => {
emitter.emit('connect', ns); emitter.emit('connect', ns);
if (ns.conn.wasReconnected()) {
this.conn = ns.conn;
this.nsConn = ns;
console.log('Ws was reconnected');
}
interval = setInterval(() => ns.emit('ping', ''), 10000); interval = setInterval(() => ns.emit('ping', ''), 10000);
}, },
...@@ -52,15 +50,33 @@ export async function getWsSock(url: string, namespace: string): Promise<Socket> ...@@ -52,15 +50,33 @@ export async function getWsSock(url: string, namespace: string): Promise<Socket>
}; };
const options = { const options = {
reconnect: 5000, reconnect: 5000,
headers: {
'X-Namespace': 'ssh'
},
}; };
const conn = <neffos.Conn>await neffos.dial(url, events, options) this.conn = <Conn>await neffos.dial(this.url, events, options)
.catch(err => { .catch(err => {
console.log('connect to neffos ws error: ', err);
return null; return null;
}); });
if (!conn) { if (!this.conn) {
return null; return null;
} }
const nsConn = <neffos.NSConn>await conn.connect(namespace); this.nsConn = <NSConn> await this.conn.connect(this.namespace)
const sock = new Socket(nsConn, emitter); .catch(err => {
return sock; console.log('connect to namespace error: ', err);
return null;
});
return this.nsConn;
}
emit(type: string, obj: any) {
const msg = marshal(obj);
this.nsConn.emit(type, msg);
}
on(type: string, fn: Function, opt_scope?: any, opt_oneshot?: boolean) {
this.emitter.on(type, fn, opt_scope, opt_oneshot);
}
} }
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