javascript - WebRTC鼠标指针

标签 javascript html webrtc

假设我们通过 webRTC 从 A 到 B 建立了连接。 B 还在窗口中看到了自己的网络摄像头。 是否可以在显示他自己的网络摄像头的 B 窗口上查看 A 鼠标箭头?

(因此,如果爱丽丝与鲍勃连接,爱丽丝可以使用他的指针向鲍勃指示鲍勃厨房里的勺子在哪里)。

最佳答案

使用 data channel将鼠标指针坐标从 A 发送到 B:

var pc1 = new RTCPeerConnection(), pc2 = new RTCPeerConnection();

pc1.onicecandidate = e => pc2.addIceCandidate(e.candidate);
pc2.onicecandidate = e => pc1.addIceCandidate(e.candidate);
pc1.onnegotiationneeded = e =>
  pc1.createOffer().then(d => pc1.setLocalDescription(d))
  .then(() => pc2.setRemoteDescription(pc1.localDescription))
  .then(() => pc2.createAnswer()).then(d => pc2.setLocalDescription(d))
  .then(() => pc1.setRemoteDescription(pc2.localDescription))
  .catch(e => console.log(e));

var dc = pc1.createDataChannel("mouse position");
document.onmousemove = e => dc.send(e.x + ", " + e.y);

pc2.ondatachannel = ({channel}) => channel.onmessage = e => console.log(e.data);
<div id="div"></div>
<script src="https://webrtc.github.io/adapter/adapter-latest.js"></script>

然后在另一端用 JavaScript 渲染它。低延迟在同伴交互中非常重要。

关于javascript - WebRTC鼠标指针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44387680/

相关文章:

javascript - 网络应用 ZXing 条码扫描仪

javascript - func.bind(thing)(arg) 与 func.bind(thing, arg)

html - 在视频(iframe 或对象)上显示 div

html - 将 html 表单居中对齐

ruby-on-rails - rails 5 : WebRTC video conferencing

javascript - 将元素入口点更改为公用文件夹后样式和 js 不起作用

javascript - Bootstrap 4 - 光泽效果和 z-index

html - 表格单元格的 :hover attribute doesn't change background color

video-streaming - WebRTC - 在通信过程中更改视频分辨率

javascript - 如何使用 RingCentral Web 电话客户端库取消注册用户代理?