javascript - 如何通过 Websocket 路由不同的数据类型?

标签 javascript websocket url-routing arraybuffer

我必须通过 websocket 服务器发送大量 ArrayBuffer(音频语音数据)。问题是,客户端必须知道传入数据是哪种类型的 ArrayBuffer(Uint8/Uint16/Float32 ...)。如果用户切换到其他音频质量,则类型可以即时更改。

告知客户端数组类型的最佳方式是什么?

到目前为止的想法:

  • 在数组中添加一个额外的前缀字节(这可能会很慢,因为我必须为每个音频 block 创建一个新的 arrayBuffer)
  • 使用不同的路由(例如/16float 或/uint8)来了解哪些数据即将到来。 (我还没有找到任何信息如何使用 websocket 完成此操作)

有更好的方法吗?任何人都可以给我一个示例,说明 URL-Path 路由如何使用 websocket 工作?

<小时/>

编辑: 我实现了前缀字节来发送有关客户端和数组类型的信息,但仍然对更好/其他解决方案感兴趣。

最佳答案

Cracker0dks,为什么你使用纯 websockets 而不是库。使用 primus,您可以使用 substream - 命名空间 - 这或多或少正是您想要的 - 您还可以将二进制解析器与 primus 一起使用。

Socket.io 也是一个选择,但它们不如 primus。(在我看来)

目前它是最受支持/稳定/完整的 ws 解决方案

 // server side:
var primus
    , server
    , substream
    , http
    , Uint8
    , Uint16
    , Float32
    ;

Primus = require('primus');
http = require('http');
substream = require('substream');

server = http.createServer();
primus = new Primus(server);

primus.use('substream', substream);
server.listen(8000);

primus.on('connection', function (spark) {
    Uint8 = spark.substream('Uint8');
    Uint16 = spark.substream('Uint16');
    Float32 = spark.substream('Float32');

    Uint8.on('data', function (data) {
        console.log(data); //we recieve data from client on Uint8 ('to server') 
    });

    Uint16.on('data', function (data) {
        console.log(data); //we recieve data from client on Uint16 ('to server') 
    });

    Float32.on('data', function (data) {
        console.log(data); //we recieve data from client on Float32 ('to server') 
    });

    Uint8.write('to client'); // write data to client to Uint8
    Uint16.write('to client'); // write data to client to Uint16
    Float32.write('to client'); // write data to client to Float32

    //
    // piping data to Float32 
    //
    fs.createReadSteam(__dirname + '/example.js').pipe(Float32, {
        end: false
    });

    //
    // To stop receiving data, simply end the substream:
    //
    Uint16.end();
});




// client side:
var primus
    , Uint8
    , Uint16
    , Float32
    ;
primus = new Primus('server address');
Uint8 = primus.substream('Uint8');
Uint8.write('to server'); // write data to server to Uint8

Uint16 = primus.substream('Uint16');
Uint16.write('to server'); // write data to server to Uint16

Float32 = primus.substream('Float32');
Float32.write('to server'); // write data to server to Float32


Uint8.on('data', function (data) {
    console.log(data); // you get data from server to Uint8 ('to client') 
});

Uint16.on('data', function (data) {
    console.log(data); // you get data from server to Uint8 ('to client') 
});

Float32.on('data', function (data) {
    console.log(data); // you get data from server to Uint8 ('to client') 
});

以上内容取自他们的文档,并进行了更改以适合您的示例 - 我没有测试它,但它应该可以工作。

希望对您有所帮助。

关于javascript - 如何通过 Websocket 路由不同的数据类型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28421827/

相关文章:

javascript - 使用 browserify 以 Angular 延迟加载组件

javascript - 有什么方法可以将 c3.js 生成的图形转换为 png,并在客户端将 Png 转换为 Pdf

html - 如何让我的搜索查询显示在 URL 中?

asp.net-mvc - 在 ASP.NET MVC 中避免使用路由规则的 Controller

java - Spring MVC 导致错误的操作

javascript - 为什么我的代码中的 cocoon 不捕获回调?

javascript - Google Maps JavaScript API 可在浏览器中使用,但无法在设备上的 Cordova 应用程序中使用

python - 使用线程和扭曲发送消息的高速公路

javascript - 意外的响应代码 : 200

javascript - Nodejs - 正确从其他文件调用方法