从WebSocket接收的JavaScript blob读取字节
我有一个WebSocket,它接收二进制消息,我希望遍历字节 我提出了以下转换函数从WebSocket接收的JavaScript blob读取字节,javascript,html,performance,Javascript,Html,Performance,我有一个WebSocket,它接收二进制消息,我希望遍历字节 我提出了以下转换函数 // Convert the buffer to a byte array. function convert(data, cb) { // Initialize a new instance of the FileReader class. var fileReader = new FileReader(); // Called when the read operation is su
// Convert the buffer to a byte array.
function convert(data, cb) {
// Initialize a new instance of the FileReader class.
var fileReader = new FileReader();
// Called when the read operation is successfully completed.
fileReader.onload = function () {
// Invoke the callback.
cb(new Uint8Array(this.result));
};
// Starts reading the contents of the specified blob.
fileReader.readAsArrayBuffer(data);
}
这确实有效,但性能很差。是否有更好的方法允许读取字节?您是否考虑过:
socket.binaryType = 'arraybuffer';
该功能变为:
function convert(data) {
return new Uint8Array(data);
}
它实际上不需要做任何工作,因为它只是缓冲区上的一个视图。您使用的浏览器是什么?Google Chrome是我使用的浏览器。在可维护性和性能方面有了巨大的改进。伟大的