46 lines
1.1 KiB
TypeScript
46 lines
1.1 KiB
TypeScript
import {
|
|
WebSocketGateway,
|
|
WebSocketServer,
|
|
SubscribeMessage,
|
|
OnGatewayConnection,
|
|
OnGatewayDisconnect,
|
|
} from '@nestjs/websockets';
|
|
import { Server, Socket } from 'socket.io';
|
|
|
|
@WebSocketGateway({
|
|
cors: {
|
|
origin: '*',
|
|
},
|
|
})
|
|
export class EventsGateway implements OnGatewayConnection, OnGatewayDisconnect {
|
|
@WebSocketServer()
|
|
server: Server;
|
|
|
|
handleConnection(client: Socket) {
|
|
console.log(`Client connected: ${client.id}`);
|
|
}
|
|
|
|
handleDisconnect(client: Socket) {
|
|
console.log(`Client disconnected: ${client.id}`);
|
|
}
|
|
|
|
@SubscribeMessage('join_floor')
|
|
handleJoinFloor(client: Socket, floorId: string) {
|
|
client.join(floorId);
|
|
console.log(`Client ${client.id} joined floor: ${floorId}`);
|
|
}
|
|
|
|
@SubscribeMessage('leave_floor')
|
|
handleLeaveFloor(client: Socket, floorId: string) {
|
|
client.leave(floorId);
|
|
console.log(`Client ${client.id} left floor: ${floorId}`);
|
|
}
|
|
|
|
emitSpaceStatusChanged(floorId: string, spaceId: string, isOccupied: boolean) {
|
|
this.server.to(floorId).emit('space_status_changed', {
|
|
spaceId,
|
|
isOccupied,
|
|
});
|
|
}
|
|
}
|