我正在尝试构建自己的实时视频应用程序,而且我无法通过websocket将视频帧从网络摄像头发送到服务器。 我使用标准浏览器websocket而没有任何外部库,遵循api here:
我每隔30ms捕捉一次网络摄像头画布并将其写入我的websocket。
ws = new WebSocket(url);
....
function update(){
ctx.drawImage(video, 0, 0, 320, 240);
var data = canvas.get()[0].toDataURL('image/jpeg', 1.0);
blob = dataURItoBlob(data);
ws.send(blob)
}
....
setInterval(update, 30)
我收到了这个错误:
CloseEvent {wasClean: true, code: 1009, reason: "Frame size of 91552 bytes exceeds maximum accepted frame size", type: "close", target: WebSocket…}
所以我意识到我需要开始编写块。 我想知道什么是最通用的解决方法。如何选择最佳块大小?
这里是dataURItoBlob函数,因此您可以了解它的作用
function dataURItoBlob(dataURI) {
var binary = atob(dataURI.split(',')[1]);
var array = [];
for(var i = 0; i < binary.length; i++) {
array.push(binary.charCodeAt(i));
}
return new Blob([new Uint8Array(array)], {type: 'image/jpeg'});
}