opentok-web-samples
opentok-web-samples copied to clipboard
Invalid state transition: Event 'disconnect' not possible in state 'disconnected'
Hi I try use session.disconnect() for disconnect clients and i got this error
Invalid state transition: Event 'disconnect' not possible in state 'disconnected'
My session is already connect so i dont understand
@msach22 ?
Thanks
@Noido Hmm, are you listening for the sessionDisconnected
event? Can you confirm that you're calling session.disconnect
and have not received the event before?
yes sessionDisconnected
I have a function
function ready(fn) {
if (document.readyState != 'loading'){
fn();
} else {
document.addEventListener('DOMContentLoaded', fn);
}
}
ready(function() {
connectToSession();
});
function connectToSession() {
session = OT.initSession(apiKey, sessionId);
session.connect(token, function(err) {
if (err) {
console.error(err.message);
return alert('An error occurred while connecting to the OpenTok session');
}
isSessionConnected = true;
});
}
And i add a btn for disconnect but when i click i got this msg
$('#disconnect').on('click',function(e){
e.preventDefault();
if(isSessionConnected == true){
session.disconnect();
}
});
But got this error i dont understand why
@Noido You're basing the disconnect method off of your flag. Please listen to the session disconnected event by doing the following to toggle your state:
session.on({
sessionDisconnected: event => {
isSessionConnected = false;
},
});
I have the same issue; sessionDisconnected does not seem to do the job for my application. Application uses openTok @2.16.0.
We have the same issue with OpenTok.js 2.16.3 08194dfcf
You can see that we are calling session.disconnect()
first, then we get the sessionDisconnected event, and then we get the invalid state transition.
It helped me doing all the following, when wanting to disconnect:
session.off();
session.disconnect();
session.unpublish(publisher, handleError)
publisher.destroy();
session.unsubscribe(subscriber);
But only run those when isSessionConnected to avoid further errors.
@davideberlein Can you please share your flow for this error?
@msach22 this is the code ending a session:
function endSession() {
// an observable that resolves synchronously and holds the Session object returned by `OT.initSession()`
this.session$.take(1).subscribe(
session => {
session.off();
session.disconnect();
},
e => $_tk.log_message("Error disconnecting session"));
// an observable that resolves synchronously and holds the Publisher object returned by `OT.initPublisher()`
this.publisher$.take(1).subscribe(
publisher => publisher.destroy(),
e => $_tk.log_message("Error destroying publisher"));
}
Any updates on this issue?
@duprez I'm still waiting for an answer from @msach22 or anyone else from opentok
I am facing this issue still and even support is acting dumb at Vonage. Need a fix for this asap. Thanks @msach22
Same here( Pls fix it @msach22 This didn't help :
closeCall = () => {
this.session.off();
this.session.disconnect();
this.session.unpublish(this.publisher);
this.publisher.destroy();
this.session.unsubscribe(this.subscriber);
};
As a last resort, we had to switch to the browser based sdk. It seems the issues exists in NPM package only
i get same issue, when using forceDisconnect(param)
I got this working by manually unsubscribing each subscriber before disconnecting the session:
const disconnectSession = useCallback(() => {
if (tokboxSession) {
tokboxStreams.forEach((stream) => {
const subscribers = tokboxSession.getSubscribersForStream(stream);
subscribers.forEach((subscriber) => tokboxSession.unsubscribe(subscriber));
});
tokboxSession.disconnect();
}
}, [tokboxStreams, tokboxSession]);
I think it may be help!
if (session.currentState === 'connected') { session.off(); session.disconnect(); session.destroy(); session.unpublish(publisher); publisher.destroy(); session.unsubscribe('subscriber'); }