我们正在尝试构建 .net 核心应用程序来共享屏幕或窗口。但它不起作用。
代码是这样的:
网页文件:
<div class="container">
<div class="row p-1">
<h1>WebRTC Screen Sharing Demo</h1>
<hr>
<button onclick="captureScreen()">Capture</button>
<video id="local-video" muted autoplay></video>
</div>
</div>
<script src="~/js/signalr/dist/browser/signalr.js"></script>
<script src="~/js/stream.js"></script>
流.js:
"use strict";
var connection = new signalR.HubConnectionBuilder().withUrl("/streamHub").build();
connection.start().then(function () {
console.log("Connection started");
}).catch(function (err) {
return console.error(err.toString());
});
connection.on("ClientReceiveStream", function (stream) {
document.getElementById("local-video").srcObject = stream; // I need to set the video source to the media stream.
});
async function captureScreen() {
let mediaStream = null;
try
{
mediaStream = await navigator.mediaDevices.getDisplayMedia({
video:
{
cursor: "always"
},
audio: false
});
//Im trying to send the media stream to the server
connection.invoke("ServerSendStream", mediaStream).catch(function (err)
{
return console.error(err.toString());
});
} catch (ex) {
console.log("Error occurred", ex);
}
}
枢纽:
public class StreamHub : Hub
{
public async Task ServerSendStream(object mediaStream) // I need to get the media stream here
{
await Clients.All.SendAsync("ClientReceiveStream", mediaStream);
}
}
我们如何将这个 mediaStream 对象发送到服务器,然后广播它呢?
回答1
无法向客户端广播流。客户端需要向服务器请求数据流。
请参阅有关如何执行此操作的文档:https://learn.microsoft.com/aspnet/core/signalr/streaming?view=aspnetcore-7.0#server-to-client-streaming-2
当在客户端使用数据时,您需要创建某种流并将数据添加到其中,可能类似于 https://developer.mozilla.org/en-US/docs/Web/API/MediaSource