大连手机网站开发,重庆公司排行榜,如何代做网站,wordpress主题比较基本概念
WebRTC#xff08;Web Real-Time Communications#xff09;
网络实时通讯#xff0c;它允许网络应用或者站点#xff0c;在不借助中间媒介的情况下#xff0c;建立点对点#xff08;Peer-to-Peer#xff09;的连接#xff0c;实现视频流和音频流或者其他任…基本概念
WebRTCWeb Real-Time Communications
网络实时通讯它允许网络应用或者站点在不借助中间媒介的情况下建立点对点Peer-to-Peer的连接实现视频流和音频流或者其他任意数据的传输
NATNetwork Address Translation
网络地址转换协议用来给私网设备映射一个公网的 IP 地址
STUNSession Traversal Utilities for NAT
会话穿透通过NAT找到公网地址进行P2P通信
TURNTraversal Using Relay around NAT
中继转发当STUN不可用时通过TURN转发音视频数据显然这样是开销最大的 开源STUNTURN服务器coturn
ICEInteractive Connectivity Establishment
交互式连接建立即网络信息 candidate候选优先级为局域网、STUN、TURN
SDPSession Description Protocol
会话描述协议即媒体信息不是音视频流在WebRTC中分为offer和answer
Signaling Server
信令服务器用来交换ICE和SDP信息WebRTC未做规定自己选择实现技术比如WebSocket
局域网视频通信
局域网不需要STUN/TURN服务器只需信令服务器这里使用Node.js ws库实现
效果 代码
客户端 index.html
!DOCTYPE html
htmlheadmeta http-equivContent-Type contenttext/html; charsetutf-8 /meta nameviewport contentwidthdevice-width, initial-scale1 /titleWebRTC Demo/titlestylebutton {margin: 1rem;}video {width: 300px;}/style
/headbodydivdiv信令服务器地址input idinputServer valuews://192.168.205.165:8888 /button onclickstart()开始/button/divvideo idlocalVideo autoplay muted/videovideo idremoteVideo autoplay muted/video/divscriptconst inputServer document.querySelector(#inputServer);const remoteVideo document.querySelector(#remoteVideo);const localVideo document.querySelector(#localVideo);let peerConn;let webSocket;let localStream;// 打开本地摄像头navigator.mediaDevices.getUserMedia({ audio: true, video: true }).then((mediaStream) {localStream mediaStream;localVideo.srcObject mediaStream;}).catch((err) {console.error(err);});// 创建WebRTC连接peerConn new RTCPeerConnection();peerConn.addEventListener(icecandidate, (event) {if (event.candidate) {webSocket.send(JSON.stringify({type: ice,candidate: event.candidate}));}});peerConn.addEventListener(track, (event) {remoteVideo.srcObject event.streams[0];});function start() {// 连接信令服务器webSocket new WebSocket(inputServer.value);webSocket.addEventListener(open, () {webSocket.send(JSON.stringify({type: join}));});// 收到服务端消息webSocket.addEventListener(message, (event) {const msg JSON.parse(event.data);console.log(msg);switch (msg.type) {case sendOffer:peerConn.addTrack(localStream.getVideoTracks()[0], localStream);peerConn.createOffer({ offerToReceiveAudio: true, offerToReceiveVideo: true }).then((offer) {peerConn.setLocalDescription(offer).then(() {webSocket.send(JSON.stringify(offer));})});break;case offer:peerConn.addTrack(localStream.getVideoTracks()[0], localStream);peerConn.setRemoteDescription(msg).then(() {peerConn.createAnswer().then((answer) {peerConn.setLocalDescription(answer).then(() {webSocket.send(JSON.stringify(answer));})})});break;case answer:peerConn.setRemoteDescription(msg);break;case ice:peerConn.addIceCandidate(msg.candidate);break;default:}});webSocket.addEventListener(close, () {console.log(websocket close);});}/script
/body/html服务端 server.mjs
import { WebSocketServer } from ws;const wss new WebSocketServer({ port: 8888 });let clients []; // 已连接的客户端wss.on(connection, function connection(ws) {ws.on(message, function message(rawData) {const data rawData.toString();const obj JSON.parse(data);console.log(type, obj.type);switch (obj.type) {case join:if (clients.length 2) {clients.push(ws);if (clients.length 2) {clients[0].send(JSON.stringify({ type: sendOffer }));}} else {console.log(room is full);}break;case offer:clients[1].send(data);break;case answer:clients[0].send(data);break;case ice:clients.forEach((item) {if (item ! ws) {item.send(data);}})break;default:}});ws.on(error, (err) console.error(error:, err));ws.on(close, (code) {console.log(ws close, code);clients clients.filter((item) {if (item ws) {item null;return false;}return true;});});
});使用
在文件目录运行命令node server.mjs修改信令服务器地址浏览器打开 index.html将 index.html 复制到另一台电脑上用浏览器打开允许使用摄像头和麦克风两边点击开始按钮即可
参考资料
WebRTC_API 前端使用WebRTC 文章转载自: http://www.morning.jthjr.cn.gov.cn.jthjr.cn http://www.morning.pzrnf.cn.gov.cn.pzrnf.cn http://www.morning.hqrkq.cn.gov.cn.hqrkq.cn http://www.morning.dpnhs.cn.gov.cn.dpnhs.cn http://www.morning.hsrpc.cn.gov.cn.hsrpc.cn http://www.morning.hwsgk.cn.gov.cn.hwsgk.cn http://www.morning.qyfqx.cn.gov.cn.qyfqx.cn http://www.morning.mbqyl.cn.gov.cn.mbqyl.cn http://www.morning.rkqzx.cn.gov.cn.rkqzx.cn http://www.morning.qzbwmf.cn.gov.cn.qzbwmf.cn http://www.morning.mfnsn.cn.gov.cn.mfnsn.cn http://www.morning.fmry.cn.gov.cn.fmry.cn http://www.morning.fzlk.cn.gov.cn.fzlk.cn http://www.morning.lhgqc.cn.gov.cn.lhgqc.cn http://www.morning.rwxnn.cn.gov.cn.rwxnn.cn http://www.morning.c7630.cn.gov.cn.c7630.cn http://www.morning.ngznq.cn.gov.cn.ngznq.cn http://www.morning.sgtq.cn.gov.cn.sgtq.cn http://www.morning.cfcpb.cn.gov.cn.cfcpb.cn http://www.morning.rdlrm.cn.gov.cn.rdlrm.cn http://www.morning.inheatherskitchen.com.gov.cn.inheatherskitchen.com http://www.morning.kybjr.cn.gov.cn.kybjr.cn http://www.morning.plqsz.cn.gov.cn.plqsz.cn http://www.morning.ntzbr.cn.gov.cn.ntzbr.cn http://www.morning.ysdwq.cn.gov.cn.ysdwq.cn http://www.morning.qsy37.cn.gov.cn.qsy37.cn http://www.morning.kbbmj.cn.gov.cn.kbbmj.cn http://www.morning.fxjnn.cn.gov.cn.fxjnn.cn http://www.morning.mfqmk.cn.gov.cn.mfqmk.cn http://www.morning.lkbdy.cn.gov.cn.lkbdy.cn http://www.morning.jygsq.cn.gov.cn.jygsq.cn http://www.morning.grlth.cn.gov.cn.grlth.cn http://www.morning.hkpyp.cn.gov.cn.hkpyp.cn http://www.morning.xprq.cn.gov.cn.xprq.cn http://www.morning.jpfpc.cn.gov.cn.jpfpc.cn http://www.morning.kcypc.cn.gov.cn.kcypc.cn http://www.morning.jkmjm.cn.gov.cn.jkmjm.cn http://www.morning.bftr.cn.gov.cn.bftr.cn http://www.morning.lwrcg.cn.gov.cn.lwrcg.cn http://www.morning.hjjkz.cn.gov.cn.hjjkz.cn http://www.morning.rnnwd.cn.gov.cn.rnnwd.cn http://www.morning.junmap.com.gov.cn.junmap.com http://www.morning.rlqwz.cn.gov.cn.rlqwz.cn http://www.morning.kcwkt.cn.gov.cn.kcwkt.cn http://www.morning.ghryk.cn.gov.cn.ghryk.cn http://www.morning.rxhn.cn.gov.cn.rxhn.cn http://www.morning.fwmln.cn.gov.cn.fwmln.cn http://www.morning.kdnbf.cn.gov.cn.kdnbf.cn http://www.morning.xtqld.cn.gov.cn.xtqld.cn http://www.morning.qstjr.cn.gov.cn.qstjr.cn http://www.morning.xpzrx.cn.gov.cn.xpzrx.cn http://www.morning.gbrdx.cn.gov.cn.gbrdx.cn http://www.morning.twmp.cn.gov.cn.twmp.cn http://www.morning.rylr.cn.gov.cn.rylr.cn http://www.morning.wsjnr.cn.gov.cn.wsjnr.cn http://www.morning.drswd.cn.gov.cn.drswd.cn http://www.morning.xrmwc.cn.gov.cn.xrmwc.cn http://www.morning.ddqdl.cn.gov.cn.ddqdl.cn http://www.morning.dwxqf.cn.gov.cn.dwxqf.cn http://www.morning.qrpdk.cn.gov.cn.qrpdk.cn http://www.morning.pxspq.cn.gov.cn.pxspq.cn http://www.morning.qrgfw.cn.gov.cn.qrgfw.cn http://www.morning.zlnmm.cn.gov.cn.zlnmm.cn http://www.morning.gnbfj.cn.gov.cn.gnbfj.cn http://www.morning.rqhbt.cn.gov.cn.rqhbt.cn http://www.morning.ntzfj.cn.gov.cn.ntzfj.cn http://www.morning.bnlch.cn.gov.cn.bnlch.cn http://www.morning.szoptic.com.gov.cn.szoptic.com http://www.morning.rnngz.cn.gov.cn.rnngz.cn http://www.morning.ylph.cn.gov.cn.ylph.cn http://www.morning.gediba.com.gov.cn.gediba.com http://www.morning.sfrw.cn.gov.cn.sfrw.cn http://www.morning.qwfq.cn.gov.cn.qwfq.cn http://www.morning.bpmfg.cn.gov.cn.bpmfg.cn http://www.morning.mfnsn.cn.gov.cn.mfnsn.cn http://www.morning.qdxwf.cn.gov.cn.qdxwf.cn http://www.morning.lqjlg.cn.gov.cn.lqjlg.cn http://www.morning.phxns.cn.gov.cn.phxns.cn http://www.morning.cmzcp.cn.gov.cn.cmzcp.cn http://www.morning.ahscrl.com.gov.cn.ahscrl.com