Skip to content

Commit 599d444

Browse files
committed
add camera view disconnection handling
1 parent 9c9e6c4 commit 599d444

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

AllSpark-ios/CameraViewController.swift

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1056,11 +1056,41 @@ extension CameraViewController {
10561056
self?.connectionAttemptTimer = nil
10571057
self?.isAttemptingConnection = false
10581058
self?.updateConnectionStatusIcon()
1059+
} else if self?.isConnected == true {
1060+
// Server was connected but became unavailable - mark as disconnected
1061+
print("Server disconnected unexpectedly")
1062+
self?.handleServerDisconnection()
10591063
}
10601064
}
10611065
}
10621066
}
10631067
}
1068+
1069+
private func handleServerDisconnection() {
1070+
isConnected = false
1071+
isAttemptingConnection = false
1072+
connectionAttemptTimer?.invalidate()
1073+
connectionAttemptTimer = nil
1074+
webSocketTask = nil
1075+
1076+
print("Updating UI to reflect server disconnection")
1077+
updateConnectionStatusIcon()
1078+
1079+
// Optionally show a notification to the user
1080+
let alert = UIAlertController(title: "Server Disconnected", message: "The connection to the server was lost. Please check your network and try reconnecting.", preferredStyle: .alert)
1081+
alert.addAction(UIAlertAction(title: "Reconnect", style: .default) { [weak self] _ in
1082+
self?.setupWebSocketConnection()
1083+
})
1084+
alert.addAction(UIAlertAction(title: "Dismiss", style: .cancel))
1085+
self.present(alert, animated: true)
1086+
1087+
// Attempt automatic reconnection after a delay
1088+
DispatchQueue.main.asyncAfter(deadline: .now() + 5.0) { [weak self] in
1089+
guard let self = self, !self.isConnected && !self.isAttemptingConnection else { return }
1090+
print("Attempting automatic reconnection after server disconnection...")
1091+
self.setupWebSocketConnection()
1092+
}
1093+
}
10641094
}
10651095

10661096
extension CameraViewController: AVCaptureVideoDataOutputSampleBufferDelegate, AVCaptureAudioDataOutputSampleBufferDelegate {

0 commit comments

Comments
 (0)