使用 Node.js 和 WebSockets 流式传输二进制文件 [英] Streaming Binary with Node.js and WebSockets

查看:106
本文介绍了使用 Node.js 和 WebSockets 流式传输二进制文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在谷歌上搜索这个并环顾 stackoverflow 一段时间,但没有找到解决方案 - 因此这篇文章.

出于好奇,我在玩 Node.js 和 WebSockets.我正在尝试将一些二进制数据(mp3)流式传输到客户端.到目前为止,我的代码如下,但显然没有按预期工作.

我怀疑我的问题是我实际上并未从服务器发送二进制数据,因此需要一些说明/帮助.

这是我的服务器...

var fs = require('fs');var WebSocketServer = require('ws').Server;var wss = new WebSocketServer({port: 8080,host:"127.0.0.1"});wss.on('connection', function(ws) {var readStream =fs.createReadStream("test.mp3",{'标志':'r','编码':'二进制','模式':0666,'bufferSize': 64 * 1024});readStream.on('data', function(data) {ws.send(data, {binary: true, mask: false});});});

还有我的客户...

context = new webkitAudioContext();var ws = new WebSocket("ws://localhost:8080");ws.binaryType = 'arraybuffer';ws.onmessage = 函数(evt){context.decodeAudioData(事件数据,功能(缓冲区){console.log("成功");},功能(错误){console.log("错误");});};

对 decode 的调用总是以错误回调结束.我假设这是因为它收到了错误的数据.

所以我的问题是如何正确地将文件流式传输为二进制文件?

谢谢

解决方案

问题已解决.

我通过从传递给createReadStream()"的选项和解决方案中删除'encoding':'binary'"参数的组合解决了这个问题.

decodeAudioData 返回空错误>

根据我的一些评论,当我更新 createReadStream 选项时,第一个块正在播放,但所有其他块都在执行来自 decodeAudioData() 的 onError 回调.上面链接中的解决方案为我解决了这个问题.decodeAudioData() 似乎对它接收的块应该如何格式化有点挑剔.它们显然应该是有效的块...

为 decodeAudioData (WebAudio API)

I've been googling this and looking around stackoverflow for a while but haven't found a solution - hence the post.

I am playing around with Node.js and WebSockets out of curiosity. I am trying to stream some binary data (an mp3) to the client. My code so far is below but is obviously not working as intended.

I suspect that my problem is that I am not actually sending binary data from the server and would like some clarification/help.

Heres my server...

var fs = require('fs');
var WebSocketServer = require('ws').Server;

var wss = new WebSocketServer({port: 8080,host:"127.0.0.1"});
wss.on('connection', function(ws) {    
    var readStream = 
    fs.createReadStream("test.mp3", 
     {'flags': 'r',
      'encoding': 'binary', 
      'mode': 0666, 
      'bufferSize': 64 * 1024});

    readStream.on('data', function(data) {
        ws.send(data, {binary: true, mask: false});
    });
});

And my client...

context = new webkitAudioContext();
var ws = new WebSocket("ws://localhost:8080");
ws.binaryType = 'arraybuffer';

ws.onmessage = function (evt) {
    context.decodeAudioData(
    evt.data,
        function(buffer) {
            console.log("Success");
        }, 
        function(error) {
            console.log("Error");
        });
};

The call to decode always end up in the error callback. I am assuming this is because it is receiving bad data.

So my question is how to I correctly stream the file as binary?

Thanks

解决方案

Problem solved.

I fixed this issue with a combination of removing the "'encoding': 'binary'" parameter from the options passed to "createReadStream()" and the solution at...

decodeAudioData returning a null error

As per some of my comments, when I updated the createReadStream options, the first chunk was playing but all other chunks were executing the onError callback from decodeAudioData(). The solution in the link above fixed this for me. It seems that decodeAudioData() is a bit picky as to how the chunks it receives should be formatted. They should be valid chunks apparently...

Define 'valid mp3 chunk' for decodeAudioData (WebAudio API)

这篇关于使用 Node.js 和 WebSockets 流式传输二进制文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆