Skip to content

Commit a6703ec

Browse files
committed
Prevent sending disconnect if disposed
1 parent 57a8186 commit a6703ec

1 file changed

Lines changed: 15 additions & 4 deletions

File tree

protocol.ts

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ export interface SocketOptions {
1313
}
1414

1515
export class Protocol extends PersistentProtocol {
16+
private disposed: boolean = false;
17+
1618
public constructor(
1719
secWebsocketKey: string,
1820
socket: net.Socket,
@@ -40,12 +42,21 @@ export class Protocol extends PersistentProtocol {
4042
].join("\r\n") + "\r\n\r\n");
4143
}
4244

45+
public sendDisconnect(): void {
46+
if (!this.disposed) {
47+
super.sendDisconnect();
48+
}
49+
}
50+
4351
public dispose(error?: Error): void {
44-
if (error) {
45-
this.sendMessage({ type: "error", reason: error.message });
52+
if (!this.disposed) {
53+
this.disposed = true;
54+
if (error) {
55+
this.sendMessage({ type: "error", reason: error.message });
56+
}
57+
super.dispose();
58+
this.getSocket().dispose();
4659
}
47-
super.dispose();
48-
this.getSocket().dispose();
4960
}
5061

5162
/**

0 commit comments

Comments
 (0)