【问题标题】:Socket io and nestjs, messages in the same rooms are not seen by both people, only the sending user can see the message, but only his ownsocket io和nestjs,同一个房间的消息两个人都看不到,只有发送的用户可以看到消息,但只有他自己的
【发布时间】:2022-11-05 11:48:32
【问题描述】:

我正在做一个 websocket 服务,但是在房间里发送消息时,只有发送它的用户才能看到消息,而且是他自己的。如果你能帮助我,我将不胜感激。

这是代码:

import {
  WebSocketGateway,
  SubscribeMessage,
  MessageBody,
  ConnectedSocket,
} from "@nestjs/websockets";
import { Socket } from "socket.io";
import { ChatService } from "./chat.service";
import { CreateChatDto } from "./dto/create-chat.dto";
import { UpdateChatDto } from "./dto/update-chat.dto";

@WebSocketGateway()
export class ChatGateway {
  constructor(private readonly chatService: ChatService) {}

  @SubscribeMessage("createRoom")
  create(
    @MessageBody() createChatDto: CreateChatDto,
    @ConnectedSocket() client: Socket
  ) {
    client.join(createChatDto.firebaseUuid);
    client.to(createChatDto.firebaseUuid);
    this.chatService.create(createChatDto);
    console.log(client.id);
    return client.emit("createRoom", createChatDto,);
  }

firebaseUuid 是房间的名称。我的想法是:

这就是发生的事情

在日志中,消息显示正常,但屏幕上不显示。如果你能告诉我我做错了什么,我将不胜感激。

【问题讨论】:

    标签: flutter socket.io nestjs prisma prisma-flutter


    【解决方案1】:

    您不需要返回 client.emit(...) 函数

    查看此tutorial,了解如何在 Nestjs 上创建聊天应用程序

    【讨论】:

      猜你喜欢
      • 2013-01-03
      • 1970-01-01
      • 2021-12-08
      • 2022-06-30
      • 2017-11-28
      • 2016-11-12
      • 2021-03-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多