Skip to content

Commit

Permalink
feat: add room router
Browse files Browse the repository at this point in the history
  • Loading branch information
woody146 committed Jun 8, 2023
1 parent 2d20281 commit be253cd
Show file tree
Hide file tree
Showing 8 changed files with 60 additions and 15 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"test": "echo \"Error: no test specified\" && exit 1",
"start:master": "PORT=3000 node dist/run_master.js",
"start:slave": "PORT=3002 node dist/run_slave.js",
"dev": "node dist/run_master.js",
"dev": "npm run start:slave",
"prepare": "husky install"
},
"repository": {
Expand Down
5 changes: 5 additions & 0 deletions src/apis/master/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { room } from './room.js';

const apiRouters = [...room];

export default apiRouters;
12 changes: 12 additions & 0 deletions src/apis/master/room.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { MediasoupRoomService } from '../../services/index.js';
import { RouteConfig, getDataSource } from '../../utils/index.js';

export const room: Array<RouteConfig> = [
{
method: 'POST',
url: '/rooms',
handler: () => {
return new MediasoupRoomService(getDataSource()).create({});
},
},
];
9 changes: 2 additions & 7 deletions src/run_master.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
import 'node-fetch';

import apiRouters from './apis/master/index.js';
import { startServer } from './utils/index.js';

startServer([
{
method: 'GET',
url: '/',
handler: () => ({ data: 'data' }),
},
]);
startServer(apiRouters);
9 changes: 9 additions & 0 deletions src/services/mediasoup.room.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { MediasoupRoom } from '../entities/index.js';
import { fetchApi } from '../utils/index.js';
import { BaseService } from './base.js';
import { MediasoupSlaveService } from './mediasoup.slave.js';

Expand All @@ -10,7 +11,15 @@ export class MediasoupRoomService extends BaseService {
MediasoupSlaveService
).getForNewRoom();
if (slave) {
const result = await fetchApi({
host: slave.externalHost,
port: slave.apiPort,
path: '/routers',
method: 'POST',
});
const mediasoupRoom = new MediasoupRoom();
mediasoupRoom.routerId = result.id;
mediasoupRoom.slaveId = slave.id;
Object.assign(mediasoupRoom, data);
await this.dataSource.getRepository(MediasoupRoom).save(mediasoupRoom);
return mediasoupRoom.id;
Expand Down
15 changes: 8 additions & 7 deletions src/services/mediasoup.slave.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { Raw } from 'typeorm';
import { constants } from '../constants.js';
import { MediasoupSlave } from '../entities/index.js';
import { BaseService } from './base.js';
Expand Down Expand Up @@ -31,12 +30,14 @@ export class MediasoupSlaveService extends BaseService {
}

async getForNewRoom() {
const result = await this.dataSource.getRepository(MediasoupSlave).findOne({
where: {
for: constants.PRODUCER,
peerCount: Raw((alias) => `${alias} < maxPeer`),
},
});
const result = await this.dataSource
.createQueryBuilder()
.select('slave')
.from(MediasoupSlave, 'slave')
.where('slave.for = :for', { for: constants.PRODUCER })
.andWhere('slave.peerCount < slave.maxPeer')
.getOne();

return result;
}
}
22 changes: 22 additions & 0 deletions src/utils/api.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
export function fetchApi({
host,
port,
path,
method,
}: {
host: string;
port?: string | number;
path: string;
method: 'POST' | 'GET' | 'PUT' | 'DELETE';
}) {
return fetch('http://' + host + (port ? ':' + port : '') + path, {
method,
}).then(async (resp) => {
if (resp.status > 400) {
const message = await resp.text();
throw { code: resp.status, message };
} else {
return resp.json();
}
});
}
1 change: 1 addition & 0 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export * from './bootstrap.js';
export * from './datasource.js';
export * from './api.js';

0 comments on commit be253cd

Please sign in to comment.