Skip to content

Commit

Permalink
Add watchOSConnectivtiyTimer
Browse files Browse the repository at this point in the history
  • Loading branch information
lawmicha committed Nov 14, 2023
1 parent 06e97e9 commit fd16761
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ extension RealtimeConnectionProvider: AppSyncWebsocketDelegate {
return
}
#if os(watchOS)
AppSyncLogger.debug(
"[RealtimeConnectionProvider] on watchOS, received disconnect."
)
self.updateCallback(event: .error(ConnectionProviderError.connection(
"This API uses low-level networking (websockets). Running on watchOS only works for specific circumstances.",
error)))
Expand Down
27 changes: 27 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,30 @@ public class StarscreamAdapter: AppSyncWebsocketProvider {
self.socket?.delegate = self
self.socket?.callbackQueue = self.callbackQueue
self.socket?.connect()
#if os(watchOS)
// On watchOS, if it takes longer than 5 seconds
// to connect, fail fast.
AppSyncLogger.debug(
"[StarscreamAdapter] Starting connectivity timer for watchOS for 3s"
)
self.watchOSConnectivityTimer.start(interval: 3) {
AppSyncLogger.debug(
"[StarscreamAdapter] watchOS connectivity timer is up."
)
if !self._isConnected {
AppSyncLogger.debug(
"[StarscreamAdapter] watchOS subscriptions not connected after 3s."
)
self.serialQueue.async {
self.delegate?.websocketDidDisconnect(provider: self, error: nil)
}
} else {
AppSyncLogger.debug(
"[StarscreamAdapter] watchOS subscriptions are connected within 3s."
)
}
}
#endif
}
}

Expand Down

0 comments on commit fd16761

Please sign in to comment.