This commit is contained in:
parent
5ca3a47d40
commit
6b6a66eef4
@ -2,6 +2,13 @@ import type { types } from 'mediasoup'
|
||||
import type { Namespace, RemoteSocket, Socket, Server as SocketServer } from 'socket.io'
|
||||
import { consola } from 'consola'
|
||||
|
||||
interface ChadClient {
|
||||
id: string
|
||||
username: string
|
||||
inputMuted: boolean
|
||||
outputMuted: boolean
|
||||
}
|
||||
|
||||
interface ProducerShort {
|
||||
producerId: types.Producer['id']
|
||||
kind: types.MediaKind
|
||||
@ -80,10 +87,14 @@ interface ClientToServerEvents {
|
||||
},
|
||||
cb: EventCallback
|
||||
) => void
|
||||
updateClient: (
|
||||
options: Partial<Omit<ChadClient, 'id'>>,
|
||||
cb: EventCallback
|
||||
) => void
|
||||
}
|
||||
|
||||
interface ServerToClientEvents {
|
||||
newPeer: (arg: { id: string, username: string }) => void
|
||||
newPeer: (arg: ChadClient) => void
|
||||
producers: (arg: ProducerShort[]) => void
|
||||
newConsumer: (
|
||||
arg: {
|
||||
@ -103,6 +114,7 @@ interface ServerToClientEvents {
|
||||
consumerPaused: (arg: { consumerId: string }) => void
|
||||
consumerResumed: (arg: { consumerId: string }) => void
|
||||
consumerScore: (arg: { consumerId: string, score: types.ConsumerScore }) => void
|
||||
clientChanged: (clientId: ChadClient['id'], client: ChadClient) => void
|
||||
}
|
||||
|
||||
interface InterServerEvent {}
|
||||
@ -110,6 +122,8 @@ interface InterServerEvent {}
|
||||
interface SocketData {
|
||||
joined: boolean
|
||||
username: string
|
||||
inputMuted: boolean
|
||||
outputMuted: boolean
|
||||
rtpCapabilities: types.RtpCapabilities
|
||||
transports: Map<types.WebRtcTransport['id'], types.WebRtcTransport>
|
||||
producers: Map<types.Producer['id'], types.Producer>
|
||||
@ -123,7 +137,10 @@ export default function (io: SocketServer, router: types.Router) {
|
||||
consola.info('[WebRtc]', 'Client connected', socket.id)
|
||||
|
||||
socket.data.joined = false
|
||||
|
||||
socket.data.username = socket.id
|
||||
socket.data.inputMuted = false
|
||||
socket.data.outputMuted = false
|
||||
|
||||
socket.data.transports = new Map()
|
||||
socket.data.producers = new Map()
|
||||
@ -158,10 +175,7 @@ export default function (io: SocketServer, router: types.Router) {
|
||||
}
|
||||
}
|
||||
|
||||
socket.broadcast.emit('newPeer', {
|
||||
id: socket.id,
|
||||
username,
|
||||
})
|
||||
socket.broadcast.emit('newPeer', socketToClient(socket))
|
||||
})
|
||||
|
||||
socket.on('getRtpCapabilities', (cb) => {
|
||||
@ -403,6 +417,22 @@ export default function (io: SocketServer, router: types.Router) {
|
||||
cb({ ok: true })
|
||||
})
|
||||
|
||||
socket.on('updateClient', (updatedClient) => {
|
||||
if (updatedClient.username) {
|
||||
socket.data.username = updatedClient.username
|
||||
}
|
||||
|
||||
if (updatedClient.inputMuted) {
|
||||
socket.data.inputMuted = updatedClient.inputMuted
|
||||
}
|
||||
|
||||
if (updatedClient.outputMuted) {
|
||||
socket.data.outputMuted = updatedClient.outputMuted
|
||||
}
|
||||
|
||||
socket.broadcast.emit('clientChanged', socket.id, socketToClient(socket))
|
||||
})
|
||||
|
||||
socket.on('disconnect', () => {
|
||||
consola.info('Client disconnected:', socket.id)
|
||||
|
||||
@ -521,4 +551,13 @@ export default function (io: SocketServer, router: types.Router) {
|
||||
consola.error('_createConsumer() | failed:%o', error)
|
||||
}
|
||||
}
|
||||
|
||||
function socketToClient(socket: Socket<ClientToServerEvents, ServerToClientEvents, InterServerEvent, SocketData>): ChadClient {
|
||||
return {
|
||||
id: socket.id,
|
||||
username: socket.data.username,
|
||||
inputMuted: socket.data.inputMuted,
|
||||
outputMuted: socket.data.outputMuted,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user