Skip to content

Commit

Permalink
Support for closing peers and closing transports
Browse files Browse the repository at this point in the history
  • Loading branch information
keianhzo committed Jan 21, 2021
1 parent 758058a commit ac31665
Show file tree
Hide file tree
Showing 3 changed files with 119 additions and 14 deletions.
86 changes: 86 additions & 0 deletions lib/Room.js
Original file line number Diff line number Diff line change
Expand Up @@ -816,6 +816,74 @@ class Room extends EventEmitter
break;
}

case 'getPeers':
{
// Ensure the Peer is not already joined.
if (!peer.data.joined)
throw new Error('Peer not joined');

// Tell the new Peer about already joined Peers.
// And also create Consumers for existing Producers.

const joinedPeers =
[
...this._getJoinedPeers(),
...this._broadcasters.values()
];

// Reply now the request with the list of joined peers (all but the new one).
const peerInfos = joinedPeers
.filter((joinedPeer) => joinedPeer.id !== peer.id)
.map((joinedPeer) => ({
id : joinedPeer.id,
displayName : joinedPeer.data.displayName,
device : joinedPeer.data.device,
hasProducers : joinedPeer.data.producers.size > 0
}));

accept({ peers: peerInfos });

for (const joinedPeer of joinedPeers)
{
if (joinedPeer.id !== peer.id) {
// Create Consumers for existing Producers.
for (const producer of joinedPeer.data.producers.values())
{
this._createConsumer(
{
consumerPeer : peer,
producerPeer : joinedPeer,
producer
});
}

// Create DataConsumers for existing DataProducers.
for (const dataProducer of joinedPeer.data.dataProducers.values())
{
if (dataProducer.label === 'bot')
continue;

this._createDataConsumer(
{
dataConsumerPeer : peer,
dataProducerPeer : joinedPeer,
dataProducer
});
}
}
}

// Create DataConsumers for bot DataProducer.
this._createDataConsumer(
{
dataConsumerPeer : peer,
dataProducerPeer : null,
dataProducer : this._bot.dataProducer
});

break;
}

case 'createWebRtcTransport':
{
// NOTE: Don't require that the Peer is joined here, so the client can
Expand Down Expand Up @@ -882,6 +950,10 @@ class Room extends EventEmitter
// Store the WebRtcTransport into the protoo Peer data Object.
peer.data.transports.set(transport.id, transport);

transport.observer.on('close', () => {
peer.data.transports.delete(transport.id);
});

accept(
{
id : transport.id,
Expand All @@ -903,6 +975,20 @@ class Room extends EventEmitter
break;
}

case 'closeWebRtcTransport': {
const { transportId } = request.data;
const transport = peer.data.transports.get(transportId);

if (!transport)
throw new Error(`transport with id "${transportId}" not found`);

transport.close();

accept();

break;
}

case 'connectWebRtcTransport':
{
const { transportId, dtlsParameters } = request.data;
Expand Down
45 changes: 32 additions & 13 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"express": "^4.17.1",
"heapdump": "^0.3.15",
"jsonwebtoken": "^8.5.1",
"mediasoup": "github:versatica/mediasoup#v3",
"mediasoup": "^3.6.31",
"pidusage": "^2.0.18",
"protoo-server": "^4.0.4",
"sctp": "^0.0.19"
Expand Down

0 comments on commit ac31665

Please sign in to comment.