Closed zephsinx closed 1 year ago
I've actually got my own note written already for handling this better :D
Keep in mind, the client attempts to connect automatically by default, unless you pass in { immediate: false }
When you call connect() in your code, you are essentially calling it a second time. This is trying to close/kill any existing connections, hence the error. This is what I intend to clean up some.
Examples:
import { StreamerbotClient } from '@streamerbot/client';
// Client will automatically try to connect() and attempt reconnection until Streamer.bot is online
const client = new StreamerbotClient();
// Client waits for you to call `connect()`
const client = new StreamerbotClient({ immediate: false });
await client.connect();
Thanks for a great report again - will try and get another update out today :)
Thanks for a great report again
Of course! I've had my fair share of struggles due to having issues reported to me with no details 😅
And interesting. That makes sense. Oddly enough, when I specify the { immediate: false }
option, and then call connect()
, I get the same issue when calling getActions()
(or any method that uses the websocket).
It doesn't seem like the connection ever actually completes/resolves.
import * as streamerbotClient from '@streamerbot/client';
const sbClient = new streamerbotClient.StreamerbotClient({ immediate: false });
await sbClient.connect();
await sbClient.getActions();
If I don't call await sbClient.connect()
in this case, I get the following error when calling getActions()
, which is probably obvious as the connection was not started.
node:internal/process/esm_loader:94
internalBinding('errors').triggerUncaughtException(
^
{
message: 'WebSocket request timeout exceeded',
request: { request: 'GetActions' }
}
throw new Error('WebSocket is not open: readyState 0 (CONNECTING)');
^
Error: WebSocket is not open: readyState 0 (CONNECTING)
at WebSocket.send (E:\Projects\Software\streamerbot-tools\sb-client\node_modules\ws\lib\websocket.js:442:13)
at StreamerbotClient.send (file:///sb-client/node_modules/@streamerbot/client/dist/index.js:339:46)
at file:///sb-client/node_modules/@streamerbot/client/dist/index.js:388:16
at new Promise (<anonymous>)
at StreamerbotClient.<anonymous> (file:///sb-client/node_modules/@streamerbot/client/dist/index.js:358:9)
at Generator.next (<anonymous>)
at file:///sb-client/node_modules/@streamerbot/client/dist/index.js:37:61
at new Promise (<anonymous>)
at __async (file:///sb-client/node_modules/@streamerbot/client/dist/index.js:21:10)
at StreamerbotClient.request (file:///sb-client/node_modules/@streamerbot/client/dist/index.js:342:12)
Thanks again for the quick replies!
As of v1.0.2
, await connect()
should properly await open state, and await disconnect()
should properly await closed state.
Working for me in node v19.4.0
:
import { StreamerbotClient } from '@streamerbot/client';
const client = new StreamerbotClient({ host: 'aphex', immediate: false });
await client.connect();
const info = await client.getInfo();
console.log(`Connected to Streamer.bot`, info);
const actions = await client.getActions();
console.log('Actions received:', actions);
It works!! Thank you so much for pushing out the change! You're very quick and I appreciate it.
It's me again! Thank you so much for your quick reply and addressing of my last issue #1. I am now running into another issue when trying to get WebSockets running on a Node app.
The Error(s)
The issue seems to come down to the WebSocket connection closing too quickly before it can be used. Note the
_readyState
of2
(i.e. Closing) in the error below when usingconnect()
.Using
getActions()
Using
connect()
The Code
As before, the code is pretty simple.
Other Info
I've validated my connection settings and that my Streamer.bot client websocket server is started. In fact if I stop it, or change the host or port on the
StreamerbotClient()
, I get connection refused messages, or messages telling me the server is not started/does not exist.Thank you again in advance!