返回资讯中心
外部精选
软件工程
#开发者实时流

webrtc-runtime 发布:为 Node.js、React Native 和浏览器提供统一 WebRTC API

来源标题:WebRTC for Node.js, React Native and Browser

名为 webrtc-runtime 的 npm 包发布,通过统一的 RTCPeerConnection API 在浏览器、Node.js 和 React Native 中实现 WebRTC 直连通信。该库支持 STUN/TURN NAT 穿透、DTLS-SRTP 加密媒体、SCTP 数据通道及自适应码率,并处理丢包与网络切换。

Hacker News 最新LuKks2026/11/8 · 今天7 分钟阅读

以下正文由 AI 翻译自 Hacker News 最新(原文版权归原站所有),可能存在偏差,重要内容请以原文为准。

Node.js、React Native 和浏览器上的 WebRTC。 npm i webrtc-runtime 直连。ICE 使用 STUN 进行 NAT 穿透,TURN 中继作为兜底。 低延迟。实时音频、视频和数据,支持自适应码率。 网络韧性。处理丢包、抖动和网络切换。 默认安全。DTLS-SRTP 加密媒体,DTLS 保护数据通道。 数据通道。SCTP 支持有序或无序、可靠或不可靠传输。 开放生态。基于 W3C 和 IETF 规范构建,实现可互操作。 一套 API 走天下。跨浏览器、Node.js 和 React Native 的 RTCPeerConnection。 有问题或想法?开个 GitHub issue :) const { RTCPeerConnection } = require('webrtc-runtime') const a = new RTCPeerConnection() const b = new RTCPeerConnection() const signalingA = perfectNegotiation(a, true, signal => signalingB(signal)) const signalingB = perfectNegotiation(b, false, signal => signalingA(signal)) a.ondatachannel = e => { e.channel.onmessage = msg => { console.log('A:', msg.data) e.channel.send('Hello from A') const channel = b.createDataChannel('test') channel.onopen = () => channel.send('Hello from B') channel.onmessage = e => console.log('B received:', e.data) 现代 WebRTC perfect negotiation(2020+): https://w3c.github.io/webrtc-pc/#perfect-negotiation-example function perfectNegotiation (pc, polite, send) { let makingOffer = false let ignoreOffer = false let settingAnswer = false pc.onnegotiationneeded = async () => { try { makingOffer = true await pc.setLocalDescription() send({ description: pc.localDescription }) } catch (e) { console.error(e) } finally { makingOffer = false pc.onicecandidate = ({ candidate }) => { send({ candidate }) async function receive ({ description, candidate }) { try { if (description) { const readyForOffer = !makingOffer && (pc.signalingState === 'stable' || settingAnswer) const collision = description.type === 'offer' && !readyForOffer ignoreOffer = !polite && collision if (ignoreOffer) return try { settingAnswer = description.type === 'answer' await pc.setRemoteDescription(description) } finally { settingAnswer = false if (description.type === 'offer') { await pc.setLocalDescription() send({ description: pc.localDescription }) } else if (candidate) { try { await pc.addIceCandidate(candidate) } catch (e) { if (!ignoreOffer) throw e } catch (e) { console.error(e) return receive 信令 内置完美协商和信令的完整方案: https://github.com/lukks/like-webrtc (内部使用 webrtc-runtime) 其他 wrtc 包已过时,仍在使用 M81 或 M106。

正文由 探How 从来源站点 RSS 同步,内容未经改写;遇到排版缺失或需要图片、视频时请以原文为准。

打开原文
去社区讨论 →