This commit is contained in:
2026-01-21 22:39:08 +06:00
parent 595354b7f0
commit 65583b1564
50 changed files with 19946 additions and 99 deletions

View File

@@ -1,17 +1,14 @@
import type { types } from 'mediasoup'
import type { Server as SocketServer } from 'socket.io'
import type {
Namespace,
SomeSocket,
} from '../types/webrtc.ts'
} from '../types/socket.ts'
import { consola } from 'consola'
import prisma from '../prisma/client.ts'
import { socketToClient } from '../utils/socket-to-client.ts'
export default function (io: SocketServer, router: types.Router) {
const namespace: Namespace = io.of('/webrtc')
namespace.on('connection', async (socket) => {
io.on('connection', async (socket) => {
consola.info('[WebRtc]', 'Client connected', socket.id)
socket.data.joined = false
@@ -23,7 +20,7 @@ export default function (io: SocketServer, router: types.Router) {
socket.data.producers = new Map()
socket.data.consumers = new Map()
const { id, username, displayName } = await prisma.user.findUnique({
const user = await prisma.user.findUnique({
where: {
id: socket.handshake.auth.userId,
},
@@ -34,11 +31,19 @@ export default function (io: SocketServer, router: types.Router) {
},
})
if (!user) {
socket.disconnect()
return
}
const { id, username, displayName } = user
socket.data.userId = id
socket.data.username = username
socket.data.displayName = displayName
socket.emit('authenticated')
socket.emit('authenticated', { channels })
socket.on('join', async ({ rtpCapabilities }, cb) => {
if (socket.data.joined) {
@@ -319,7 +324,7 @@ export default function (io: SocketServer, router: types.Router) {
cb(socketToClient(socket))
namespace.emit('clientChanged', socket.id, socketToClient(socket))
io.emit('clientChanged', socket.id, socketToClient(socket))
})
socket.on('disconnect', () => {
@@ -336,7 +341,7 @@ export default function (io: SocketServer, router: types.Router) {
})
async function getJoinedSockets(excludeId?: string) {
const sockets = await namespace.fetchSockets()
const sockets = await io.fetchSockets()
return sockets.filter(socket => socket.data.joined && (excludeId ? excludeId !== socket.id : true))
}