nestjs / nest

A progressive Node.js framework for building efficient, scalable, and enterprise-grade server-side applications with TypeScript/JavaScript 🚀
https://nestjs.com
MIT License
67.73k stars 7.63k forks source link

websocket error: can't bind Socket to client #6998

Closed j92z closed 3 years ago

j92z commented 3 years ago

Bug Report

Current behavior

when connect server, handleConnection() client.id is undefined.

[Nest] 21204   - 2021-04-28 10:27:40 ├F10: PM┤   [ClassroomChatGateway] Client connected: undefined

when emit event, handleMessage() 'joinRoom' client.to is not a function

[Nest] 21204   - 2021-04-28 10:32:49 ├F10: PM┤   [WsExceptionsHandler] client.to is not a function +309241ms
TypeError: client.to is not a function
    at ClassroomChatGateway.handleMessage (C:\code_file\dyoung.server\dist\classroom\classroom-chat.gateway.js:23:16)
    at C:\code_file\dyoung.server\node_modules\@nestjs\websockets\context\ws-context-creator.js:43:33
    at processTicksAndRejections (internal/process/task_queues.js:97:5)
    at async ClassroomChatGateway.<anonymous> (C:\code_file\dyoung.server\node_modules\@nestjs\websockets\context\ws-proxy.js:12:32)
    at async WebSocketsController.pickResult (C:\code_file\dyoung.server\node_modules\@nestjs\websockets\web-sockets-controller.js:86:24)

Input Code

// gateway.ts
import {
    WebSocketGateway,
    OnGatewayInit,
    WebSocketServer,
    OnGatewayConnection,
    OnGatewayDisconnect,
    ConnectedSocket,
    MessageBody,
    SubscribeMessage,
} from '@nestjs/websockets';
import { Logger } from '@nestjs/common';
import { Socket, Server } from 'socket.io';

@WebSocketGateway()
export class ClassroomChatGateway implements OnGatewayInit, OnGatewayConnection, OnGatewayDisconnect {

    @WebSocketServer() server: Server;
    private logger: Logger = new Logger('ClassroomChatGateway');

    @SubscribeMessage('joinRoom')
    handleMessage( @MessageBody() data: any,
    @ConnectedSocket() client: Socket,): void {
        client.to("111")
        this.server.emit('joinRoom', data);
    }

    afterInit(server: Server) {
        this.logger.log('Init');
    }

    handleDisconnect(client: Socket) {
        this.logger.log(`Client disconnected: ${client.id}`);
    }

    handleConnection(client: Socket, ...args: any[]) {
        this.logger.log(`Client connected: ${client.id}`);
    }
}
// main.ts
import { NestFactory } from '@nestjs/core';
import { WsAdapter } from '@nestjs/platform-ws';
import { AppModule } from './app.module';
async function bootstrap() {
    const app = await NestFactory.create(AppModule);
    app.useWebSocketAdapter(new WsAdapter(app));
    await app.listen(process.env.SERVER_PORT || 3000);
}
bootstrap();

Expected behavior

I wonder what caused socket object empty

Possible Solution

Environment


Nest version: 7.6.15


For Tooling issues:
- Node version: 12.18.1  
- Platform: Windows 10 

Others:

jmcdo29 commented 3 years ago

More than likely, this is caused by incompatible versions of socket.io. can you please provide a minimum reproduction?

j92z commented 3 years ago

@jmcdo29 i get it. use incompatible versions of socket.io.

chaddaess commented 6 months ago

I have the exact same error and i don't understand what you mean by "incompatible versions of socket.io." can you please elaborate?

Highnesslin commented 2 months ago

How to solve it???