景元 發表於 2021-2-1 21:49:00

[javascript] 使用javascript实现webrtc视频聊天demo

<p>自己的ID是自动获取的 ,然后输入对方的ID , 对方的ID可以这样获取 , 再打开新的浏览器或者手机也可以&nbsp;</p>
<p>原理是 利用js的h5新特性获取摄像头视频流, 通过peerjs的服务交换双方的信息 , 然后使用浏览器的webrtc特性进行点对点通信 , 这个时候是不需要中间服务器的</p>
<p><img src="https://img2020.cnblogs.com/blog/726254/202102/726254-20210201214522424-1164961918.png" alt="" loading="lazy"></p>
<p>&nbsp;</p>
<p>&nbsp;代码如下:</p>
<div class="cnblogs_code">
<pre>&lt;html&gt;
&lt;head&gt;
    &lt;title&gt;视频聊天&lt;/title&gt;
    &lt;meta charset=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">utf-8</span><span style="color: rgba(128, 0, 0, 1)">"</span>&gt;
    &lt;script src=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">https://cdn.jsdelivr.net/npm/jquery/dist/jquery.min.js</span><span style="color: rgba(128, 0, 0, 1)">"</span>&gt;&lt;/script&gt;
    &lt;script src=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">https://cdn.bootcdn.net/ajax/libs/peerjs/1.3.1/peerjs.min.js</span><span style="color: rgba(128, 0, 0, 1)">"</span>&gt;&lt;/script&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;h3&gt;本地视频&lt;/h3&gt;
&lt;video id=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">localVideo</span><span style="color: rgba(128, 0, 0, 1)">"</span> style=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">width: 400px;height: 300px;</span><span style="color: rgba(128, 0, 0, 1)">"</span>&gt;&lt;/video&gt;
&lt;div style=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">text-align: left</span><span style="color: rgba(128, 0, 0, 1)">"</span>&gt;<span style="color: rgba(0, 0, 0, 1)">
    自己ID</span>&lt;input type=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">text</span><span style="color: rgba(128, 0, 0, 1)">"</span> id=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">myPeerid</span><span style="color: rgba(128, 0, 0, 1)">"</span>/&gt;<span style="color: rgba(0, 0, 0, 1)">(自动获取)
    对方ID</span>&lt;input type=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">text</span><span style="color: rgba(128, 0, 0, 1)">"</span> id=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">youPeerid</span><span style="color: rgba(128, 0, 0, 1)">"</span>/&gt;<span style="color: rgba(0, 0, 0, 1)">(请手动输入)
    </span>&lt;button id=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">callBtn</span><span style="color: rgba(128, 0, 0, 1)">"</span>&gt;聊天&lt;/button&gt;
&lt;/div&gt;
&lt;h3&gt;远程视频&lt;/h3&gt;
&lt;video id=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">remoteVideo</span><span style="color: rgba(128, 0, 0, 1)">"</span> style=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">width: 400px;height: 300px;</span><span style="color: rgba(128, 0, 0, 1)">"</span>&gt;&lt;/video&gt;



&lt;script type=<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">text/javascript</span><span style="color: rgba(128, 0, 0, 1)">"</span>&gt;
    <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">访问用户媒体设备的兼容方法</span>
<span style="color: rgba(0, 0, 0, 1)">    function getUserMedia(constrains,success,error){
      </span><span style="color: rgba(0, 0, 255, 1)">if</span><span style="color: rgba(0, 0, 0, 1)">(navigator.mediaDevices.getUserMedia){
            </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">最新标准API</span>
            navigator.mediaDevices.getUserMedia(constrains).then(success).<span style="color: rgba(0, 0, 255, 1)">catch</span><span style="color: rgba(0, 0, 0, 1)">(error);
      } </span><span style="color: rgba(0, 0, 255, 1)">else</span> <span style="color: rgba(0, 0, 255, 1)">if</span><span style="color: rgba(0, 0, 0, 1)"> (navigator.webkitGetUserMedia){
            </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">webkit内核浏览器</span>
            navigator.webkitGetUserMedia(constrains).then(success).<span style="color: rgba(0, 0, 255, 1)">catch</span><span style="color: rgba(0, 0, 0, 1)">(error);
      } </span><span style="color: rgba(0, 0, 255, 1)">else</span> <span style="color: rgba(0, 0, 255, 1)">if</span><span style="color: rgba(0, 0, 0, 1)"> (navigator.mozGetUserMedia){
            </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">Firefox浏览器</span>
            navagator.mozGetUserMedia(constrains).then(success).<span style="color: rgba(0, 0, 255, 1)">catch</span><span style="color: rgba(0, 0, 0, 1)">(error);
      } </span><span style="color: rgba(0, 0, 255, 1)">else</span> <span style="color: rgba(0, 0, 255, 1)">if</span><span style="color: rgba(0, 0, 0, 1)"> (navigator.getUserMedia){
            </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">旧版API</span>
            navigator.getUserMedia(constrains).then(success).<span style="color: rgba(0, 0, 255, 1)">catch</span><span style="color: rgba(0, 0, 0, 1)">(error);
      }
    }
    </span><span style="color: rgba(0, 0, 255, 1)">var</span> localVideo = document.querySelector(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">video#localVideo</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">);
    </span><span style="color: rgba(0, 0, 255, 1)">var</span> remoteVideo = document.querySelector(<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">video#remoteVideo</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">);
    </span><span style="color: rgba(0, 0, 255, 1)">var</span> localStream=<span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">;
    $(function(){
      getUserMedia({video: </span><span style="color: rgba(0, 0, 255, 1)">true</span>, audio: <span style="color: rgba(0, 0, 255, 1)">true</span><span style="color: rgba(0, 0, 0, 1)">}, function(stream) {
            localStream</span>=<span style="color: rgba(0, 0, 0, 1)">stream;
            localVideo.srcObject </span>=<span style="color: rgba(0, 0, 0, 1)"> stream;
            localVideo.autoplay </span>= <span style="color: rgba(0, 0, 255, 1)">true</span><span style="color: rgba(0, 0, 0, 1)">;
            localVideo.play();
      }, function(err) {
            console.log(</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">Failed to get local stream</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)"> ,err);
      });

      </span><span style="color: rgba(0, 0, 255, 1)">var</span> peer = <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> Peer();
      peer.on(</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">open</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">, function(id) {
            $(</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">#myPeerid</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">).val(id);
      });
      peer.on(</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">call</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">, function(call) {
            call.answer(localStream);
            call.on(</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">stream</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">, function(remoteStream) {
                console.log(remoteStream);
                remoteVideo.srcObject </span>=<span style="color: rgba(0, 0, 0, 1)"> remoteStream;
                remoteVideo.autoplay </span>= <span style="color: rgba(0, 0, 255, 1)">true</span><span style="color: rgba(0, 0, 0, 1)">;
            });
      });
      $(</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">#callBtn</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">).click(function(){
            </span><span style="color: rgba(0, 0, 255, 1)">var</span> remoteId=$(<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">#youPeerid</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">).val();
            </span><span style="color: rgba(0, 0, 255, 1)">if</span>(remoteId==<span style="color: rgba(128, 0, 0, 1)">""</span><span style="color: rgba(0, 0, 0, 1)">){
                alert(</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">请输入对方ID</span><span style="color: rgba(128, 0, 0, 1)">"</span>);<span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)">;
            }

            </span><span style="color: rgba(0, 0, 255, 1)">var</span> call =<span style="color: rgba(0, 0, 0, 1)"> peer.call(remoteId,localStream);
            call.on(</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">stream</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">, function(remoteStream) {
                console.log(remoteStream);
                remoteVideo.srcObject </span>=<span style="color: rgba(0, 0, 0, 1)"> remoteStream;
                remoteVideo.autoplay </span>= <span style="color: rgba(0, 0, 255, 1)">true</span><span style="color: rgba(0, 0, 0, 1)">;
            });
            call.on(</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">close</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">, function() {
                console.log(</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">call close</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">);
                _this.loading.close();
            });
            call.on(</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">error</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">, function(err) {
                console.log(err);
                _this.loading.close();
            });
      });
    });
</span>&lt;/script&gt;
&lt;/body&gt;
&lt;/html&gt;</pre>
</div>
<p>&nbsp;</p>

</div>
<div id="MySignature" role="contentinfo">
    <h3>十年开发经验程序员,离职全心创业中,历时三年开发出的产品《唯一客服系统》</h3>
<div> 一款基于Golang+Vue开发的在线客服系统,软件著作权编号:2021SR1462600。一套可私有化部署的网站在线客服系统,编译后的二进制文件可直接使用无需搭开发环境,下载zip解压即可,仅依赖MySQL数据库,是一个开箱即用的全渠道在线客服系统,致力于帮助广大开发者/公司快速部署整合私有化客服功能。 </div>
<div> 开源地址:唯一客服(开源学习版) </div>
<div> 官网地址:唯一客服官网 </div><br><br>
来源:https://www.cnblogs.com/taoshihan/p/14359243.html
頁: [1]
查看完整版本: [javascript] 使用javascript实现webrtc视频聊天demo