前端

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);
}
}

js 使用socket-io发送文件的更多相关文章

  1. Node.js 和Socket.IO 实现chat WEBIM

    socket官方:   http://socket.io/  需求:实现WEB IM功能,数据从服务器PUSH  不是PULL  websocket是基于HTML5的新特性,不兼容IE6,7,8 .. ...

  2. 使用Node.js的socket.io模块开发实时web程序

    首发:个人博客,更新&纠错&回复 今天的思维漫游如下:从.net的windows程序开发,摸到nodejs的桌面程序开发,又熟悉了一下nodejs,对“异步”的理解有了上上周对操作系统 ...

  3. node.js和socket.io纯js实现的即时通讯实例分享

    在这个例子中,其实node.js并没有真正起到服务器的作用,因为我们这里可以直接运行client.html文件,而不用输入url请求,当 然,要想输入url请求页面内容还需要加入请求静态文件的代码.这 ...

  4. node.js和socket.io实现im

    im——Instant Messaging 即时通讯 基本技术原理 (1)通过IM服务器登陆或注销 (2)用户A通过列表找到B,用户B获得消息并与之交谈 (3)通过IM服务器指引建立与B单独的通讯通道 ...

  5. 利用Socket远程发送文件

    思想: 1.注意使用两个通道,一个普通对象通信通道,另一个纯净的文件字节流通道 2.利用通信通道发送文件请求,新建字节流通道,开始发送文件

  6. [Node.js] 基于Socket.IO 的私聊

    原文地址:http://www.moye.me/2015/01/02/node_socket-io/ 引子 最近听到这么一个问题:Socket.IO 怎么实现私聊?换个提法:怎么定位到人(端),或者说 ...

  7. Socket.io发送消息含义

    仅作收藏:转自博客园 若相忆; // send to current request socket client socket.emit('message', "this is a test ...

  8. socket.io发送给指定的客户端

    http://www.cnblogs.com/ajccom/archive/2013/07/18/3197809.html http://stackoverflow.com/questions/100 ...

  9. 05 Node.js学习笔记之发送文件数据

    这章学习在NodeJs中如何将Html文件发送到客户端上,以及定义Content-Type内容类型 //1.载入http和fs模块 var http=require("http") ...

  10. 使用Node.js+Socket.IO搭建WebSocket实时应用

    Web领域的实时推送技术,也被称作Realtime技术.这种技术要达到的目的是让用户不需要刷新浏览器就可以获得实时更新.它有着广泛的应用场景,比如在线聊天室.在线客服系统.评论系统.WebIM等. W ...

随机推荐

  1. 圣诞快乐!OIer挂分小技巧

    OIer常犯错误 自己的错误 循环里套return 线段树求和 int 定义,下传 int 定义 cmp<,>号分不清 主观行为举动错误 踢电源线,注意安全(_Destiny) TLE 大 ...

  2. FFT,NTT 笔记

    FFT 简介 FFT是干啥的?它是用来加速多项式乘法的.我们平时经常求多项式乘法,比如\((x+1)(x+3)=(x^2+4x+3)\).假设两个式子都是\(n\)项(不足的补0),那朴素的算法是\( ...

  3. MariaDB数据库 ----数据库简介,用户管理,数据库创建,数据类型、数据增删改(实例演示)

    数据库简介 数据库--即电子文件柜,用户可以对文件中的数据进行增,删,改,查等操作. 数据库分类 关系型数据库 关系型数据库管理系统(Relational Database Management Sy ...

  4. centos 7.6安装WeADMIN ITOSS步骤

    0.升级系统并安装必要的软件 yum -y update yum -y install wget unzip nmap 1.下载JDK jdk下载地址:http://www.oracle.com/te ...

  5. F - F(最小生成树)

    题意:连通各点最短距离,最小生成树. You are assigned to design network connections between certain points in a wide a ...

  6. codeforces 86D D. Powerful array

    An array of positive integers a1, a2, ..., an is given. Let us consider its arbitrary subarray al, a ...

  7. Educational Codeforces Round 95 (Rated for Div. 2) A. Buying Torches (数学)

    题意:刚开始你有一个木棍,造一个火炬需要一个木根和一个煤块,现在你可以用一个木棍换取\(x\)个木棍,或者\(y\)根木棍换一个煤块,消耗一次操作,问最少需要操作多少次才能造出\(k\)把火炬. 题解 ...

  8. 已处理证书链,但是在不受信任提供程序信任的根证书中终止 - Windows 7安装.Net Framework 4.6.2时出现此问题

    https://blog.csdn.net/inchat/article/details/104294302

  9. HTTP的传输编码(Transfer-Encoding:chunked) / net::ERR_INVALID_CHUNKED_ENCODING

    https://blog.csdn.net/m0_37668842/article/details/89138733 https://www.cnblogs.com/jamesvoid/p/11297 ...

  10. Os-hackNos-1(drupal7+suid提权)

    一.信息收集 得到 ip是 192.168.56.101 , 端口开启了22和80,扫目录发现drupal 7 访问一下呢 在exploit-db上面搜索到存在相关的漏洞,而且是用直接上msf 使用第 ...