Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Auto reconnect when the connection is disconnected or fails #11

Open
manishsaxenavidealpha opened this issue Nov 8, 2023 · 1 comment
Labels
bug Something isn't working help wanted Extra attention is needed question Further information is requested

Comments

@manishsaxenavidealpha
Copy link

thanks, for the great🌟 repo on webRTC in flutter. I

'm working on this for months, and facing some problems. (I'm working on flutter web)

  1. Sometimes 2 different systems doesn't connect to each other, sometimes need to hit 3-4 times joinRoom button for connection.

  2. Big problem-Sometimes connection establishes after then if it disconnected/ failed to connect in the mid of meeting, there should be a method of reconnectAgain. I tried to reconnect by putting joinRoom method but this approach is wrong or not working. here is the code:
    `
    void registerPeerConnectionListeners() {
    peerConnection?.onIceGatheringState = (RTCIceGatheringState state) => debugPrint('ICE gathering state changed: $state');

    peerConnection?.onConnectionState = (RTCPeerConnectionState state) {
    debugPrint('Connection state change: $state');
    if (state == RTCPeerConnectionState.RTCPeerConnectionStateFailed) {
    debugPrint('it is failed');
    // reconnectIfNeeded();
    } else if (state == RTCPeerConnectionState.RTCPeerConnectionStateConnected) {
    debugPrint('it is connected now');
    } else if (state == RTCPeerConnectionState.RTCPeerConnectionStateDisconnected) {
    debugPrint('it is RTCPeerConnectionStateDisconnected');
    // reconnectIfNeeded();
    }
    };

    peerConnection?.onSignalingState = (RTCSignalingState state) => debugPrint('Signaling state change: $state');

    peerConnection?.onIceGatheringState = (RTCIceGatheringState state) {
    debugPrint('ICE connection state change: $state');
    if (state == RTCIceGatheringState.RTCIceGatheringStateComplete) {
    debugPrint(' RTCIceGatheringState.RTCIceGatheringStateComplete');
    } else if (state == RTCIceGatheringState.RTCIceGatheringStateGathering) {
    debugPrint(' RTCIceGatheringState.RTCIceGatheringStateGathering');
    } else if (state == RTCIceGatheringState.RTCIceGatheringStateNew) {
    debugPrint(' RTCIceGatheringState. RTCIceGatheringStateNew');
    }
    };

    peerConnection?.onAddStream = (MediaStream stream) {
    debugPrint("Add remote stream");
    onAddRemoteStream?.call(stream);
    remoteStream = stream;
    };
    }

///////
void reconnectIfNeeded() {
if (peerConnection != null &&
(peerConnection!.connectionState == RTCPeerConnectionState.RTCPeerConnectionStateDisconnected ||
peerConnection!.connectionState == RTCPeerConnectionState.RTCPeerConnectionStateFailed)) {
debugPrint('Reconnecting...');
// joinRoom(); this is not working need to add another logic, how can I reconnect again
}
}
`
thanks in advance

@md-weber md-weber added bug Something isn't working help wanted Extra attention is needed question Further information is requested labels Nov 8, 2023
@aminetoktokmessai
Copy link

aminetoktokmessai commented Apr 19, 2024

Hello,
Here is my implementation for reconnecting, tested on android and chrome and does the job for me. Code might seem not optimized enough (not reusing old ice candidates, sdp offers...etc)
https://github.com/aminetoktokmessai/webrtc_tutorial/tree/main/lib

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working help wanted Extra attention is needed question Further information is requested
Projects
None yet
Development

No branches or pull requests

3 participants