我想处理使用ws从客户端收到的ArrayBuffer消息。在客户端,由于ws,我直接接收一个ArrayBuffer。binaryType,但服务器端,我没有收到一个ArrayBuffer,但像这样的东西(字符串?):'<缓冲区00 _x0030_0=" _x0032_a=">'。缓冲区00>
服务器如何接收一个ArrayBuffer ?
客户端:
var ws = new WebSocket("ws://localhost:3000/");
ws.binaryType = 'arraybuffer';
ws.onopen = function() {
var buffer = new ArrayBuffer(15);
var dv = new DataView(buffer);
dv.setInt16(1, 42);
ws.send(buffer);
};
服务器端:var express = require('express'),
app = express(),
server = require('http').createServer(),
WebSocketServer = require('ws').Server,
wss = new WebSocketServer({
server: server
});
...
wss.on('connection', function(socket) {
console.log(message);
//log : <Buffer 00 00 2a 00 00 00 00 00 00 00 00 00 00 00 00 00>
socket.on('message', function(message) {
//would like to read and handle the arraybuffer properly
});
});
只需将Buffer转换为ArrayBuffer
var buf = new Uint8Array(message).buffer;
var dv = new DataView(buf);