Skip to content

Commit

Permalink
fix: save multiple consumers in a peer
Browse files Browse the repository at this point in the history
  • Loading branch information
woody146 committed Jun 19, 2023
1 parent b753f28 commit 97e2031
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 12 deletions.
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -235,8 +235,12 @@ npm run dev
</tr>
<tr>
<td>POST</td>
<td>/consumer_peers/:peerId/resume<br /><i>make peer resume after connected</i></td>
<td></td>
<td>/consumer_peers/:peerId/resume<br /><i>make consumer resume after connected</i></td>
<td>
<ul>
<li>consumerId: string</li>
</ul>
</td>
<td></td>
</tr>
<tr>
Expand Down
1 change: 1 addition & 0 deletions examples/rooms/src/components/Consumer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ export function Consumer({
fetchApi({
path: `/api/consumer_peers/${transport.id}/resume`,
method: 'POST',
data: { consumerId: id },
});
}
};
Expand Down
4 changes: 2 additions & 2 deletions src/entities/media.peer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ export class MediaPeer extends BaseEntity {
@Column('uuid', { nullable: true })
producerId?: string;

@Column('uuid', { nullable: true })
consumerId?: string;
@Column({ type: 'jsonb', default: {} })
consumers!: any;

@Column('text')
type!: string; // consumer | producer
Expand Down
16 changes: 8 additions & 8 deletions src/services/peer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ export class PeerService extends BaseService {
id: string;
}> {
const peer = await this.get({ peerId: data.peerId });
if (peer.type === constants.CONSUMER) {
if (peer.type === constants.CONSUMER && !peer.consumers[data.producerId]) {
await this.createService(RouterService).checkToPipe({
routerId: peer.routerId,
producerId: data.producerId,
Expand All @@ -172,8 +172,7 @@ export class PeerService extends BaseService {
},
});

peer.consumerId = result.id;
peer.producerId = data.producerId;
peer.consumers[data.producerId] = result.id;
await this.dataSource.getRepository(MediaPeer).save(peer);
return result;
}
Expand Down Expand Up @@ -216,17 +215,18 @@ export class PeerService extends BaseService {
throw new ServiceError(400, 'Invalid type peer');
}

async resume(data: { peerId: string }) {
async resume(data: { peerId: string; consumerId: string }) {
const peer = await this.get({ peerId: data.peerId });
if (peer.consumerId && peer.type === constants.CONSUMER) {
if (
peer.type === constants.CONSUMER &&
Object.values(peer.consumers).includes(data.consumerId)
) {
await fetchApi({
host: peer.worker.internalHost,
port: peer.worker.apiPort,
path: '/consumers/:consumerId/resume',
method: 'POST',
data: {
consumerId: peer.consumerId,
},
data: { consumerId: data.consumerId },
});
return {};
}
Expand Down

0 comments on commit 97e2031

Please sign in to comment.