-
Notifications
You must be signed in to change notification settings - Fork 0
/
ws.ts
26 lines (21 loc) · 810 Bytes
/
ws.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
import { applyWSSHandler } from '@trpc/server/adapters/ws';
import WebSocket, { WebSocketServer as WSWebSocketServer } from 'ws';
import { appRouter } from '~/server/api/trpc/[trpc]';
export default defineNitroPlugin((nitro) => {
const WebSocketServer = WebSocket.Server || WSWebSocketServer;
const wss = new WebSocketServer({
port: 3002,
});
const handler = applyWSSHandler({ wss, router: appRouter });
wss.on('connection', (ws) => {
console.log(`➕➕ Connection (${wss.clients.size})`);
ws.once('close', () => {
console.log(`➖➖ Connection (${wss.clients.size})`);
});
});
console.log('✅ WebSocket Server listening on ws://localhost:3002');
nitro.hooks.hookOnce('close', async () => {
handler.broadcastReconnectNotification();
wss.close();
})
})