diff --git a/AppSyncRealTimeClient/ConnectionProvider/AppsyncRealtimeConnection/RealtimeConnectionProvider.swift b/AppSyncRealTimeClient/ConnectionProvider/AppsyncRealtimeConnection/RealtimeConnectionProvider.swift index 0cd88a17..f19ad0a8 100644 --- a/AppSyncRealTimeClient/ConnectionProvider/AppsyncRealtimeConnection/RealtimeConnectionProvider.swift +++ b/AppSyncRealTimeClient/ConnectionProvider/AppsyncRealtimeConnection/RealtimeConnectionProvider.swift @@ -85,7 +85,13 @@ public class RealtimeConnectionProvider: ConnectionProvider { self.serialCallbackQueue = serialCallbackQueue self.connectivityMonitor = connectivityMonitor + // On a physical watchOS device, it is showing "unsatisfied" despite connected to the internet + // according to https://developer.apple.com/forums/thread/729568 + // To avoid an incorrect network status state for the system, do not use connectivity monitor + // for watchOS apps. + #if !os(watchOS) connectivityMonitor.start(onUpdates: handleConnectivityUpdates(connectivity:)) + #endif if #available(iOS 13.0, macOS 10.15, tvOS 13.0, watchOS 6.0, *) { subscribeToLimitExceededThrottle() diff --git a/AppSyncRealTimeClient/ConnectionProvider/AppsyncRealtimeConnectionAsync/RealtimeConnectionProviderAsync.swift b/AppSyncRealTimeClient/ConnectionProvider/AppsyncRealtimeConnectionAsync/RealtimeConnectionProviderAsync.swift index 577f75b9..9d6a0e60 100644 --- a/AppSyncRealTimeClient/ConnectionProvider/AppsyncRealtimeConnectionAsync/RealtimeConnectionProviderAsync.swift +++ b/AppSyncRealTimeClient/ConnectionProvider/AppsyncRealtimeConnectionAsync/RealtimeConnectionProviderAsync.swift @@ -80,7 +80,14 @@ public class RealtimeConnectionProviderAsync: ConnectionProvider { self.serialCallbackQueue = serialCallbackQueue self.connectivityMonitor = connectivityMonitor + // On a physical watchOS device, it is showing "unsatisfied" despite connected to the internet + // according to https://developer.apple.com/forums/thread/729568 + // To avoid an incorrect network status state for the system, do not use connectivity monitor + // for watchOS apps. + #if !os(watchOS) connectivityMonitor.start(onUpdates: handleConnectivityUpdates(connectivity:)) + #endif + subscribeToLimitExceededThrottle() } diff --git a/AppSyncRealTimeClient/Websocket/Starscream/StarscreamAdapter.swift b/AppSyncRealTimeClient/Websocket/Starscream/StarscreamAdapter.swift index a75591c8..59fc9482 100644 --- a/AppSyncRealTimeClient/Websocket/Starscream/StarscreamAdapter.swift +++ b/AppSyncRealTimeClient/Websocket/Starscream/StarscreamAdapter.swift @@ -23,6 +23,8 @@ public class StarscreamAdapter: AppSyncWebsocketProvider { } } + let watchOSConnectivityTimer: CountdownTimer + public init() { let serialQueue = DispatchQueue(label: "com.amazonaws.StarscreamAdapter.serialQueue") let callbackQueue = DispatchQueue( @@ -32,6 +34,7 @@ public class StarscreamAdapter: AppSyncWebsocketProvider { self._isConnected = false self.serialQueue = serialQueue self.callbackQueue = callbackQueue + self.watchOSConnectivityTimer = CountdownTimer() } public func connect(urlRequest: URLRequest, protocols: [String], delegate: AppSyncWebsocketDelegate?) { @@ -49,6 +52,7 @@ public class StarscreamAdapter: AppSyncWebsocketProvider { self.socket?.delegate = self self.socket?.callbackQueue = self.callbackQueue self.socket?.connect() + self.startWatchOSConnectivityTimer() } } @@ -66,4 +70,31 @@ public class StarscreamAdapter: AppSyncWebsocketProvider { self.socket?.write(string: message) } } + + private func startWatchOSConnectivityTimer() { + #if os(watchOS) + let watchOSConnectTimeoutInSeconds = TimeInterval(5) + AppSyncLogger.debug( + "[StarscreamAdapter] Starting connectivity timer for watchOS for \(watchOSConnectTimeoutInSeconds)s." + ) + self.watchOSConnectivityTimer.start(interval: watchOSConnectTimeoutInSeconds) { + AppSyncLogger.debug( + "[StarscreamAdapter] watchOS connectivity timer is up." + ) + self.serialQueue.async { + if !self._isConnected { + AppSyncLogger.debug( + "[StarscreamAdapter] Subscriptions not connected after \(watchOSConnectTimeoutInSeconds)s. Manually disconnecting" + ) + let error = ConnectionProviderError.connection + self.delegate?.websocketDidDisconnect(provider: self, error: error) + } else { + AppSyncLogger.debug( + "[StarscreamAdapter] Subscriptions are connected within \(watchOSConnectTimeoutInSeconds)s." + ) + } + } + } + #endif + } }