Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Deploy : Wakttu #7 #94

Merged
merged 2 commits into from
Nov 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions src/auth/auth.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,6 @@ export class AuthService {
}

async logout(@Req() request: Request): Promise<any> {
const { id, provider } = request.session.user;
if (provider === 'guest') this.userService.deleteGuest(id);
request.session.destroy(() => {});
return { success: true, message: 'Logout Success' };
}
Expand Down
54 changes: 37 additions & 17 deletions src/socket/socket.gateway.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@ interface Chat {
success?: boolean;
}

interface Emoticon {
emoticonId: string;
userId: string;
roomId: string;
}

export class Game {
constructor() {
this.host = ''; // 호슀트
Expand Down Expand Up @@ -138,48 +144,55 @@ export class SocketGateway
[roomId: string]: NodeJS.Timeout;
} = {};

// μ ‘μ†μ‹œ μˆ˜ν–‰λ˜λŠ” μ½”λ“œ
async handleConnection(@ConnectedSocket() client: any) {
try {
const isAuthenticated = await this.guard.validateClient(client);

if (!isAuthenticated) {
client.disconnect(); // 인증 μ‹€νŒ¨ μ‹œ μ—°κ²° ν•΄μ œ
client.disconnect();
return;
}
// μ΅œλŒ€ μ—°κ²° 수 체크
if (this.currentConnections >= this.MAX_CONNECTIONS) {

this.currentConnections++; // μ—°κ²° 수 증가

// μ΅œλŒ€ μ—°κ²° 수 확인
if (this.currentConnections > this.MAX_CONNECTIONS) {
this.logger.warn(
`Connection rejected - Max connections reached: ${client.id}`,
);
client.emit('full', {
message: 'μ„œλ²„κ°€ 가득 μ°ΌμŠ΅λ‹ˆλ‹€. μž μ‹œ ν›„ λ‹€μ‹œ μ‹œλ„ν•΄μ£Όμ„Έμš”.',
});
client.disconnect();
setTimeout(() => client.disconnect(), 200);
return;
}
const user = client.request.session.user;
this.logger.log(`Client connected: ${client.id}`);

const user = client.request.session?.user;
if (!user) {
this.logger.warn(`Connection rejected - No user session: ${client.id}`);
client.disconnect();
return;
}
for (const key in this.user) {
if (this.user[key].id === user.id) {
this.server
.to(key)
.emit('alarm', { message: '이미 접속쀑인 μœ μ €μž…λ‹ˆλ‹€!' });
this.handleDisconnect({ id: key });
}

// 쀑볡 접속 확인 및 처리
const existingClientId = Object.keys(this.user).find(
(key) => this.user[key].id === user.id,
);
if (existingClientId) {
this.server
.to(existingClientId)
.emit('alarm', { message: '이미 접속쀑인 μœ μ €μž…λ‹ˆλ‹€!' });
this.handleDisconnect({ id: existingClientId });
}
this.currentConnections++; // μ—°κ²° 수 증가

this.user[client.id] = await this.socketService.reloadUser(user.id);
this.user[client.id].color = this.socketService.getColor();
client.emit('list', this.user);

client.emit('connected'); // ν•„μš”ν•œ μ •λ³΄λ§Œ 전달
this.logger.log(`Client connected: ${client.id}`);
} catch (error) {
this.logger.error(`Connection error: ${error.message}`, error.stack);
client.emit('error', { message: 'μ„œλ²„ 였λ₯˜κ°€ λ°œμƒν–ˆμŠ΅λ‹ˆλ‹€.' });
}
}

Expand Down Expand Up @@ -237,7 +250,7 @@ export class SocketGateway
}
}

if (this.user[client.id].provider === 'guest') {
if (this.user[client.id] && this.user[client.id].provider === 'guest') {
await this.socketService.deleteGuest(this.user[client.id].id);
client.request.session.destroy(() => {});
}
Expand Down Expand Up @@ -396,6 +409,13 @@ export class SocketGateway
}
}

// Emoticon
@SubscribeMessage('emoticon')
handleEmoticon(@MessageBody() data: Emoticon) {
console.log('emoticon');
this.server.to(data.roomId).emit('emoticon', data);
}

// κ²Œμž„ λ°© 생성
@SubscribeMessage('createRoom')
async handleCreate(
Expand Down