Databuffer Doesn't Write to File
So I'm Trying to Send Videochunks(Blobs) from a Client to My Server Through Reactive Websocket in Webflux and Save Each Chunk into the Same File, for Now. the...
So I'm trying to send videochunks(Blobs) from a client to my server through reactive websocket in webflux and save each chunk into the same file, for now. The blobs are about 100kb each and it seems like the serverside is receiving them, because the terminal gives me a cryptic output every time the clients sends a blob.
Unfortunately the file is beeing created but the received data(videoDataFlux) is not written to the file. The file is always 0 bytes.
It makes no difference if I don't use DatabufferUtils::realease.
If someone could give me a hint, that would be great.
ReactiveWebSocketHandler.java
[...]
@Override
public Mono<Void> handle(WebSocketSession webSocketSession) {
String filename = "Streamname_" + webSocketSession.getId();
Path path = FileSystems.getDefault().getPath("C:\\Program Files (x86)\\Apache Software Foundation\\Tomcat 9.0\\webapps\\stream\\videos");
//File filelocation = new File(path + filename);
System.out.println("The path is: " + path);
Flux<DataBuffer> videoDataFlux = webSocketSession.receive()
.map(WebSocketMessage::getPayload);
try {
//AsynchronousFileChannel channel = AsynchronousFileChannel.open(path, StandardOpenOption.WRITE);
//DataBufferUtils.write(videoDataFlux, channel, 0).subscribe();
Path file = Files.createTempFile(path, filename, ".webm");
WritableByteChannel channel = Files.newByteChannel(file, StandardOpenOption.WRITE);
DataBufferUtils.write(videoDataFlux, channel)
.map(DataBufferUtils::release)
.then(Mono.just(file));
} catch (IOException e) {
}
return webSocketSession.send(intervalFlux
.map(webSocketSession::textMessage))
.and(webSocketSession.receive());
//.map(WebSocketMessage::getPayloadAsText).log());
}
UPDATE:
I tried it like Thomas Andolf said but now there is a weird behaviour. It gets data which actually shouldn't even be send by the client and writes it once to a file. So that is workin partially. But if the client starts sending data, it takes the first blob, doesn't write it to a file and immmediately closes the websocket connection.