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);
}
}
js 使用socket-io发送文件的更多相关文章
- Node.js 和Socket.IO 实现chat WEBIM
socket官方: http://socket.io/ 需求:实现WEB IM功能,数据从服务器PUSH 不是PULL websocket是基于HTML5的新特性,不兼容IE6,7,8 .. ...
- 使用Node.js的socket.io模块开发实时web程序
首发:个人博客,更新&纠错&回复 今天的思维漫游如下:从.net的windows程序开发,摸到nodejs的桌面程序开发,又熟悉了一下nodejs,对“异步”的理解有了上上周对操作系统 ...
- node.js和socket.io纯js实现的即时通讯实例分享
在这个例子中,其实node.js并没有真正起到服务器的作用,因为我们这里可以直接运行client.html文件,而不用输入url请求,当 然,要想输入url请求页面内容还需要加入请求静态文件的代码.这 ...
- node.js和socket.io实现im
im——Instant Messaging 即时通讯 基本技术原理 (1)通过IM服务器登陆或注销 (2)用户A通过列表找到B,用户B获得消息并与之交谈 (3)通过IM服务器指引建立与B单独的通讯通道 ...
- 利用Socket远程发送文件
思想: 1.注意使用两个通道,一个普通对象通信通道,另一个纯净的文件字节流通道 2.利用通信通道发送文件请求,新建字节流通道,开始发送文件
- [Node.js] 基于Socket.IO 的私聊
原文地址:http://www.moye.me/2015/01/02/node_socket-io/ 引子 最近听到这么一个问题:Socket.IO 怎么实现私聊?换个提法:怎么定位到人(端),或者说 ...
- Socket.io发送消息含义
仅作收藏:转自博客园 若相忆; // send to current request socket client socket.emit('message', "this is a test ...
- socket.io发送给指定的客户端
http://www.cnblogs.com/ajccom/archive/2013/07/18/3197809.html http://stackoverflow.com/questions/100 ...
- 05 Node.js学习笔记之发送文件数据
这章学习在NodeJs中如何将Html文件发送到客户端上,以及定义Content-Type内容类型 //1.载入http和fs模块 var http=require("http") ...
- 使用Node.js+Socket.IO搭建WebSocket实时应用
Web领域的实时推送技术,也被称作Realtime技术.这种技术要达到的目的是让用户不需要刷新浏览器就可以获得实时更新.它有着广泛的应用场景,比如在线聊天室.在线客服系统.评论系统.WebIM等. W ...
随机推荐
- 【算法】深度优先搜索(dfs)
突然发现机房里有很多人不会暴搜(dfs),所以写一篇他们能听得懂的博客(大概?) PS:万能 yuechi ---- 大法师怎么能不会呢?! 若有错误,请 dalao 指出. 前置 我知道即使很多人都 ...
- 十六:SpringBoot-自定义启动页,项目打包和指定运行环境
SpringBoot-自定义启动页,项目打包和指定运行环境 1.自定义启动页 2.打包配置 2.1 打包pom配置 2.2 多环境配置 3.环境测试接口 4.打包执行 4.1 指定模块打包 4.2 运 ...
- socket套接字编程(1)——基本函数
TCP交互流程: 服务器:1. 创建socket:2. 绑定socket和端口号:3. 监听端口号:4. 接收来自客户端的连接请求:5. 从socket中读取字符:6. 关闭socket. 客户端:1 ...
- SpringApplication.run
SpringApplication.run一共做了两件事,分别是 创建SpringApplication对象 利用创建好的SpringApplication对象,调用run方法 1.创建SpringA ...
- java中的IO处理和使用,API详细介绍(二)
字符流 [向文件中写入数据] 现在我们使用字符流 /** * 字符流 * 写入数据 * */ import java.io.*; class hello{ public static void mai ...
- Java排序算法(三)直接插入排序
一.测试类SortTest import java.util.Arrays; public class SortTest { private static final int L = 20; publ ...
- bootstrap实例
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- Codeforces Round #677 (Div. 3) G. Reducing Delivery Cost(dijkstra算法)
题目链接:https://codeforces.com/contest/1433/problem/G 题解 跑 \(n\) 遍 \(dijkstra\) 得到任意两点间的距离,然后枚举哪一条边权为 \ ...
- HDU - 4300 Clairewd’s message (拓展kmp)
HDU - 4300 题意:这个题目好难读懂,,先给你一个字母的转换表,然后给你一个字符串密文+明文,密文一定是全的,但明文不一定是全的,求最短的密文和解密后的明文: 题解:由于密文一定是全的,所以他 ...
- poj 1511-- Invitation Cards (dijkstra+优先队列)
刚开始想复杂了,一直做不出来,,,其实就是两遍dijkstra+优先队列(其实就是板子题,只要能有个好的板子,剩下的都不是事),做出来感觉好简单...... 题意:有n个车站和n个志愿者,早上每个志愿 ...