opentok / opentok-web-samples

Sample applications for using OpenTok.js
MIT License
196 stars 255 forks source link

Invalid state transition: Event 'disconnect' not possible in state 'disconnected' #101

Closed Noido closed 1 year ago

Noido commented 4 years ago

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

msach22 commented 4 years ago

@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?

Noido commented 4 years ago

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

msach22 commented 4 years ago

@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;
  },
});
Nasmar commented 4 years ago

I have the same issue; sessionDisconnected does not seem to do the job for my application. Application uses openTok @2.16.0.

davideberlein commented 4 years ago

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. image

Nasmar commented 4 years ago

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.

msach22 commented 4 years ago

@davideberlein Can you please share your flow for this error?

davideberlein commented 4 years ago

@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"));
}
duprez commented 4 years ago

Any updates on this issue?

davideberlein commented 4 years ago

@duprez I'm still waiting for an answer from @msach22 or anyone else from opentok

mmubasher commented 3 years ago

I am facing this issue still and even support is acting dumb at Vonage. Need a fix for this asap. Thanks @msach22

SardukarSilver commented 3 years ago

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);
  };
mmubasher commented 3 years ago

As a last resort, we had to switch to the browser based sdk. It seems the issues exists in NPM package only

danilrafiqi commented 3 years ago

i get same issue, when using forceDisconnect(param)

bryandowning commented 3 years ago

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]);
sunilmanipara commented 3 years ago

I think it may be help!

if (session.currentState === 'connected') { session.off(); session.disconnect(); session.destroy(); session.unpublish(publisher); publisher.destroy(); session.unsubscribe('subscriber'); }