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