Cracker0dks,爲什麼你使用純websockets而不是庫。有了primus,你可以使用substream - 命名空間 - 這或多或少正是你想要的 - 你也可以使用帶有primus的二元分析器。
Socket.io也是一種選擇,但他們並不像普里默斯。好(在我看來)
目前它是最支持/穩定/完整的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')
});
上述解決方案被從他們的文檔中提取出來並更改爲適合您的示例 - 我沒有測試它,但它應該可以正常工作。
我希望有所幫助。
您可以發送一個類型爲「'{type:」uint8「,data:myArrayBuffer}'的對象。 – jfriend00 2015-02-10 01:19:59
不幸的是,當我傳輸的數據比數據元素多時,我沒有語音。 myWebSocket.send([e.data,「s」]);和var voice = new Uint16Array(data [0]);來測試它。但不工作,因爲「聲音」是空的。不知道爲什麼websockets是如此不情願。 – Cracker0dks 2015-02-10 01:52:21
好的,我正在考慮爲您封裝數據結構的'socket.io'。不是我猜想的香草webSocket界面。 – jfriend00 2015-02-10 02:48:17