networked-aframe / naf-janus-adapter

networked-aframe Janus network adapter
Mozilla Public License 2.0
12 stars 9 forks source link

InvalidStateError: Failed to execute 'send' on 'RTCDataChannel': RTCDataChannel.readyState is not 'open' #60

Closed arpu closed 7 months ago

arpu commented 8 months ago

some times i get InvalidStateError: Failed to execute 'send' on 'RTCDataChannel': RTCDataChannel.readyState is not 'open'

should we check the cannel before send if readyState is open?

vincentfretin commented 8 months ago

You get the errors when the user disconnect I guess. Because initially it's waiting for readyState to be open before setting this.publisher. Yes, checking the readyState to be open before sending is probably a good idea here https://github.com/networked-aframe/naf-janus-adapter/blob/01c83c75e17758d8184f422c5b90150f4e0b33ae/src/index.js#L994 and here https://github.com/networked-aframe/naf-janus-adapter/blob/01c83c75e17758d8184f422c5b90150f4e0b33ae/src/index.js#L1012 PR welcome.

https://developer.mozilla.org/en-US/docs/Web/API/RTCDataChannel/readyState

With websocket set for reliableTransport and unreliableTransport

adapter.reliableTransport = "websocket";
adapter.unreliableTransport = "websocket";

I don't get the RTCDataChannel.readyState is not 'open' errors but I get another one

"minijanus.js:111 Uncaught (in promise) Error: Janus session was disposed."

it can be ignored of course, but it's spamming sentry. It's related to naf updates continuing to be generated after a little while after disconnecting the user. On my leave button I do

    const scene = document.querySelector("a-scene");
    // Stop the animation loop first to prevent any naf messages to be sent and disconnect after a delay
    scene?.renderer?.setAnimationLoop(null);
    // Wait a bit to be sure messages are sent and acknowledged
    // to avoid error "minijanus.js:111 Uncaught (in promise) Error: Janus session was disposed."
    setTimeout(() => {
      const scene = document.querySelector("a-scene");
      if (scene) {
        // Removing networked-scene will call NAF.connection.adapter.disconnect()
        scene.removeAttribute("networked-scene");
        scene.parentNode?.removeChild(scene);
      }
    }, 2000);

I think I got ride of those errors except in the case it calls reconnect(). I'll probably modify minijanus to not log an error, or just a warning for this case.

arpu commented 8 months ago

maybe for websocket we shoud look at https://developer.mozilla.org/en-US/docs/Web/API/WebSocket/readyState

vincentfretin commented 8 months ago

Oh you may be right, if we check for readyState OPEN in a similar way for weboscket, this "Janus session was disposed." error emitted when rejecting all the pending transactions in minijanus may go away.

vincentfretin commented 7 months ago

PR for 3.0x branch #63 I'm testing it in prod for websocket transport to see if it removes completely the "Janus session was disposed." errors in my sentry.

vincentfretin commented 7 months ago

I also removed my setAnimationLoop(null) hack and 2s delay.

vincentfretin commented 7 months ago

I'm testing a build with @babel/preset-env removed (just removed .babelrc) to not include regenerator-runtime, transforming arrow functions, const, let, that shouldn't be needed nowadays. I'll do a separate PR for this after #63 is merged.

vincentfretin commented 7 months ago

The last error I got was 7 days ago and I think it was someone having the old js in cache because I didn't force an url change. I didn't have an error since then.