mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-17 20:13:30 +01:00
feat(ws): support for custom worker messaging (#10241)
This commit is contained in:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user