Refactor: Extract WebSocketClientBase (DRY #63)#64
Open
shanselman wants to merge 1 commit intomasterfrom
Open
Conversation
Extract ~200 lines of duplicated WebSocket lifecycle code into a shared abstract base class. Both OpenClawGatewayClient and WindowsNodeClient now inherit from WebSocketClientBase. Shared in base class: - Connection lifecycle: ConnectAsync, ListenForMessagesAsync, ReconnectWithBackoffAsync - SendRawAsync (thread-safe with TOCTOU protection) - Dispose (defensive pattern, skip CTS dispose) - Fields: WebSocket, URL, token, credentials, CTS, backoff array - StatusChanged event with RaiseStatusChanged helper - Constructor: URL normalization, credential extraction, validation Subclass hooks (template method pattern): - ProcessMessageAsync (abstract) - Gateway wraps sync, Node uses async - ReceiveBufferSize (abstract) - Gateway 16KB, Node 64KB - ClientRole (abstract) - for log messages - OnConnectedAsync, OnDisconnected, OnError, OnDisposing (virtual) Safety improvements (Node's safer patterns adopted everywhere): - ObjectDisposedException catch in listen loop - Post-delay CTS check in reconnect - Try-catch around reconnect guard - Constructor argument validation 20 new tests via TestWebSocketClient test double. 596 total tests pass (503 shared + 93 tray). Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
WebSocket DRY Extraction (Issue #63)
Extracts ~200 lines of duplicated WebSocket lifecycle code from OpenClawGatewayClient and WindowsNodeClient into a shared abstract base class.
Design (Opus + Codex consensus)
ProcessMessageAsyncis always async (Task). Gateway wraps sync with Task.CompletedTask (zero-alloc cached singleton). Both models rejected async void.What moved to base class
Subclass hooks
Safety improvements
Adopted Node's safer patterns everywhere:
Tests
Stats
Closes #63