40 lines
999 B
TypeScript
40 lines
999 B
TypeScript
import type { FastifyInstance } from 'fastify'
|
|
import type { ServerOptions } from 'socket.io'
|
|
import fp from 'fastify-plugin'
|
|
import { Server } from 'socket.io'
|
|
import registerWebrtcSocket from '../socket/webrtc.ts'
|
|
|
|
declare module 'fastify' {
|
|
interface FastifyInstance {
|
|
io: Server
|
|
}
|
|
}
|
|
|
|
export default fp<Partial<ServerOptions>>(
|
|
async (fastify, opts) => {
|
|
fastify.decorate('io', new Server(fastify.server, opts))
|
|
|
|
fastify.addHook('preClose', () => {
|
|
fastify.io.disconnectSockets(true)
|
|
})
|
|
|
|
fastify.addHook('onClose', async (fastify: FastifyInstance) => {
|
|
await fastify.io.close()
|
|
})
|
|
|
|
fastify.ready(() => {
|
|
registerWebrtcSocket(fastify.io, fastify.mediasoupRouter)
|
|
})
|
|
},
|
|
{ name: 'socket-io', dependencies: ['mediasoup-worker', 'mediasoup-router'] },
|
|
)
|
|
|
|
export const autoConfig: Partial<ServerOptions> = {
|
|
path: '/chad/ws',
|
|
cors: {
|
|
origin: process.env.CORS_ORIGIN || '*',
|
|
methods: ['GET', 'POST'],
|
|
credentials: true,
|
|
},
|
|
}
|