-
Notifications
You must be signed in to change notification settings - Fork 227
V5: Use web-socket client from StreamCore #3861
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
c40a170
48d707e
5616360
4fc353a
c1edb89
eb10341
be8a898
a9651f5
23725d8
43fd18e
7e38507
4bab5d5
68fa522
a34ca9d
3ce5e3d
9117dfd
3ba4f42
e267b66
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -22,21 +22,25 @@ class ConnectionRepository: @unchecked Sendable { | |
| set { connectionQueue.async(flags: .barrier) { self._connectionId = newValue }} | ||
| } | ||
|
|
||
| let webSocketConnectEndpoint = AllocatedUnfairLock<Endpoint<EmptyResponse>?>(nil) | ||
| let isClientInActiveMode: Bool | ||
| private let syncRepository: SyncRepository | ||
| private let webSocketEncoder: RequestEncoder? | ||
| private let webSocketClient: WebSocketClient? | ||
| private let apiClient: APIClient | ||
| private let timerType: TimerScheduling.Type | ||
|
|
||
| init( | ||
| isClientInActiveMode: Bool, | ||
| syncRepository: SyncRepository, | ||
| webSocketEncoder: RequestEncoder?, | ||
| webSocketClient: WebSocketClient?, | ||
| apiClient: APIClient, | ||
| timerType: TimerScheduling.Type | ||
| ) { | ||
| self.isClientInActiveMode = isClientInActiveMode | ||
| self.syncRepository = syncRepository | ||
| self.webSocketEncoder = webSocketEncoder | ||
| self.webSocketClient = webSocketClient | ||
| self.apiClient = apiClient | ||
| self.timerType = timerType | ||
|
|
@@ -80,6 +84,7 @@ class ConnectionRepository: @unchecked Sendable { | |
| } | ||
| } | ||
| } | ||
| updateWebSocketConnectURLRequest() | ||
| webSocketClient?.connect() | ||
| } | ||
|
|
||
|
|
@@ -114,29 +119,52 @@ class ConnectionRepository: @unchecked Sendable { | |
|
|
||
| /// Updates the WebSocket endpoint to use the passed token and user information for the connection | ||
| func updateWebSocketEndpoint(with token: Token, userInfo: UserInfo?) { | ||
| webSocketClient?.connectEndpoint = .webSocketConnect(userInfo: userInfo ?? .init(id: token.userId)) | ||
| webSocketConnectEndpoint.value = .webSocketConnect(userInfo: userInfo ?? .init(id: token.userId)) | ||
| } | ||
|
|
||
| /// Updates the WebSocket endpoint to use the passed user id | ||
| func updateWebSocketEndpoint(with currentUserId: UserId) { | ||
| webSocketClient?.connectEndpoint = .webSocketConnect(userInfo: UserInfo(id: currentUserId)) | ||
| webSocketConnectEndpoint.value = .webSocketConnect(userInfo: UserInfo(id: currentUserId)) | ||
| } | ||
|
|
||
| private func updateWebSocketConnectURLRequest() { | ||
| guard let webSocketClient, let webSocketEncoder, let webSocketConnectEndpoint = webSocketConnectEndpoint.value else { return } | ||
| let request: URLRequest? = { | ||
| do { | ||
| return try webSocketEncoder.encodeRequest(for: webSocketConnectEndpoint) | ||
| } catch { | ||
| log.error(error.localizedDescription, error: error) | ||
| return nil | ||
| } | ||
| }() | ||
| guard let request else { return } | ||
| webSocketClient.connectRequest = request | ||
| } | ||
|
|
||
| func handleConnectionUpdate( | ||
| state: WebSocketConnectionState, | ||
| onExpiredToken: () -> Void | ||
| ) { | ||
| let event = ConnectionStatusUpdated(webSocketConnectionState: state) | ||
| if event.connectionStatus != connectionStatus { | ||
| // Publish Connection event with the new state | ||
| webSocketClient?.publishEvent(event) | ||
| } | ||
|
Comment on lines
+148
to
+152
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Used to be in WebSocketClient. Since this is a custom event, I left it here instead. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. makes sense |
||
|
|
||
| connectionStatus = .init(webSocketConnectionState: state) | ||
|
|
||
| // We should notify waiters if connectionId was obtained (i.e. state is .connected) | ||
| // or for .disconnected state except for disconnect caused by an expired token | ||
| let shouldNotifyConnectionIdWaiters: Bool | ||
| let connectionId: String? | ||
| switch state { | ||
| case let .connected(connectionId: id): | ||
| case let .connected(healthCheckInfo: healthCheckInfo): | ||
| shouldNotifyConnectionIdWaiters = true | ||
| connectionId = id | ||
| case let .disconnected(source) where source.serverError?.isExpiredTokenError == true: | ||
| connectionId = healthCheckInfo.connectionId | ||
| syncRepository.syncLocalState { | ||
| log.info("Local state sync completed", subsystems: .offlineSupport) | ||
| } | ||
|
Comment on lines
+164
to
+166
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Was in WebSocketClient, now it is here There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yes, this is chat specific logic |
||
| case let .disconnected(source) where source.serverError?.isTokenExpiredError == true: | ||
| onExpiredToken() | ||
| shouldNotifyConnectionIdWaiters = false | ||
| connectionId = nil | ||
|
|
@@ -146,7 +174,7 @@ class ConnectionRepository: @unchecked Sendable { | |
| case .initialized, | ||
| .connecting, | ||
| .disconnecting, | ||
| .waitingForConnectionId: | ||
| .authenticating: | ||
| shouldNotifyConnectionIdWaiters = false | ||
| connectionId = nil | ||
| } | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Endpoint used to be in WebSocketClient, but the one from StreamCore does not use endpoint type, so this logic was moved to here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes, and it would make it easier to migrate away from endpoints to generated code.