Skip to content

Commit

Permalink
feat: [BE] 인메모리 방식으로 중앙 서버 로직 구현
Browse files Browse the repository at this point in the history
- socket register api 구현
- room join 이벤트 api 구현
- room leave 이벤트 api 구현
  • Loading branch information
Gseungmin committed Nov 15, 2023
1 parent b0c85b5 commit 7a8a548
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 6 deletions.
23 changes: 19 additions & 4 deletions backEnd/center/src/connections/connections.controller.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,28 @@
import { Controller, Post, Body } from '@nestjs/common';
import { ConnectionsService } from './connections.service';
import { SignalingConnectionDto } from './dto/signaling-connections.dto';
import { RegisterSignalingSocket } from './dto/register-signaling-socket.dto';
import { ReturnConnectionsDto } from './dto/return-connections.dto copy';

@Controller('connections')
export class ConnectionsController {
constructor(private readonly connectionsService: ConnectionsService) {}

@Post()
create() {
const url = this.connectionsService.create();
return { url: url };
@Post('signaling')
register(@Body() data: RegisterSignalingSocket) {
return this.connectionsService.registerSockets(data);
}

@Post('signaling/join')
create(@Body() data: SignalingConnectionDto) {
const response: ReturnConnectionsDto =
this.connectionsService.createConnection(data);
return response;
}

@Post('signaling/leave')
leave(@Body() data: SignalingConnectionDto) {
this.connectionsService.leaveRoom(data);
return 'leave Success';
}
}
56 changes: 54 additions & 2 deletions backEnd/center/src/connections/connections.service.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,60 @@
import { Injectable } from '@nestjs/common';
import { ReturnConnectionsDto } from './dto/return-connections.dto copy';
import { SignalingConnectionDto } from './dto/signaling-connections.dto';
import { RegisterSignalingSocket } from './dto/register-signaling-socket.dto';

@Injectable()
export class ConnectionsService {
create() {
return 'url';
private roomToSocket = new Map<string, string>();
private socketToConnections = new Map<string, number>();

registerSockets(data: RegisterSignalingSocket) {
const url = data.url;
console.log(url);
const isSocket = this.socketToConnections.has(url);

if (isSocket) {
return 'Already Registered';
}

this.socketToConnections.set(url, 0);
return 'Register Success';
}

createConnection(data: SignalingConnectionDto): ReturnConnectionsDto {
const { roomName } = data;

const isRoom = this.roomToSocket.has(roomName);

if (isRoom) {
const socket = this.roomToSocket.get(roomName);
const connections = this.socketToConnections.get(socket);
this.socketToConnections.set(socket, connections + 1);
const result: ReturnConnectionsDto = { url: socket };
return result;
}

let socket = null;
let connections = Number.MAX_SAFE_INTEGER;
this.socketToConnections.forEach((value, key) => {
if (value < connections) {
connections = value;
socket = key;
}
});

this.roomToSocket.set(roomName, socket);
this.socketToConnections.set(socket, connections + 1);
const result: ReturnConnectionsDto = { url: socket };

return result;
}

leaveRoom(data: SignalingConnectionDto): void {
const { roomName } = data;
const socket = this.roomToSocket.get(roomName);
const connections = this.socketToConnections.get(socket);
this.socketToConnections.set(socket, connections - 1);
return;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export class RegisterSignalingSocket {
url: string;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export class ReturnConnectionsDto {
url: string;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export class SignalingConnectionDto {
roomName: string;
}

0 comments on commit 7a8a548

Please sign in to comment.