-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- socket register api 구현 - room join 이벤트 api 구현 - room leave 이벤트 api 구현
- Loading branch information
Showing
5 changed files
with
82 additions
and
6 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
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'; | ||
} | ||
} |
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,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; | ||
} | ||
} |
3 changes: 3 additions & 0 deletions
3
backEnd/center/src/connections/dto/register-signaling-socket.dto.ts
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,3 @@ | ||
export class RegisterSignalingSocket { | ||
url: string; | ||
} |
3 changes: 3 additions & 0 deletions
3
backEnd/center/src/connections/dto/return-connections.dto copy.ts
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,3 @@ | ||
export class ReturnConnectionsDto { | ||
url: string; | ||
} |
3 changes: 3 additions & 0 deletions
3
backEnd/center/src/connections/dto/signaling-connections.dto.ts
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,3 @@ | ||
export class SignalingConnectionDto { | ||
roomName: string; | ||
} |