{"id":24587,"date":"2024-11-21T10:06:23","date_gmt":"2024-11-21T02:06:23","guid":{"rendered":"https:\/\/fwq.ai\/blog\/24587\/"},"modified":"2024-11-21T10:06:23","modified_gmt":"2024-11-21T02:06:23","slug":"%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8webman%e6%8a%80%e6%9c%af%e6%9e%84%e5%bb%ba%e5%9c%a8%e7%ba%bf%e8%a7%86%e9%a2%91%e4%bc%9a%e8%ae%ae%e7%b3%bb%e7%bb%9f","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/24587\/","title":{"rendered":"\u5982\u4f55\u4f7f\u7528WebMan\u6280\u672f\u6784\u5efa\u5728\u7ebf\u89c6\u9891\u4f1a\u8bae\u7cfb\u7edf"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/000\/887\/227\/169311098496099.jpg\" class=\"aligncenter\" title=\"\u5982\u4f55\u4f7f\u7528WebMan\u6280\u672f\u6784\u5efa\u5728\u7ebf\u89c6\u9891\u4f1a\u8bae\u7cfb\u7edf\u63d2\u56fe\" alt=\"\u5982\u4f55\u4f7f\u7528WebMan\u6280\u672f\u6784\u5efa\u5728\u7ebf\u89c6\u9891\u4f1a\u8bae\u7cfb\u7edf\u63d2\u56fe\" \/><\/p>\n<p>\u5982\u4f55\u4f7f\u7528WebRTC\u6280\u672f\u6784\u5efa\u5728\u7ebf\u89c6\u9891\u4f1a\u8bae\u7cfb\u7edf<\/p>\n<p>\u968f\u7740\u73b0\u4ee3\u79d1\u6280\u7684\u53d1\u5c55\uff0c\u8d8a\u6765\u8d8a\u591a\u7684\u4eba\u9009\u62e9\u5728\u7f51\u7edc\u4e0a\u8fdb\u884c\u89c6\u9891\u4f1a\u8bae\uff0c\u65e0\u8bba\u662f\u5546\u52a1\u4f1a\u8bae\u3001\u6559\u80b2\u6559\u5b66\u8fd8\u662f\u8fdc\u7a0b\u533b\u7597\uff0c\u90fd\u53ef\u4ee5\u901a\u8fc7\u5728\u7ebf\u89c6\u9891\u4f1a\u8bae\u7cfb\u7edf\u6765\u5b9e\u73b0\u3002\u5728\u6784\u5efa\u8fd9\u6837\u4e00\u4e2a\u7cfb\u7edf\u65f6\uff0c\u6211\u4eec\u53ef\u4ee5\u5229\u7528WebRTC\uff08Web Real-time Communication\uff09\u6280\u672f\uff0c\u5b83\u662f\u4e00\u79cd\u57fa\u4e8eWeb\u7684\u5373\u65f6\u901a\u8baf\u6280\u672f\uff0c\u53ef\u4ee5\u5728\u6d4f\u89c8\u5668\u4e4b\u95f4\u5b9e\u73b0\u97f3\u9891\u3001\u89c6\u9891\u548c\u6570\u636e\u7684\u5b9e\u65f6\u901a\u4fe1\u3002<\/p>\n<p>\u672c\u6587\u5c06\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528WebRTC\u6280\u672f\u6765\u642d\u5efa\u4e00\u4e2a\u7b80\u5355\u7684\u5728\u7ebf\u89c6\u9891\u4f1a\u8bae\u7cfb\u7edf\uff0c\u4ee5\u4e0b\u662f\u5177\u4f53\u6b65\u9aa4\uff1a<\/p>\n<ol>\n<li>\u786e\u4fdd\u6240\u4f7f\u7528\u7684\u6d4f\u89c8\u5668\u652f\u6301WebRTC\u6280\u672f\uff0c\u76ee\u524d\u5927\u90e8\u5206\u4e3b\u6d41\u6d4f\u89c8\u5668\u90fd\u5df2\u7ecf\u652f\u6301\u4e86WebRTC\u3002<\/li>\n<li>\u642d\u5efa\u4e00\u4e2a\u57fa\u672c\u7684Web\u670d\u52a1\u5668\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528Node.js\u6765\u642d\u5efa\u4e00\u4e2a\u7b80\u5355\u7684\u670d\u52a1\u5668\u3002\u521b\u5efa\u4e00\u4e2a\u540d\u4e3aserver.js\u7684\u6587\u4ef6\uff0c\u5e76\u8f93\u5165\u4ee5\u4e0b\u4ee3\u7801\uff1a<\/li>\n<\/ol>\n<pre>const express = require('express');\nconst app = express();\n\napp.use(express.static('public'));\n\nconst server = app.listen(3000, function() {\n  console.log('Server running on port 3000');\n});<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<ol>\n<li>\u5728\u670d\u52a1\u5668\u6587\u4ef6\u5939\u4e0b\u521b\u5efa\u4e00\u4e2a\u540d\u4e3apublic\u7684\u6587\u4ef6\u5939\uff0c\u5e76\u5728\u8be5\u6587\u4ef6\u5939\u4e0b\u521b\u5efa\u4e00\u4e2aindex.html\u6587\u4ef6\u3002\u5728index.html\u6587\u4ef6\u4e2d\u8f93\u5165\u4ee5\u4e0b\u4ee3\u7801\uff1a<\/li>\n<\/ol>\n<pre>\n\n\n  &lt;title&gt;WebRTC Video Conference&lt;\/title&gt;&lt;script src=\"https:\/\/webrtc.github.io\/adapter\/adapter-latest.js\"&gt;&lt;\/script&gt;&lt;h1&gt;WebRTC Video Conference&lt;\/h1&gt;\n  &lt;video id=\"localVideo\" autoplay&gt;&lt;\/video&gt;&lt;video id=\"remoteVideo\" autoplay&gt;&lt;\/video&gt;&lt;script src=\"script.js\"&gt;&lt;\/script&gt;<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<ol>\n<li>\u5728public\u6587\u4ef6\u5939\u4e0b\u521b\u5efa\u4e00\u4e2a\u540d\u4e3ascript.js\u7684\u6587\u4ef6\uff0c\u5e76\u5728\u8be5\u6587\u4ef6\u4e2d\u8f93\u5165\u4ee5\u4e0b\u4ee3\u7801\uff1a<\/li>\n<\/ol>\n<pre>const localVideo = document.getElementById('localVideo');\nconst remoteVideo = document.getElementById('remoteVideo');\n\nnavigator.mediaDevices.getUserMedia({ video: true, audio: true })\n  .then(function(stream) {\n    localVideo.srcObject = stream;\n  })\n  .catch(function(error) {\n    console.error('Error accessing media devices:', error);\n  });\n\nconst configuration = {\n  iceServers: [\n    { urls: 'stun:stun.l.google.com:19302' },\n    { urls: 'stun:stun1.l.google.com:19302' },\n  ],\n};\n\nconst peerConnection = new RTCPeerConnection(configuration);\n\npeerConnection.addEventListener('track', function(event) {\n  remoteVideo.srcObject = event.streams[0];\n});\n\npeerConnection.addEventListener('icecandidate', function(event) {\n  if (event.candidate) {\n    sendToServer({ type: 'icecandidate', candidate: event.candidate });\n  }\n});\n\nfunction sendToServer(message) {\n  \/\/ Send the message to the server using WebSocket or AJAX\n}\n\nfunction receiveFromServer(message) {\n  \/\/ Receive the message from the server using WebSocket or AJAX\n}\n\nreceiveFromServer({ type: 'offer', offer: \/* Offer SDP *\/ });\n\nfunction setRemoteDescription(message) {\n  peerConnection.setRemoteDescription(new RTCSessionDescription(message.offer))\n    .then(function() {\n      return peerConnection.createAnswer();\n    })\n    .then(function(answer) {\n      return peerConnection.setLocalDescription(answer);\n    })\n    .then(function() {\n      sendToServer({ type: 'answer', answer: peerConnection.localDescription });\n    })\n    .catch(function(error) {\n      console.error('Error setting remote description:', error);\n    });\n}\n\nfunction addIceCandidate(message) {\n  peerConnection.addIceCandidate(new RTCIceCandidate(message.candidate))\n    .catch(function(error) {\n      console.error('Error adding ICE candidate:', error);\n    });\n}<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<ol>\n<li>\u5728script.js\u6587\u4ef6\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u4e86getUserMedia\u65b9\u6cd5\u6765\u83b7\u53d6\u672c\u5730\u5a92\u4f53\u6d41\uff08\u5305\u62ec\u89c6\u9891\u548c\u97f3\u9891\uff09\uff0c\u7136\u540e\u5c06\u5176\u5c55\u793a\u5728\u9875\u9762\u4e2d\u7684localVideo\u5143\u7d20\u4e0a\u3002<\/li>\n<li>\u6211\u4eec\u8fd8\u9700\u8981\u8fdb\u884cPeerConnection\u7684\u521d\u59cb\u5316\u548c\u8bbe\u7f6e\u3002\u5176\u4e2d\uff0cconfiguration\u662f\u4e00\u4e2a\u5305\u542bSTUN\u670d\u52a1\u5668\u5730\u5740\u7684\u914d\u7f6e\u5bf9\u8c61\u3002peerConnection.addEventListener(&#8216;track&#8217;, &#8230;)\u548cpeerConnection.addEventListener(&#8216;icecandidate&#8217;, &#8230;)\u662f\u4e00\u4e9b\u4e8b\u4ef6\u76d1\u542c\u5668\uff0c\u7528\u4e8e\u63a5\u6536\u8fdc\u7a0b\u5a92\u4f53\u6d41\u548cICE\u5019\u9009\u7684\u4e8b\u4ef6\u3002<\/li>\n<li>\u5728sendToServer\u548creceiveFromServer\u51fd\u6570\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528WebSocket\u6216\u8005AJAX\u6765\u4e0e\u670d\u52a1\u5668\u8fdb\u884c\u5b9e\u65f6\u7684\u901a\u4fe1\u3002<\/li>\n<li>\u6700\u540e\uff0c\u6211\u4eec\u9700\u8981\u6839\u636e\u670d\u52a1\u7aef\u53d1\u9001\u8fc7\u6765\u7684offer SDP\u521b\u5efa\u4e00\u4e2a\u4f1a\u8bdd\u63cf\u8ff0\u7b26\uff0c\u5e76\u5c06\u5176\u8bbe\u7f6e\u4e3a\u8fdc\u7a0b\u63cf\u8ff0\u7b26\uff0c\u7136\u540e\u6839\u636e\u8fdc\u7a0b\u63cf\u8ff0\u7b26\u521b\u5efa\u4e00\u4e2aanswer SDP\uff0c\u5e76\u5c06\u5176\u8bbe\u7f6e\u4e3a\u672c\u5730\u63cf\u8ff0\u7b26\uff0c\u5e76\u901a\u8fc7sendToServer\u51fd\u6570\u5c06\u5176\u53d1\u9001\u7ed9\u670d\u52a1\u5668\u3002\u5f53\u7136\uff0c\u5728\u8fd9\u91cc\u8fd8\u8981\u5904\u7406\u4e0eICE\u5019\u9009\u76f8\u5173\u7684\u64cd\u4f5c\u3002<\/li>\n<\/ol>\n<p>\u901a\u8fc7\u4ee5\u4e0a\u6b65\u9aa4\uff0c\u6211\u4eec\u5c31\u6210\u529f\u5730\u4f7f\u7528WebRTC\u6280\u672f\u6784\u5efa\u4e86\u4e00\u4e2a\u7b80\u5355\u7684\u5728\u7ebf\u89c6\u9891\u4f1a\u8bae\u7cfb\u7edf\u3002\u5f53\u7528\u6237\u6253\u5f00\u7f51\u9875\u65f6\uff0c\u4f1a\u81ea\u52a8\u83b7\u53d6\u672c\u5730\u6444\u50cf\u5934\u548c\u9ea6\u514b\u98ce\u7684\u5a92\u4f53\u6d41\uff0c\u5e76\u5728\u9875\u9762\u4e2d\u5c55\u793a\u51fa\u6765\u3002\u540c\u65f6\uff0c\u5b83\u4e5f\u5177\u5907\u4e86\u5b9e\u65f6\u901a\u4fe1\u7684\u80fd\u529b\uff0c\u53ef\u4ee5\u8fdb\u884c\u8fdc\u7a0b\u89c6\u9891\u7684\u5448\u73b0\uff0c\u5b9e\u73b0\u53cc\u5411\u7684\u89c6\u9891\u4f1a\u8bae\u529f\u80fd\u3002<\/p>\n<p>\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u6b64\u5904\u7684\u793a\u4f8b\u4ee3\u7801\u53ea\u662f\u4e00\u4e2a\u57fa\u7840\u7684\u6846\u67b6\uff0c\u5b9e\u9645\u5e94\u7528\u4e2d\u8fd8\u9700\u8981\u8fdb\u4e00\u6b65\u7684\u529f\u80fd\u548c\u4f18\u5316\u3002\u540c\u65f6\uff0c\u4e3a\u4e86\u5b9e\u73b0\u66f4\u597d\u7684\u7528\u6237\u4f53\u9a8c\u548c\u5b89\u5168\u6027\uff0c\u8fd8\u9700\u8fdb\u4e00\u6b65\u5f00\u53d1\u548c\u4f18\u5316\u7cfb\u7edf\u7684\u754c\u9762\u3001\u7528\u6237\u8ba4\u8bc1\u3001\u670d\u52a1\u5668\u7aef\u4ee3\u7801\u7b49\u3002<\/p>\n<p>\u5e0c\u671b\u672c\u6587\u5bf9\u4f60\u7406\u89e3\u5982\u4f55\u4f7f\u7528WebRTC\u6280\u672f\u6784\u5efa\u5728\u7ebf\u89c6\u9891\u4f1a\u8bae\u7cfb\u7edf\u63d0\u4f9b\u4e86\u4e00\u4e9b\u5e2e\u52a9\uff0c\u5e0c\u671b\u4f60\u53ef\u4ee5\u8fdb\u4e00\u6b65\u7814\u7a76\u548c\u5e94\u7528\u8fd9\u9879\u6280\u672f\uff0c\u6253\u9020\u51fa\u66f4\u52a0\u5b8c\u5584\u548c\u5f3a\u5927\u7684\u5728\u7ebf\u89c6\u9891\u4f1a\u8bae\u7cfb\u7edf\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u5982\u4f55\u4f7f\u7528WebMan\u6280\u672f\u6784\u5efa\u5728\u7ebf\u89c6\u9891\u4f1a\u8bae\u7cfb\u7edf\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5982\u4f55\u4f7f\u7528WebRTC\u6280\u672f\u6784\u5efa\u5728\u7ebf\u89c6\u9891\u4f1a\u8bae\u7cfb\u7edf \u968f\u7740\u73b0\u4ee3\u79d1\u6280\u7684\u53d1\u5c55\uff0c\u8d8a\u6765\u8d8a\u591a\u7684\u4eba\u9009\u62e9\u5728\u7f51\u7edc\u4e0a\u8fdb\u884c\u89c6\u9891\u4f1a\u8bae\uff0c\u65e0\u8bba\u662f\u5546\u52a1\u4f1a\u8bae\u3001\u6559\u80b2\u6559\u5b66\u8fd8\u662f\u8fdc\u7a0b\u533b\u7597\uff0c\u90fd\u53ef\u4ee5\u901a\u8fc7\u5728\u7ebf\u89c6\u9891\u4f1a\u8bae\u7cfb\u7edf\u6765\u5b9e\u73b0\u3002\u5728\u6784\u5efa\u8fd9\u6837\u4e00\u4e2a\u7cfb\u7edf\u65f6\uff0c\u6211\u4eec\u53ef\u4ee5\u5229\u7528WebRTC\uff08Web Real-time Communication\uff09\u6280\u672f\uff0c\u5b83\u662f\u4e00\u79cd\u57fa\u4e8eWeb\u7684\u5373\u65f6\u901a\u8baf\u6280\u672f\uff0c\u53ef\u4ee5\u5728\u6d4f\u89c8\u5668\u4e4b\u95f4\u5b9e\u73b0\u97f3\u9891\u3001\u89c6\u9891\u548c\u6570\u636e\u7684\u5b9e\u65f6\u901a\u4fe1\u3002 \u672c\u6587\u5c06\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528WebRTC\u6280\u672f\u6765\u642d\u5efa\u4e00\u4e2a\u7b80\u5355\u7684\u5728\u7ebf\u89c6\u9891\u4f1a\u8bae\u7cfb\u7edf\uff0c\u4ee5\u4e0b\u662f\u5177\u4f53\u6b65\u9aa4\uff1a \u786e\u4fdd\u6240\u4f7f\u7528\u7684\u6d4f\u89c8\u5668\u652f\u6301WebRTC\u6280\u672f\uff0c\u76ee\u524d\u5927\u90e8\u5206\u4e3b\u6d41\u6d4f\u89c8\u5668\u90fd\u5df2\u7ecf\u652f\u6301\u4e86WebRTC\u3002 \u642d\u5efa\u4e00\u4e2a\u57fa\u672c\u7684Web\u670d\u52a1\u5668\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528Node.js\u6765\u642d\u5efa\u4e00\u4e2a\u7b80\u5355\u7684\u670d\u52a1\u5668\u3002\u521b\u5efa\u4e00\u4e2a\u540d\u4e3aserver.js\u7684\u6587\u4ef6\uff0c\u5e76\u8f93\u5165\u4ee5\u4e0b\u4ee3\u7801\uff1a const express = require(&#8216;express&#8217;); const app = express(); app.use(express.static(&#8216;public&#8217;)); const server = app.listen(3000, function() { console.log(&#8216;Server running on port 3000&#8217;); }); \u767b\u5f55\u540e\u590d\u5236 \u5728\u670d\u52a1\u5668\u6587\u4ef6\u5939\u4e0b\u521b\u5efa\u4e00\u4e2a\u540d\u4e3apublic\u7684\u6587\u4ef6\u5939\uff0c\u5e76\u5728\u8be5\u6587\u4ef6\u5939\u4e0b\u521b\u5efa\u4e00\u4e2aindex.html\u6587\u4ef6\u3002\u5728index.html\u6587\u4ef6\u4e2d\u8f93\u5165\u4ee5\u4e0b\u4ee3\u7801\uff1a &lt;title&gt;WebRTC Video Conference&lt;\/title&gt;&lt;script src=&#8221;https:\/\/webrtc.github.io\/adapter\/adapter-latest.js&#8221;&gt;&lt;\/script&gt;&lt;h1&gt;WebRTC Video Conference&lt;\/h1&gt; &lt;video id=&#8221;localVideo&#8221; autoplay&gt;&lt;\/video&gt;&lt;video id=&#8221;remoteVideo&#8221; autoplay&gt;&lt;\/video&gt;&lt;script src=&#8221;script.js&#8221;&gt;&lt;\/script&gt; \u767b\u5f55\u540e\u590d\u5236 \u5728public\u6587\u4ef6\u5939\u4e0b\u521b\u5efa\u4e00\u4e2a\u540d\u4e3ascript.js\u7684\u6587\u4ef6\uff0c\u5e76\u5728\u8be5\u6587\u4ef6\u4e2d\u8f93\u5165\u4ee5\u4e0b\u4ee3\u7801\uff1a const localVideo = document.getElementById(&#8216;localVideo&#8217;); const remoteVideo = document.getElementById(&#8216;remoteVideo&#8217;); navigator.mediaDevices.getUserMedia({ video: true, [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-24587","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/24587","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=24587"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/24587\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=24587"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=24587"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=24587"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}