Answer the question
In order to leave comments, you need to log in
How to redirect streaming video from the server to the video tag using socket.io?
'use strict'
var app = require('express')();
var http = require('http').createServer(app);
var io = require('socket.io')(http);
var fs = require('fs');
app.get('/', (req, res) => {
res.sendFile(__dirname + '/index.html');
});
io.sockets.on('connection', (socket) => {
console.log('a user connected');
socket.on('disconnect', function () {
console.log('A user disconnected');
});
socket.emit('message', 'Welcome');
socket.on('message', function all(message){
console.log(message);
socket.broadcast.emit('message', message);
});
let streamVideo = fs.createReadStream('/js/ControlPC/images/2.mp4');
streamVideo.on('data', (bufferVideo)=>{
//console.log(chunk);
socket.emit('streamVideo', bufferVideo);
})
});
io.on("err", (err)=>{
console.log(err.message);
})
http.listen(8000, () => {
console.log('listening on *:8000');
});
<!DOCTYPE html>
<html>
<head>
<title>Hello world</title>
<script src = "/socket.io/socket.io.js"></script>
<script>
var socket = io();
socket.on('message', function(mes){
alert('server message: ' + mes);
});
function is (){
console.log("run")
socket.emit('message', "Hello all");
}
</script>
</head>
<body>
<p>Hello world</p>
<video src="/js/ControlPC/images/2.mp4" id="stream" controls width="auto" height="auto" autoplay muted></video>
<script>
/*
socket.on('streamVideo', function(bufferVideo){
let stream = document.getElementById('stream');
//stream.src = bufferVideo;
console.log(stream);
});
*/
let videoElement = document.createElement("video");
videoElement.setAttribute("id", 'stream2');
videoElement.setAttribute('src', '/js/ControlPC/images/1.mp4');
videoElement.setAttribute("width", "auto");
videoElement.setAttribute("height", "auto");
videoElement.setAttribute("controls", "controls");
videoElement.setAttribute("autoplay", "");
videoElement.setAttribute("muted", "");
document.body.append(videoElement);
</script>
<p><input type="button" onclick="is()" value="Poke the server" id="poke" /></p>
</body>
</html>
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question