Skip to content

Commit f1fa1b3

Browse files
committed
fix: ensure onClose triggers on unexpected disconnections
Added channelInactive handler to MessageHandler to catch connection drops that don't send a proper WebSocket close frame. This ensures the onClose callback fires in all disconnect scenarios: - Network failures - Server crashes - Connection timeouts - Any scenario where the channel becomes inactive All close events are propagated to the onClose callback, ensuring the Realtime service's reconnection logic triggers correctly.
1 parent 85fb57a commit f1fa1b3

File tree

1 file changed

+10
-1
lines changed

1 file changed

+10
-1
lines changed

templates/swift/Sources/WebSockets/MessageHandler.swift.twig

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,16 @@ class MessageHandler {
3131
extension MessageHandler: ChannelInboundHandler, RemovableChannelHandler {
3232

3333
typealias InboundIn = WebSocketFrame
34-
34+
35+
public func channelInactive(context: ChannelHandlerContext) {
36+
let emptyData = Data()
37+
if let delegate = client.delegate {
38+
delegate.onClose(channel: context.channel, data: emptyData)
39+
} else {
40+
client.onClose(context.channel, emptyData)
41+
}
42+
}
43+
3544
public func channelRead(context: ChannelHandlerContext, data: NIOAny) {
3645
let frame = self.unwrapInboundIn(data)
3746
switch frame.opcode {

0 commit comments

Comments
 (0)