29 lines
809 B
TypeScript
29 lines
809 B
TypeScript
import { WebSocketGateway, WebSocketServer, OnGatewayInit, OnGatewayConnection, OnGatewayDisconnect } from '@nestjs/websockets';
|
|
import { Server, Socket } from 'socket.io';
|
|
|
|
@WebSocketGateway(3001, {
|
|
namespace: 'library-events',
|
|
cors: {
|
|
origin: '*', // 或者你可以指定特定的来源,例如 "http://localhost:3000"
|
|
methods: ['GET', 'POST'],
|
|
credentials: true
|
|
}
|
|
})
|
|
export class SocketGateway implements OnGatewayInit, OnGatewayConnection, OnGatewayDisconnect {
|
|
|
|
@WebSocketServer() server: Server;
|
|
|
|
afterInit(server: Server) {
|
|
console.log('WebSocket initialized');
|
|
}
|
|
|
|
handleConnection(client: Socket, ...args: any[]) {
|
|
console.log(`Client connected: ${client.id}`);
|
|
}
|
|
|
|
handleDisconnect(client: Socket) {
|
|
console.log(`Client disconnected: ${client.id}`);
|
|
}
|
|
|
|
}
|