I am trying to stream a video file over an websocket using StreamingResponseBody. But its not working. Is it possible to use StreamingResponseBody over websocket connection?
@Controller
@Slf4j
public class RoomController {
@Autowired
RoomService roomService;
@Autowired
private SimpMessagingTemplate messagingTemplate;
@MessageMapping("/message")
@SendTo("/topic/data")
public StreamingResponseBody getContent() throws IOException {
// Replace with the path to your video file
Path videoPath = Paths.get("F:\\earth.mp4");
// Open the video file as an InputStream
InputStream videoInputStream = Files.newInputStream(videoPath, StandardOpenOption.READ);
// Create an InputStreamSource from the InputStream
InputStreamSource source = new InputStreamResource(videoInputStream);
return outputStream -> {
log.warn("inside writeTo"); //<- not logging on console
InputStream is = source.getInputStream();
byte[] buffer = new byte[1024];
int bytesRead;
while ((bytesRead = is.read(buffer)) != -1) {
log.warn("Read byte {}", bytesRead);
outputStream.write(buffer, 0, bytesRead);
}
};
}
}