使用ConnectyCube视频通话切换网络后会话中断的重连方案咨询
When switching between Wi-Fi and mobile data, ConnectyCube's built-in reconnection often fails—here's a manual approach to get it working:
1. Detect Network Changes First
You can't rely on ConnectyCube to catch every network transition, so implement platform-level listeners to trigger your reconnection logic:
Android
Use ConnectivityManager to watch for network availability:
val connectivityManager = context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager connectivityManager.registerDefaultNetworkCallback(object : ConnectivityManager.NetworkCallback() { override fun onAvailable(network: Network) { // Run reconnection on main thread Handler(Looper.getMainLooper()).post { reconnectVideoCall() } } })
iOS
Use NWPathMonitor to track network status:
let pathMonitor = NWPathMonitor() pathMonitor.pathUpdateHandler = { path in guard path.status == .satisfied else { return } DispatchQueue.main.async { self.reconnectVideoCall() } } pathMonitor.start(queue: DispatchQueue.global(qos: .utility))
2. Manual Reconnection Flow
Skip the built-in functions and roll your own sequence:
Pause media first
Don't destroy the session immediately—pause streams to avoid state corruption:// Android example ConnectyCube.video.pauseCall(session)Refresh session token
Fetch a new session token to ensure validity with the new network:ConnectyCube.createSession { newSession, error -> if (error == null) { // Proceed with reconnection } }Reinitialize peer connection with fresh ICE servers
Outdated ICE servers are a common culprit. Fetch new ones and rebuild the peer connection:// iOS example ConnectyCube.video.getICEServers { iceServers, error in guard let servers = iceServers else { return } let config = RTCConfiguration() config.iceServers = servers // Create new RTC peer connection with this config let peerConnection = ConnectyCube.video.createPeerConnection(config) // Reattach local media streams to this new peer connection }Reattach media streams
Recreate your local camera/audio streams and attach them to the new peer connection. Then trigger a renegotiation to sync with the remote user.Signal the remote peer
Use ConnectyCube's chat API to send a custom message (like "RECONNECT_REQUEST") to the other participant, so they can also reinitialize their end of the connection.
3. Critical Tips
- Avoid calling
destroyCallright away—keeping the session context cuts down on reconnection time. - Add a cooldown period between reconnection attempts to prevent loops if the network is unstable.
- Double-check permissions (camera, mic) after reinitializing streams—some platforms reset permissions on network changes.
内容的提问来源于stack exchange,提问作者Manish Nithin

