我尝试制作一个实时流槽socket.io。为了实现此目标,我通过套接字发送MediaStream并尝试将其设置为Client中的MediaStream: 服务器:
navigator.webkitGetUserMedia({
audio: false,
video: {
mandatory: {
chromeMediaSource: 'desktop',
chromeMediaSourceId: remote.getGlobal('screen').window,
minWidth: 0,
maxWidth: 10000,
minHeight: 0,
maxHeight: 10000
}
}
}, gotStream, getUserMediaError);
var ss = require('socket.io-stream')
function gotStream(stream) {
console.log("Got Stream! Starting websocket...");
var video = document.querySelector('video');
video.srcObject = stream
io.on('connection', function(socket) {
ss(socket).emit("stream", stream);
});
}
客户:
<body>
<video id="video" style="position: fixed; width: 100%" id="photo" style="width: 100%" src=""></video>
</body>
<script src="js/socketio.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/socket.io-stream/0.9.1/socket.io-stream.js"></script>
<script>
var socket = io('ws://192.168.2.110:3000');
socket.on('img', function(e) {
document.getElementById("photo").src = e;
});
ss(socket).on("stream", function(data) {
document.getElementById("video").srcObject = URL.createObjectURL(data);
});
</script>