Skip to content

Commit 2d2407c

Browse files
authored
Merge pull request #15 from Astitva877/astitva/testing
feat: added dummy socket for testing
2 parents 2413873 + 5d53dbc commit 2d2407c

File tree

3 files changed

+74
-10
lines changed

3 files changed

+74
-10
lines changed

src/main.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ import { IoAdapter } from '@nestjs/platform-socket.io';
66

77
async function bootstrap() {
88
const app = await NestFactory.create(AppModule);
9-
// Increase payload size limit
10-
app.use(bodyParser.json({ limit: '50mb' }));
11-
app.use(bodyParser.urlencoded({ limit: '50mb', extended: true }));
9+
// Increase payload size limit
10+
app.use(bodyParser.json({ limit: '50mb' }));
11+
app.use(bodyParser.urlencoded({ limit: '50mb', extended: true }));
1212

1313
/**
1414
* The url endpoint for open api ui
@@ -31,8 +31,8 @@ async function bootstrap() {
3131
*/
3232
const SWAGGER_API_CURRENT_VERSION = '1.0';
3333

34-
// Use the custom WebSocket adapter to handle both WS and SocketIo
35-
app.useWebSocketAdapter(new IoAdapter(app));
34+
// Use the custom WebSocket adapter to handle both WS and SocketIo
35+
// app.useWebSocketAdapter(new IoAdapter(app));
3636

3737
// Configure Swagger options for API documentation
3838
const options = new DocumentBuilder()
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
import {
2+
WebSocketGateway,
3+
WebSocketServer,
4+
OnGatewayConnection,
5+
OnGatewayDisconnect,
6+
SubscribeMessage,
7+
MessageBody,
8+
OnGatewayInit,
9+
ConnectedSocket,
10+
} from '@nestjs/websockets';
11+
import { Server, Socket } from 'socket.io';
12+
@WebSocketGateway({
13+
namespace: '/dummy',
14+
cors: {
15+
origin: '*',
16+
},
17+
transports: ['websocket'],
18+
methods: ['GET', 'POST'],
19+
})
20+
export class DummyGateway
21+
implements OnGatewayInit, OnGatewayConnection, OnGatewayDisconnect
22+
{
23+
@WebSocketServer()
24+
server: Server;
25+
26+
constructor() {}
27+
async afterInit() {
28+
console.log('AI Websocket Gateway initialized!');
29+
}
30+
31+
async handleConnection(client: Socket) {
32+
setTimeout(() => {
33+
client.emit('Client', 'Client is connected, first event initiated.');
34+
}, 5000);
35+
}
36+
37+
handleDisconnect(client: Socket) {
38+
console.log(`Client disconnected: ${client.id}`);
39+
}
40+
41+
@SubscribeMessage('')
42+
async handleMessage2(
43+
@ConnectedSocket() client: Socket,
44+
@MessageBody() payload: string,
45+
) {
46+
client.emit('third', payload);
47+
}
48+
49+
@SubscribeMessage('second')
50+
async handleMessage3(
51+
@ConnectedSocket() client: Socket,
52+
@MessageBody() payload: string,
53+
) {
54+
client.emit('second', payload);
55+
client.emit('latest', payload);
56+
}
57+
58+
@SubscribeMessage('first')
59+
async handleMessage(
60+
@ConnectedSocket() client: Socket,
61+
@MessageBody() payload: string,
62+
) {
63+
client.emit('first', payload);
64+
client.emit('new', payload);
65+
}
66+
}
Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
import { Module } from '@nestjs/common';
2-
import { SocketIoGateway } from './socketio.gateway';
3-
import { SocketIoService } from './socketio.service';
4-
import { WebSocketService } from './websocket.service';
2+
import { DummyGateway } from './dummy.gateway';
53

64
@Module({
7-
providers: [SocketIoGateway, SocketIoService, WebSocketService],
8-
exports: [SocketIoService, WebSocketService],
5+
providers: [DummyGateway],
6+
exports: [],
97
})
108
export class SocketIoModule {}

0 commit comments

Comments
 (0)