问题描述
我有一个node.js服务器应用程序和一个浏览器客户端。发送 ArrayBuffer
数据浏览器 - >服务器工作正常,但服务器 - >浏览器产生字符串[object ArrayBuffer]
被接受。这种情况发生在Chrome和Firefox的最新版本中。
I have a node.js server application and a browser client. Sending ArrayBuffer
data browser -> server works perfectly, but server -> browser results in a string "[object ArrayBuffer]"
being received. This happens in the latest versions of both Chrome and Firefox.
服务器:
var serverPort = 9867;
// dependencies
var webSocketServer = require('websocket').server;
var http = require('http');
var players = {};
var nextPlayerId = 0;
// create http server
var server = http.createServer(function(request, response) { });
server.listen(serverPort, function() {
console.log((new Date()) + " Server is listening on port " + serverPort);
});
// create websocket server
var wServer = new webSocketServer({ httpServer: server });
// connection request callback
wServer.on('request', function(request) {
var connection = request.accept(null, request.origin);
connection.binaryType = "arraybuffer";
var player = {};
player.connection = connection;
player.id = nextPlayerId;
nextPlayerId++;
players[player.id] = player;
console.log((new Date()) + ' connect: ' + player.id);
// message received callback
connection.on('message', function(message) {
if (message.type == 'binary' && 'binaryData' in message && message.binaryData instanceof Buffer) {
// this works!
console.log('received:');
console.log(message);
}
});
// connection closed callback
connection.on('close', function(connection) {
console.log((new Date()) + ' disconnect: ' + player.id);
delete players[player.id];
});
});
function loop() {
var byteArray = new Uint8Array(2);
byteArray[0] = 1;
byteArray[0] = 2;
for (var index in players) {
var player = players[index];
console.log('sending: ');
console.log(byteArray.buffer);
player.connection.send(byteArray.buffer);
}
}
timerId = setInterval(loop, 500);
客户:
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<script type="text/javascript">
window.WebSocket = window.WebSocket || window.MozWebSocket;
var connection = new WebSocket('ws://127.0.0.1:9867');
connection.binaryType = "arraybuffer";
// most important part - incoming messages
connection.onmessage = function (event) {
document.getElementById("log").innerHTML += typeof(event.data) + ' ';
document.getElementById("log").innerHTML += event.data + ' ';
if (event.data instanceof ArrayBuffer) {
// string received instead of a buffer
}
};
window.onkeydown = function(e) {
var byteArray = new Uint8Array(2);
byteArray[0] = 1;
byteArray[1] = e.keyCode;
connection.send(byteArray.buffer);
};
</script>
<div id='log'>Log: </div>
</body>
</html>
我做错了什么?
修改
来自node.js websocket源:
From the node.js websocket source:
WebSocketConnection.prototype.send = function(data, cb) {
if (Buffer.isBuffer(data)) {
this.sendBytes(data, cb);
}
else if (typeof(data['toString']) === 'function') {
this.sendUTF(data, cb);
}
所以如果你使用 Uint8Array
,它将数据作为字符串发送,而不是使用 sendBytes
,因为 sendBytes
需要缓冲区
对象。如下面的答案,我需要 sendBytes
。由于我无法将 ArrayBuffer
传递给 sendBytes
,我在服务器上执行了此操作:
So if you use an Uint8Array
, it sends the data as a string, instead of using sendBytes
, as sendBytes
needs a Buffer
object. As in the answer below, I need sendBytes
. As I can't pass an ArrayBuffer
to sendBytes
, I did this on the server:
function loop() {
var buffer = new Buffer(2);
buffer[0] = 1;
buffer[1] = 2;
for (var index in players) {
var player = players[index];
console.log('sending: ');
console.log(buffer);
player.connection.send(buffer);
}
}
现在可行。
结论:
Chrome和Firefox网页套件 .send()
一个 Uint8Array
缓冲区作为二进制数据,似乎node.js websockets将其作为 string
数据发送,你需要一个 Buffer
缓冲区来发送二进制文件。
While Chrome and Firefox websockets .send()
a Uint8Array
buffer as binary data, it seems node.js websockets send it as string
data, and you need a Buffer
buffer to send binary.
推荐答案
发送二进制数据使用 sendBytes()
方法。
send binary data use sendBytes()
method.
这篇关于在浏览器中接收WebSocket ArrayBuffer数据 - 改为接收字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!