• js 使用socket-io发送文件


    前端

    import { Component, OnInit, ViewChild, ElementRef } from '@angular/core';
    import { MediaDevicesService } from '../../media-devices.service';
    import * as io from 'socket.io-client';
    import { MESSAGE_EVENT } from '@live-video-example/p2p';
    
    @Component({
      selector: 'live-video-example-get-audio',
      templateUrl: './get-audio.component.html',
      styleUrls: ['./get-audio.component.styl'],
    })
    export class GetAudioComponent implements OnInit {
      constructor(private readonly mediaDevicesService: MediaDevicesService) {}
    
      @ViewChild('audio')
      audioRef: ElementRef<HTMLAudioElement>;
    
      localStream: MediaStream;
      mr: MediaRecorder;
      recordedChunks: any[] = [];
      private socket?: SocketIOClient.Socket;
    
      isStart = false;
    
      async ngOnInit() {
        await this._initLocalStream();
        await this._initSocket();
        // await this.ngAfterViewInit();
        await this._initMr();
      }
      private _initSocket() {
        this.socket = io(`https://dev.ajanuw.com:3333/audio`);
    
        this.socket.on(MESSAGE_EVENT, (data) => {
          this.saveFile(
            new Blob([data.audio], {
              type: data.type,
            })
          );
        });
      }
    
      private async _initLocalStream() {
        this.localStream = await this.mediaDevicesService.getUserMedia({
          audio: true,
        });
      }
    
      private async _initMr() {
        this.mr = new MediaRecorder(this.localStream);
        this.mr.addEventListener('dataavailable', (e: any) => {
          if (e.data.size > 0) this.recordedChunks.push(e.data);
        });
    
        this.mr.addEventListener('stop', () => {
          const type = 'audio/mp3; codecs=opus';
          const blob = new Blob(this.recordedChunks, {
            type,
          });
          this._sendServer(blob, type);
        });
      }
    
      saveFile(blob: Blob) {
        const downloadLink = document.createElement('a');
        downloadLink.href = URL.createObjectURL(blob);
        downloadLink.download = 'acetest.mp3';
        downloadLink.click();
      }
    
      ngAfterViewInit(): void {
        if (!this.localStream) return;
        this.audioRef.nativeElement.srcObject = this.localStream;
      }
    
      download() {
        this.isStart = false;
    
        this.mr.stop();
      }
    
      start() {
        this.isStart = true;
        this.mr.start();
      }
    
      /**
       * 将录制的audio blob发送给服务器
       */
      private async _sendServer(audio: Blob, type: string) {
        this.socket.emit(MESSAGE_EVENT, { audio, type });
      }
    }
    

    服务器

    import {
      SubscribeMessage,
      WebSocketGateway,
      WebSocketServer,
      OnGatewayConnection,
      OnGatewayDisconnect,
    } from '@nestjs/websockets';
    import { Socket } from 'socket.io';
    import { MESSAGE_EVENT } from '@live-video-example/p2p';
    
    @WebSocketGateway({ namespace: 'audio' })
    export class AudioGateway implements OnGatewayConnection, OnGatewayDisconnect {
      @WebSocketServer() server: Socket;
    
      handleDisconnect(client: Socket) {}
    
      handleConnection(client: Socket, ...args: any[]) {}
    
      @SubscribeMessage(MESSAGE_EVENT)
      onMessage(client: Socket, payload: any): any {
        console.log(payload);
    
        client.emit(MESSAGE_EVENT, payload);
      }
    }
    

    See also:

  • 相关阅读:
    基于MATLAB求解矩阵的正交补矩阵
    MySQL的安装与配置
    删除ubuntu后修复win7系统的引导
    VS2010中快捷添加命名空间
    java学习之函数
    java学习之break 和 continue
    For循环复杂练习
    For循环练习之99乘法表和转义字符
    java学习之语句结构
    java学习之运算符
  • 原文地址:https://www.cnblogs.com/ajanuw/p/12746109.html
Copyright © 2020-2023  润新知