Skip to content

Commit

Permalink
fix: watchOS support - disable network monitoring, add client timeout
Browse files Browse the repository at this point in the history
  • Loading branch information
lawmicha committed Nov 23, 2023
1 parent f286f6f commit 270a8db
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}

Expand Down
31 changes: 31 additions & 0 deletions AppSyncRealTimeClient/Websocket/Starscream/StarscreamAdapter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ public class StarscreamAdapter: AppSyncWebsocketProvider {
}
}

let watchOSConnectivityTimer: CountdownTimer

public init() {
let serialQueue = DispatchQueue(label: "com.amazonaws.StarscreamAdapter.serialQueue")
let callbackQueue = DispatchQueue(
Expand All @@ -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?) {
Expand All @@ -49,6 +52,7 @@ public class StarscreamAdapter: AppSyncWebsocketProvider {
self.socket?.delegate = self
self.socket?.callbackQueue = self.callbackQueue
self.socket?.connect()
self.startWatchOSConnectivityTimer()
}
}

Expand All @@ -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
}
}

0 comments on commit 270a8db

Please sign in to comment.