我正在尝试通过websocket将recordRTC blob发送到我的Java服务器,我的目标是构建视频流服务。
但是,当服务器收到Blob并写入文件时,网页将刷新,导致recordRTC停止并且套接字断开连接。
使用过的包裹
<dependency>
<groupId>org.java-websocket</groupId>
<artifactId>Java-WebSocket</artifactId>
<version>1.4.0</version>
</dependency>
以下是我的代码。
服务器端
public void onMessage( WebSocket conn, ByteBuffer message ) {
byte[] bytes = new byte[message.remaining()];
message.get(bytes, 0, bytes.length);
if(bytes.length > 0) {
OutputStream os = null;
UUID uuid = UUID.randomUUID();
String id=uuid.toString();
String filename =id.replace("-", "");
try {
BufferedOutputStream bos = new BufferedOutputStream(
new FileOutputStream(
new File("./uploads/"+filename +".webm")));
bos.write(bytes);
bos.flush();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
客户端
function startCapture() {
logElem.innerHTML = "";
try {
videoElem.srcObject = navigator.mediaDevices.getDisplayMedia(displayMediaOptions).then(
(stream)=>{
console.log(stream);
recordRTC = RecordRTC(stream);
recordRTC.startRecording();
// blob segment and send every 3 second
recording = setInterval(()=>{
recordRTC.stopRecording(()=>{
console.log("BLOB : ", recordRTC.getBlob());
recordRTC.getBlob().arrayBuffer().then((val)=>{
console.log("BLOB : ", recordRTC.getBlob());
socket.send(val);
});
})
recordRTC.startRecording();
}, 3000);
videoElem.srcObject = new MediaStream(stream);
});
} catch (err) {
console.error("Error: " + err);
}
}
我发现重装事件在fisrt blob发送并写入文件后发生,但我不知道为什么发生该事件。
如果服务器不将blob写入文件,只需将其接收,则websocket和web可以完美运行而无需重新加载。
我该如何解决重新加载EVENT问题? 谢谢