From f184b6920fe7e0e11e6ed0d5a8f368bdb229c291 Mon Sep 17 00:00:00 2001 From: Adam Fowler Date: Sat, 23 Mar 2024 16:22:48 +0000 Subject: [PATCH] Clients should not close the TCP connection Instead they should wait for the server to initiate the connection close --- Sources/HummingbirdWebSocket/WebSocketHandler.swift | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/Sources/HummingbirdWebSocket/WebSocketHandler.swift b/Sources/HummingbirdWebSocket/WebSocketHandler.swift index ed1bc61..f45b3d8 100644 --- a/Sources/HummingbirdWebSocket/WebSocketHandler.swift +++ b/Sources/HummingbirdWebSocket/WebSocketHandler.swift @@ -194,7 +194,12 @@ actor WebSocketHandler: Sendable { var buffer = context.allocator.buffer(capacity: 2) buffer.write(webSocketErrorCode: code) try await outbound.write(frame: .init(fin: true, opcode: .connectionClose, data: buffer)) - outbound.finish() + // Only server should initiate a connection close. Clients should wait for the + // server to close the connection when it receives the WebSocket close packet + // See https://www.rfc-editor.org/rfc/rfc6455#section-7.1.1 + if self.type == .server { + outbound.finish() + } } }