admin管理员组

文章数量:1123697

I'm using obs-websocket-js in a Tampermonkey userscript. This is more or less how it's defined, as minimal reproducible example:

let webSocketUrl = 'wss://link.to.ngrok.free.endpoint';
let websocketPassword = 'something';
const obs = new OBSWebSocket();

await connectToOBS();

async function connectToOBS() {
  try {
    await obs.connect(webSocketURL, webSocketPassword);
  } catch (error) {
    console.error(`Failed to connect to OBS: ${error.message || error.code}`);
  }
}

With that code I had 1006 errors quite often when refreshing a page with the script or going back in history. My reconnection mechanism also couldn't reconnect. So I expected the problem to be with hanging sessions, which OBS terminates too late and causes some connection conflicts. It turned out to be right as adding:

window.addEventListener('beforeunload', async () => {
  await obs.disconnect;
});

to the script almost got rid of the error. The problem is that it still appears very rarely.

What could other possible causes be? I suspected the disconnect erroring, but when debugging that it always closed the connection successfully. Any other ideas?

One more note is that there's nothing listen in the OBS websocket window when the fails happen, which would indicate there's in fact no session conflict anymore. It'd likely help to know what the 1006 is, but couldn't find how to get more debugging info about it.

本文标签: javascriptobswebsocketjs fails to connect through ngrok sometimes due to error 1006Stack Overflow