-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
73 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
export * from './slave.js'; | ||
export * from './room.js'; | ||
export * from './peer.js'; | ||
export * from './router.js'; | ||
export * from './mediasoup/index.js'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import { constants } from '../constants.js'; | ||
import { MediaRouter } from '../entities/index.js'; | ||
import { fetchApi } from '../utils/api.js'; | ||
import { BaseService, ServiceError } from './base.js'; | ||
import { SlaveService } from './slave.js'; | ||
|
||
export class RouterService extends BaseService { | ||
async create(data: { roomId: string }) { | ||
const slave = await this.createService(SlaveService).getFor( | ||
constants.CONSUMER | ||
); | ||
if (slave) { | ||
const result = await fetchApi({ | ||
host: slave.internalHost, | ||
port: slave.apiPort, | ||
path: '/routers', | ||
method: 'POST', | ||
}); | ||
const mediaRouter = new MediaRouter(); | ||
mediaRouter.id = result.id; | ||
mediaRouter.slaveId = slave.id; | ||
Object.assign(mediaRouter, data); | ||
await this.dataSource.getRepository(MediaRouter).save(mediaRouter); | ||
return { result }; | ||
} | ||
throw new ServiceError(404, 'Slave not found'); | ||
} | ||
|
||
async get(data: { roomId: string }) { | ||
const router = await this.dataSource | ||
.createQueryBuilder(MediaRouter, 'router') | ||
.leftJoinAndSelect('router.slave', 'slave') | ||
.where('router.roomId = :roomId', { roomId: data.roomId }) | ||
.andWhere('slave.peerCount < slave.maxPeer') | ||
.getOne(); | ||
if (router) { | ||
const result = await fetchApi({ | ||
host: router.slave.internalHost, | ||
port: router.slave.apiPort, | ||
path: '/routers/:routerId', | ||
method: 'GET', | ||
data: { routerId: router.id }, | ||
}); | ||
return { ...result, id: data.roomId }; | ||
} | ||
return null; | ||
} | ||
|
||
async getOrCreate(data: { roomId: string }) { | ||
const result = await this.get(data); | ||
if (result) { | ||
return result; | ||
} | ||
return this.create(data); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters