feat(ws): support for custom worker messaging (#10241)

This commit is contained in:
DD
2024-05-03 17:53:09 +03:00
committed by GitHub
parent 6cf094c282
commit 728164ed86
2 changed files with 24 additions and 1 deletions

View File

@@ -132,6 +132,10 @@ const manager = new WebSocketManager({
new WorkerShardingStrategy(manager, {
shardsPerWorker: 2,
workerPath: './worker.js',
// Optionally, if you you have custom messaging, like for analytic collection, you can use this:
async unknownPayloadHandler(data: any) {
// handle data here :3
},
}),
});
```
@@ -140,6 +144,7 @@ And your `worker.ts` file:
```ts
import { WorkerBootstrapper, WebSocketShardEvents } from '@discordjs/ws';
import { parentPort } from 'node:worker_threads';
const bootstrapper = new WorkerBootstrapper();
void bootstrapper.bootstrap({
@@ -158,6 +163,9 @@ void bootstrapper.bootstrap({
});
},
});
// This will go to `unknownPayloadHandler` in the main thread, or be ignored if not provided
parentPort!.postMessage({ custom: 'data' });
```
## Links