1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import {EventEmitter} from 'events/events';
import {Conn, NSConn, marshal} from 'neffos.js';
import * as neffos from 'neffos.js';
export class Socket {
conn: Conn;
nsConn: NSConn;
emitter: EventEmitter;
url: string;
namespace: string;
constructor(url: string, namespace: string) {
this.url = url;
this.namespace = namespace;
}
async connect() {
const emitter = new EventEmitter();
emitter.setMaxListeners(20);
this.emitter = emitter;
const events = {
};
let interval = null;
events[this.namespace] = {
_OnNamespaceConnected: (ns, msg) => {
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);
},
_OnNamespaceDisconnect: function (ns, msg) {
emitter.emit('disconnect', ns);
if (interval) {
clearInterval(interval);
}
},
_OnAnyEvent: function (ns, msg) {
let data = '';
if (msg.Body) {
data = msg.unmarshal();
}
emitter.emit(msg.Event, data);
},
};
const options = {
reconnect: 5000,
headers: {
'X-Namespace': 'ssh'
},
};
this.conn = <Conn>await neffos.dial(this.url, events, options)
.catch(err => {
console.log('connect to neffos ws error: ', err);
return null;
});
if (!this.conn) {
return null;
}
this.nsConn = <NSConn> await this.conn.connect(this.namespace)
.catch(err => {
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);
}
}