Skip to content

Commit 5474b52

Browse files
authored
Merge pull request #33 from Adesin-fr/master
2 parents 8856362 + 7646411 commit 5474b52

File tree

1 file changed

+53
-36
lines changed

1 file changed

+53
-36
lines changed

js-src/Websocket.ts

+53-36
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,27 @@ export class Websocket {
2121
private socketId: string;
2222

2323
private closing = false;
24+
private hasConnected = false;
2425

2526
private pingInterval: NodeJS.Timeout;
2627

2728
private connect(host: string): void {
28-
this.options.debug && console.log('Connecting');
29+
this.options.debug && console.log('Trying to connect...');
2930

3031
this.websocket = new WebSocket(host)
3132

33+
this.websocket.onerror = () => {
34+
if (!this.hasConnected) {
35+
setTimeout(() => {
36+
this.socketId = undefined
37+
this.connect(host)
38+
}, 3000);
39+
}
40+
}
41+
3242
this.websocket.onopen = () => {
43+
this.options.debug && console.log('Connected !');
44+
this.hasConnected = true;
3345
this.send({
3446
event: 'whoami',
3547
})
@@ -41,66 +53,71 @@ export class Websocket {
4153

4254
this.buffer.splice(0, 1)
4355
}
44-
}
4556

46-
this.websocket.onmessage = (messageEvent: MessageEvent) => {
47-
const message = this.parseMessage(messageEvent.data)
57+
// Register events only once connected, or they won't be registered if connection failed/lost
4858

49-
if (!message) {
50-
return
51-
}
59+
this.websocket.onmessage = (messageEvent: MessageEvent) => {
60+
const message = this.parseMessage(messageEvent.data)
61+
this.options.debug && console.log('onmessage', messageEvent.data)
62+
63+
if (!message) {
64+
return
65+
}
66+
67+
if (message.channel) {
68+
this.options.debug && console.log(`Received event ${message.event} on channel ${message.channel}`)
5269

53-
if (message.channel) {
54-
this.options.debug && console.log(`Received event ${message.event} on channel ${message.channel}`)
70+
if (this.listeners[message.channel] && this.listeners[message.channel][message.event]) {
71+
this.listeners[message.channel][message.event](message.data)
72+
}
5573

56-
if (this.listeners[message.channel] && this.listeners[message.channel][message.event]) {
57-
this.listeners[message.channel][message.event](message.data)
74+
return
5875
}
5976

60-
return
77+
if (this.internalListeners[message.event]) {
78+
this.internalListeners[message.event](message.data)
79+
}
6180
}
6281

63-
if (this.internalListeners[message.event]) {
64-
this.internalListeners[message.event](message.data)
65-
}
6682

83+
// send ping every 60 seconds to keep connection alive
84+
this.pingInterval = setInterval(() => {
85+
if (this.websocket.readyState === this.websocket.OPEN) {
86+
this.options.debug && console.log('Sending ping')
87+
this.send({
88+
event: 'ping',
89+
})
90+
}
91+
}, 60 * 1000)
6792
}
6893

6994

7095
this.websocket.onclose = () => {
71-
if (this.socketId && !this.closing || !this.socketId) {
72-
this.options.debug && console.info('Connection lost, reconnecting...');
73-
setTimeout(() => {
74-
this.socketId = undefined
75-
this.connect(host)
76-
}, 1000);
96+
this.options.debug && console.info('Connection closed.');
97+
if (this.closing){
98+
return;
7799
}
100+
this.hasConnected = false
101+
this.options.debug && console.info('Connection lost, reconnecting...');
102+
setTimeout(() => {
103+
this.socketId = undefined
104+
this.connect(host)
105+
}, 1000);
78106
};
79107

80108
this.on('whoami', ({ socket_id: socketId }) => {
81109
this.socketId = socketId
82110

83111
this.options.debug && console.log(`just set socketId to ${socketId}`)
84112

85-
while (this.channelBacklog.length) {
86-
const channel = this.channelBacklog[0]
87-
113+
// Handle the backlog and don't empty it, we'll need it if we lose connection
114+
let channel: Channel;
115+
for(channel of this.channelBacklog){
88116
this.actuallySubscribe(channel)
89-
90-
this.channelBacklog.splice(0, 1)
91117
}
92-
})
93118

119+
})
94120

95-
// send ping every 60 seconds to keep connection alive
96-
this.pingInterval = setInterval(() => {
97-
if (this.websocket.readyState === this.websocket.OPEN) {
98-
this.options.debug && console.log('Sending ping')
99-
this.send({
100-
event: 'ping',
101-
})
102-
}
103-
}, 60 * 1000)
104121

105122
}
106123

0 commit comments

Comments
 (0)