当前位置: 首页 > news >正文

网站开发所需的技术神童预言新冠2023结束

网站开发所需的技术,神童预言新冠2023结束,浙江做铁塔的公司网站,域名防红短链接生成首先需要一个信令服务器,我们使用nodejs来搭建。两个端:发送端和接收端。我的目录结构如下图:流程 创建一个文件夹 WebRTC-Test。进入文件夹中,新建一个node的文件夹。使用终端并进入node的目录下,使用 npm init 创建p…
  1. 首先需要一个信令服务器,我们使用nodejs来搭建。两个端:发送端和接收端。
  2. 我的目录结构如下图:
  3. 流程
    1. 创建一个文件夹 WebRTC-Test。
    2. 进入文件夹中,新建一个node的文件夹。
    3. 使用终端并进入node的目录下,使用
      npm init

      创建package.json。

    4. 新建server.js,复制一下代码
      const app = require('express')();
      const wsInstance = require('express-ws')(app);const cors = require('cors');
      app.use(cors({ origin: 'http://localhost:3000' }));app.ws('/', ws => {ws.on('message', data => {// 未做业务处理,收到消息后直接广播wsInstance.getWss().clients.forEach(server => {if (server !== ws) {server.send(data);console.log(data,)}});});
      });console.log("服务启动: http://localhost:8080");
      app.listen(8080, '0.0.0.0');
    5. 下载信令服务器的依赖。
      npm install express;
      npm install express-ws;
      npm install cors;
    6. 使用node server.js启动node的服务。
    7. 准备接收方的代码(receive.html)。
      <!DOCTYPE html>
      <html lang="zh-CN"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0"><meta http-equiv="Content-Security-Policy" content="upgrade-insecure-requests"><title>Receiver</title>
      </head><body><video autoplay id="remote"></video>
      </body>
      <script>const remoteVideo = document.querySelector('#remote')const socket = new WebSocket('ws://localhost:8080');socket.onopen = function () {console.log("Socket Success")}let buddy = new RTCPeerConnection()// 如果接收到对方的视频socket.onmessage = function (e) {const { type, sdp, iceCandidate } = JSON.parse(e.data)console.log(type)switch (type) {case "offer":buddy.setRemoteDescription(new RTCSessionDescription({ type, sdp }))buddy.createAnswer().then(answer => {buddy.setLocalDescription(answer)socket.send(JSON.stringify(answer))})break;case "offer_ice":buddy.addIceCandidate(iceCandidate)break;default:break;}}buddy.ontrack = function (e) {remote.srcObject = e.streams[0]}buddy.onicecandidate = function (e) {if (e.candidate) {socket.send(JSON.stringify({type: "answer_ice",iceCandidate: e.candidate}))}}</script></html>
    8. 准备发送方的代码(send.html)。
      <!DOCTYPE html>
      <html lang="zh-CN"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0"><meta http-equiv="Content-Security-Policy" content="upgrade-insecure-requests"><title>Send</title>
      </head><body><script>const socket = new WebSocket('ws://localhost:8080');socket.onopen = function () {console.log("Socket Success")}let peer = new RTCPeerConnection()navigator.mediaDevices.getUserMedia({video: true,audio: true}).then(stream => {stream.getTracks().forEach(track => {peer.addTrack(track, stream);})peer.createOffer().then(offer => {peer.setLocalDescription(offer);socket.send(JSON.stringify(offer));})})peer.onicecandidate = function (e) {if (e.candidate) {socket.send(JSON.stringify({type: "offer_ice",iceCandidate: e.candidate}))}}// 如果接收到对方的视频socket.onmessage = function (e) {const { type, sdp, iceCandidate } = JSON.parse(e.data)console.log(type)switch (type) {case "answer":peer.setRemoteDescription(new RTCSessionDescription({ type, sdp }))break;case "answer_ice":peer.addIceCandidate(iceCandidate)break;default:break;}}</script>
      </body></html>
    9. 使用vscode的插件live server启动两个html文件。
      下载live server插件。

      启动live server。

      启动后两个端口号。


    10. 等全部启动后,查看接收端的页面,要等一段时间,可以看到摄像头拍到的画面。
      接受端播放不了,在浏览器控制台中输入 remoteVideo.play() 就行
       

http://www.yidumall.com/news/23420.html

相关文章:

  • 东莞市建设管理局徐州百度seo排名
  • 东易日盛装饰公司电话号码和地址泉州网站seo外包公司
  • python可以做的网站论文查询网站备案信息
  • 淘宝网站做多久seo公司是什么意思
  • 呼市做开发网站的公司排名优化软件点击
  • 宿州公司做网站免费发帖推广平台有哪些
  • 可以做淘宝客的网站快速优化seo软件推广方法
  • 深圳公司注册办理流程win7优化大师
  • 网站建设岗位有哪些网络工程师
  • 苏州沧浪做网站哪家好seo关键词挖掘
  • 如何设计大型电商网站建设百度云网盘网页版登录
  • 汽车网站网页模板深圳企业seo
  • 建设安全协会网站惠州网络推广
  • 企业大型网站开发网站模板设计海外seo网站推广
  • 广州建设网站是什么关系nba在线直播免费观看直播
  • 网站上怎么做星星评分汽车推广软文
  • 攀枝花网站怎么做seo推广引流app
  • 公安网站备案号查询网络广告的计费方式
  • 网站的做网站公司哪家好最新天气预报最新消息
  • 网站优化工具网站优化塔山双喜
  • 河北建基官网优化服务公司
  • 广州公认的第一富人区网站建设及推广优化
  • <>chatgpt入口
  • 响应式网站好处网站建成后应该如何推广
  • 网站如何做流媒体上海十大公关公司排名
  • 桐乡哪里有做网站的网络营销百度百科
  • 党建网站建设技术方案百度快速收录3元一条
  • wordpress+评论网站排名优化公司
  • 重庆科技建设信息网站危机公关处理
  • 东莞网约车驾驶员资格证网上报名智推教育seo课程