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

Sip.js Reconnection Issue when Web socket connection Lost or Network connectin lost #1095

Open
m-yunus opened this issue Dec 9, 2024 · 3 comments

Comments

@m-yunus
Copy link

m-yunus commented Dec 9, 2024

I am facing an issue with reconnection handling in SIP.js when the WebSocket connection is lost or the network is disconnected. After a disconnection, the onDisconnect delegate is triggered, but the UserAgent doesn't reconnect properly, and I can't maintain registration automatically.
const transportOptions = {
server: "wss:///ws",
traceSip: true,
iceCheckingTimeout: 35000,
register: true,
stunServers: ["stun:stun.l.google.com:19302"],
turnServers: [
{
urls: "t",
username: "",
credential: "",
},
],
connectionTimeout: 5,
maxReconnectionAttempts : 100,
reconnectionTimeout: 4,
keepAliveInterval: 10
};
const handleRegister = (event) => {
const uri = UserAgent.makeURI(sip:${sipUri}@);
const userAgentOptions = {
authorizationUsername: sipUri,
authorizationPassword: "",
transportOptions,
uri,
delegate: {
onInvite,
onDisconnect: (error) => {
alert("disconnected")
console.log("Network connectivity lost");
if (!error) {
console.log("User agent stopped");
}
},
onConnect: () => {
alert("connected")
handleRegister();
console.log("Network connectivity established");
},

}

};

const ua = new UserAgent(userAgentOptions);
setUserAgent(ua);

ua.start()
.then(() => {
const registerer = new Registerer(ua);
return registerer.register({
requestDelegate: {
onAccept(response) {
isRegistering = false;
alert("registerred successfully")
console.log("UserAgent registered successfully", response);
},
onReject(sip) {
isRegistering = false; // Reset flag on failure
console.log("UserAgent registration failed", sip);

    },
  },
});

})
.catch((error) => {
isRegistering = false;
console.error("Error starting the UserAgent:", error);
});
} I’ve reviewed the SIP.js documentation and attempted to use the onDisconnect and onConnect delegates for handling reconnections, but I'm unable to achieve reliable re-registration.

Any guidance or recommendations would be greatly appreciated.

@greatapps
Copy link

Is there any solution on this, How to handle the same ?

@shibran
Copy link

shibran commented Jan 30, 2025

You have to call reconnection method inside onDisconnect delegate. Like,

const reconnectionAttempts = 3;
const reconnectionDelay = 4;
let attemptingReconnection = false;
let shouldBeConnected = true;

userAgent.delegate.onDisconnect = (error) => {
  if (error) {
    attemptReconnection();
  }
};

const attemptReconnection = (reconnectionAttempt = 1)=> {
  // If not intentionally connected, don't reconnect.
  if (!shouldBeConnected) {
    return;
  }

  // Reconnection attempt already in progress
  if (attemptingReconnection) {
    return;
  }

  // Reconnection maximum attempts reached
  if (reconnectionAttempt > reconnectionAttempts) {
    return;
  }

  // We're attempting a reconnection
  attemptingReconnection = true;

  setTimeout(() => {
    // If not intentionally connected, don't reconnect.
    if (!shouldBeConnected) {
      attemptingReconnection = false;
      return;
    }
    // Attempt reconnect
    userAgent.reconnect()
      .then(() => {
        // Reconnect attempt succeeded
        attemptingReconnection = false;
      })
      .catch((error) => {
        // Reconnect attempt failed
        attemptingReconnection = false;
        attemptReconnection(++reconnectionAttempt);
      });
  }, reconnectionAttempt === 1 ? 0 : reconnectionDelay * 1000);
};

@m-yunus
Copy link
Author

m-yunus commented Feb 5, 2025

i dont hear anything on when an outgoing call not hearting early media any issue on this

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants