Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save pawansingh00/2508c409fc63cfdbc19f62369bf1c4bf to your computer and use it in GitHub Desktop.
Save pawansingh00/2508c409fc63cfdbc19f62369bf1c4bf to your computer and use it in GitHub Desktop.
tokbox_reconnection.js
session.connect(token, function (error, data) {
if (error) {
console.error("Error connecting: ", error.code, error.message);
if (["OT_NOT_CONNECTED", "OT_CONNECT_FAILED", "OT_SOCKET_CLOSE_TIMEOUT"].includes(error.name)) {
// CODE TO UPDATE UI that connection Lost, trying to Reconnect.
showReconnectingUI();
if (!!window.chrome) {
let videoDetails = Session.get("videoDetails");
let tbSessionId = videoDetails?.sessionId;
setTimeout(
connect({
apiKey: apiKey,
token: token,
sessionId: tbSessionId,
}),
TOKBOX_RECONNECTION_RETRY_INTERVAL || 5000
);
}
}
// Error codes that needs to be Handled
// if (error.name === "OT_NOT_CONNECTED") {
// console.log("You are not connected to the internet. Check your network connection.");
// }
// 1004 - Authentication error. Check the error message for details. This error can result if you pass in an expired token when trying to connect to a session. It can also occur if you pass in an invalid token or API key. Make sure that you are generating the token using the current version of one of the OpenTok server SDKs.
// 1005 - Invalid Session ID. Make sure you generate the session ID using the current version of one of the OpenTok server SDKs.
// 1006 - "OT_CONNECT_FAILED" - Connect Failed. Unable to connect to the session.You may want to have the client check the network connection.
// 4010 - OT_SOCKET_CLOSE_TIMEOUT - Timed out while waiting for the Rumor socket to connect.
return;
}
});
sessionDisconnectedHandler(event) {
if (event.reason === "networkDisconnected") {
// CODE TO UPDATE UI that connection Lost, trying to Reconnect.
showReconnectingUI();
setTimeout(
connect({
apiKey: apiKey,
token: token,
sessionId: tbSessionId,
}),
TOKBOX_RECONNECTION_RETRY_INTERVAL || 5000
);
}
}
streamDestroyed: function (event) {
console.log("STREAM DESTROYED +++++++++++++ event.reason :: ", event.reason);
if (event.stream.videoType === "camera") {
if (event.reason === "networkDisconnected") {
event.preventDefault();
let subscribers = instance.session.getSubscribersForStream(event.stream);
if (subscribers.length > 0) {
let subscriber = document.getElementById(subscribers[0].id);
// Display error message inside the Subscriber
// subscriber.innerHTML = 'Lost connection. This could be due to your internet connection '
// + 'or because the other party lost their connection.';
subscriber.innerHTML = `<div class="reconnecting" style="z-index: 50000;color: red;">
<div>Disconnected</div>
<img style="width: 1;height: 50px;" class="reconnecting-spinner" src="https://cloudinary-images-prod.s3.amazonaws.com/prod-uploaded-logos/general/loader.gif">
</div>`;
event.preventDefault(); // Prevent the Subscriber from being removed
}
} else {
// clear Stream View
deleteStreamView(event.stream.streamId);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment