Skip to content

Commit 85fb57a

Browse files
committed
fix: trigger onOpen callback in Swift WebSocket after upgrade completes
Previously, the onOpen callback was not being triggered when a WebSocket connection was established. This was due to a lifecycle timing issue where the MessageHandler's channelActive method was never called. The issue occurred because: 1. HTTP channel becomes active first 2. HTTP-to-WebSocket upgrade happens 3. MessageHandler is added to the pipeline after the channel is already active 4. channelActive only fires when a handler is added to an inactive channel The fix explicitly triggers the onOpen callback in the upgradePipelineHandler method after successfully adding the MessageHandler to the pipeline, which is exactly when the WebSocket connection is fully established. This ensures the onOpen callback works correctly in all SDKs that depend on this Swift WebSocket client, including the Apple (iOS/macOS) SDK.
1 parent 5970def commit 85fb57a

File tree

1 file changed

+7
-1
lines changed

1 file changed

+7
-1
lines changed

templates/swift/Sources/WebSockets/WebSocketClient.swift.twig

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,13 @@ public class WebSocketClient {
297297
self.channel = channel
298298
}
299299

300-
return channel.pipeline.addHandler(handler)
300+
return channel.pipeline.addHandler(handler).map {
301+
if let delegate = self.delegate {
302+
delegate.onOpen(channel: channel)
303+
} else {
304+
self.onOpen(channel)
305+
}
306+
}
301307
}
302308

303309
// MARK: - Close connection

0 commit comments

Comments
 (0)