如何通过Websockets路由不同的数据类型
how to route different data types over Websockets?
我必须通过websocket服务器发送大量ArrayBuffer(音频语音数据)。问题是,客户端必须知道传入数据是哪种类型的ArrayBuffer(Uint8/Uint16/Float32…)。如果用户切换到其他音频质量,该类型可能会随时更改。
向客户端通知阵列类型的最佳方式是什么?
迄今为止的想法:
- 在数组中添加一个额外的前缀字节(这可能很慢,因为我必须为每个音频块创建一个新的arrayBuffer)
- 使用不同的路由,如/16float或/uint8来知道哪些数据即将到来。(我没有找到任何关于如何使用websocket的信息)
有更好的方法吗?有人能给我举一个使用websocket的URL路径路由的例子吗?
编辑:我实现了前缀字节来发送有关客户端和数组类型的信息,但仍然对更好/其他的解决方案感兴趣。
Cracker0dks,为什么要使用纯websocket而不是库。有了primus,你可以使用子流名称空间——这或多或少正是你想要的——你也可以使用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解释器如何理解变量的数据类型
- 从查询字符串参数推断出正确的数据类型
- 为什么不'我们在javascript中使用函数参数的数据类型
- 基于数据类型的编辑框的汇总列表
- 如何降低此函数的复杂性,该函数根据数据类型进行回调
- 比较和分配数据类型的困难
- 在javascript中调用函数/对象引用时,可容纳任何数据类型
- E4X中是否有一种方法可以将基元数据类型的XMLList转换为类似于Array.join()的Array
- Highcharts系列.数据数据类型限制
- Hapi-js重定向到另一个带有额外请求数据的路由
- 如何在php循环中显示隐藏的数据类型
- 如何从标签数据类型=“;无线电”;
- 如何使用 Java 脚本从 csv 文件中查找每列的数据类型
- 在 Javascript 中更改数据类型
- 抽象数据类型等效于类型
- JavaScript中的“符号”原始数据类型是什么?
- 数据类型Jasmine
- 有没有一种方法可以在Typescript中自动检查数据类型的数据相等性
- 了解JavaScript中的基元数据类型
- 如何通过Websockets路由不同的数据类型