Skip to content

Commit 72d950e

Browse files
committed
more reliable disconnects?
1 parent 3bdb937 commit 72d950e

File tree

4 files changed

+17
-6
lines changed

4 files changed

+17
-6
lines changed

src/Client.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,18 +99,20 @@ class Client {
9999
/**
100100
* Send a message to the client.
101101
* @param {import('ws').Data} data The data to send.
102+
* @returns {boolean} true if written
102103
*/
103104
send(data) {
104105
if (this.ws === null) {
105106
this.log('Cannot send message; ws is null');
106-
return;
107+
return false;
107108
}
108109
if (this.ws.readyState !== this.ws.OPEN) {
109110
this.log('Cannot send message; readyState ' + this.ws.readyState);
110-
return;
111+
return false;
111112
}
112113
this.isDraining = true;
113114
this.ws.send(data, this.handleSent);
115+
return true;
114116
}
115117

116118
handleSent() {

src/ConnectionManager.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ class ConnectionManager {
4646
}
4747
}
4848

49+
const timedOut = [];
4950
this.clientBuckets[bucketNumber].forEach((client) => {
5051
if (!client.ws) {
5152
client.timedOut('no ws');
@@ -69,6 +70,10 @@ class ConnectionManager {
6970
// Clients are sent a ping, and expected to respond to the ping by the time the next ping will be sent.
7071
client.ping();
7172
});
73+
74+
for (const client of timedOut) {
75+
this.handleDisconnect(client);
76+
}
7277
}
7378

7479
getNextClientBucket () {
@@ -92,6 +97,7 @@ class ConnectionManager {
9297
* @param {Client} client The Client disconnecting.
9398
*/
9499
handleDisconnect(client) {
100+
client.room?.removeClient(client);
95101
this.clientBuckets[client.bucket].delete(client);
96102
this.allClients.delete(client);
97103
}

src/Room.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,12 +68,11 @@ class Room {
6868
/**
6969
* Remove a client.
7070
* @param {Client} client The client to remove
71-
* @throws Will throw if the client is not part of this room.
7271
*/
7372
removeClient(client) {
7473
const index = this.clients.indexOf(client);
7574
if (index === -1) {
76-
throw new Error(`Client is not part of room ${this.id}`);
75+
return;
7776
}
7877
this.clients.splice(index, 1);
7978
this.lastDisconnectTime = Date.now();

src/server.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,10 @@ function sendBuffered() {
7676
dataToSend += createSetMessage(name, value);
7777
}
7878
stats.recordBytesSent(client, dataToSend.length);
79-
client.send(dataToSend);
8079
client.bufferedVariableSets.clear();
80+
if (!client.send(dataToSend)) {
81+
connectionManager.handleDisconnect(client);
82+
}
8183
}
8284
}
8385

@@ -87,7 +89,9 @@ function sendSetMessageToClient(client, name, value) {
8789
} else {
8890
const dataToSend = createSetMessage(name, value);
8991
stats.recordBytesSent(client, dataToSend.length);
90-
client.send(dataToSend);
92+
if (!client.send(dataToSend)) {
93+
connectionManager.handleDisconnect(client);
94+
}
9195
}
9296
}
9397

0 commit comments

Comments
 (0)