angular / nest js scoket io中无法在所有客户端接收数据。
创始人
2024-10-15 05:00:26
0

要在所有客户端接收数据,您可以使用Angular和NestJS中的Socket.IO来实现实时通信。下面是一个示例代码,展示了如何在Angular和NestJS之间进行Socket.IO通信。

首先,在NestJS中创建一个WebSocket模块,用于处理Socket.IO连接和事件:

// src/websocket/websocket.module.ts

import { Module } from '@nestjs/common';
import { SocketGateway } from './socket.gateway';

@Module({
  providers: [SocketGateway],
})
export class WebSocketModule {}
// src/websocket/socket.gateway.ts

import {
  SubscribeMessage,
  WebSocketGateway,
  WebSocketServer,
} from '@nestjs/websockets';
import { Server } from 'socket.io';

@WebSocketGateway()
export class SocketGateway {
  @WebSocketServer() server: Server;

  @SubscribeMessage('message')
  handleMessage(client: any, payload: any): void {
    this.server.emit('message', payload); // 向所有客户端发送消息
  }
}

然后,创建一个Angular服务来处理与NestJS的Socket.IO通信:

// src/app/socket.service.ts

import { Injectable } from '@angular/core';
import { io, Socket } from 'socket.io-client';
import { Observable } from 'rxjs';

@Injectable({
  providedIn: 'root',
})
export class SocketService {
  private socket: Socket;

  constructor() {
    this.socket = io('http://localhost:3000'); // 与NestJS服务器建立Socket.IO连接
  }

  sendMessage(message: string): void {
    this.socket.emit('message', message);
  }

  getMessage(): Observable {
    return new Observable((observer) => {
      this.socket.on('message', (data: string) => {
        observer.next(data); // 接收来自服务器的消息
      });
    });
  }
}

最后,在Angular组件中使用SocketService来发送和接收消息:

// src/app/app.component.ts

import { Component, OnDestroy, OnInit } from '@angular/core';
import { Subscription } from 'rxjs';
import { SocketService } from './socket.service';

@Component({
  selector: 'app-root',
  template: `
    
{{ msg }}
`, }) export class AppComponent implements OnInit, OnDestroy { message: string; messages: string[] = []; private subscription: Subscription; constructor(private socketService: SocketService) {} ngOnInit(): void { this.subscription = this.socketService .getMessage() .subscribe((message: string) => { this.messages.push(message); // 接收来自服务器的消息并添加到消息数组中 }); } ngOnDestroy(): void { this.subscription.unsubscribe(); } sendMessage(): void { this.socketService.sendMessage(this.message); // 发送消息到服务器 this.message = ''; } }

确保在NestJS中启用WebSocket模块,并在Angular中提供SocketService:

// src/main.ts

import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
import { SocketAdapter } from './websocket/socket.adapter';

async function bootstrap() {
  const app = await NestFactory.create(AppModule);
  app.useWebSocketAdapter(new SocketAdapter(app));
  await app.listen(3000);
}
bootstrap();
// src/app.module.ts

import { Module } from '@nestjs/common';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { WebSocketModule } from './websocket/websocket.module';

@Module({
  imports: [WebSocketModule],
  controllers: [AppController],
  providers: [AppService],
})
export class AppModule {}

以上就是一个基本的示例,可以让所有客户端接收数据。您可以根据自己的需求进行修改和扩展。

相关内容

热门资讯

此事引发广泛关注"功... 此事引发广泛关注"功夫川麻小程序有挂吗"一贯存在有辅助神器(哔哩哔哩)运功夫川麻小程序有挂吗辅助工具...
透视了解"越乡游义乌... 透视了解"越乡游义乌透视软件"切实是有辅助教程(哔哩哔哩);1、越乡游义乌透视软件模拟器是什么优化,...
黑科技辅助挂"乐酷辅... 黑科技辅助挂"乐酷辅助"一贯是有辅助插件(哔哩哔哩)1、任何乐酷辅助透视是真的假的的玩家都可以机会成...
据监测"新518互游... 据监测"新518互游脚本"切实真的是有辅助攻略(哔哩哔哩)1、任何新518互游脚本透视是真的假的的玩...
透视好友"葫芦娃七子... 透视好友"葫芦娃七子连心攻略"好像真的是有辅助插件(哔哩哔哩)进入游戏-大厅左侧-新手福利-激活码辅...
透视总结"友友联盟免... 透视总结"友友联盟免费辅助器"确实是真的有辅助攻略(哔哩哔哩)1、友友联盟免费辅助器脚本辅助下载、友...
值得注意的是"广西友... 值得注意的是"广西友乐软件辅助"真是存在有辅助器(哔哩哔哩)广西友乐软件辅助破解侠是真的助透视。每个...
透视智能ai"爱来掌... 透视智能ai"爱来掌中宝有没有挂"好像是有辅助插件(哔哩哔哩)暗藏猫腻,小编详细说明爱来掌中宝有没有...
透视好牌"佛手在线破... 透视好牌"佛手在线破解"竟然确实有辅助app(哔哩哔哩)1、每一步都需要思考,不同水平的挑战会更加具...
透视好友房"微乐智能... 透视好友房"微乐智能辅助app"果然真的有辅助器(哔哩哔哩)1、微乐智能辅助app透视辅助软件激活码...