Browse Source

Signed-off-by: caner <5658514@qq.com>

caner 3 years ago
parent
commit
33fadc61c6
5 changed files with 125 additions and 11 deletions
  1. 5 5
      contrl.js
  2. 10 6
      index.js
  3. 1 0
      package.json
  4. 104 0
      web/index.html
  5. 5 0
      web/socket.io.min.js

+ 5 - 5
contrl.js

@@ -7,10 +7,10 @@ class ContrlService {
         // 寄存器地址
         this.ADR = 0x48
         // 电位器地址->对应模块in1,in2
-        this.P1 = 0x40
-        this.P2 = 0x41
-        this.P3 = 0x42
-        this.P4 = 0x43
+        this.P1 = 0x41
+        this.P2 = 0x42
+        this.P3 = 0x43
+        this.P4 = 0x44
         this.run()
     }
 
@@ -18,7 +18,7 @@ class ContrlService {
         // 数模初始值
         const data = await this.Orientation()
         process.send(data);
-        await sleep(50)
+        await sleep(500)
         this.run()
     }
 

+ 10 - 6
index.js

@@ -1,7 +1,9 @@
-// socket
+// socket + http
+const { createServer } = require("http");
+const express = require("express");
 const { Server } = require("socket.io");
 const { fork } = require('child_process');
-class ContrlServer {
+class VideoServer {
   constructor() {
     this.Contrl = null
     this.socket = null
@@ -9,7 +11,10 @@ class ContrlServer {
   }
   // socket 服务
   initSoket() {
-    const io = new Server({
+    const app = express();
+    app.use(express.static("./web"));
+    const httpServer = createServer(app);
+    const io = new Server(httpServer, {
       cors: {
         origin: ["*"],
       },
@@ -48,7 +53,7 @@ class ContrlServer {
       // 控制服务
       this.socket = socket
     });
-    io.listen(7896)
+    httpServer.listen(7896);
     console.log("服务开启成功7896");
     this.initContrl()
   }
@@ -58,10 +63,9 @@ class ContrlServer {
     this.Contrl.on('message', (msg) => {
       if (this.socket && this.socket.connected) {
         console.log('用户连接可发送', msg);
-        this.socket.emit('msg',JSON.parse(msg))
       }
       console.log('contrl message: ' , msg);
     })
   }
 }
-new ContrlServer();
+new VideoServer();

+ 1 - 0
package.json

@@ -12,6 +12,7 @@
   "author": "",
   "license": "ISC",
   "dependencies": {
+    "express": "^4.17.3",
     "socket.io": "^4.4.1",
     "i2c-bus": "^5.2.2"
   }

+ 104 - 0
web/index.html

@@ -0,0 +1,104 @@
+<!DOCTYPE html>
+<html lang="en">
+
+<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" />
+    <title>car</title>
+    <script src="./socket.io.min.js"></script>
+    <style>
+        video {
+            width: 500px;
+            height: 400px;
+            background: none;
+            object-fit: fill;
+            border: solid 1px red;
+        }
+    </style>
+</head>
+
+<body>
+    <div>
+        <video id="v2" autoplay playsinline></video>
+    </div>
+    <!-- webrtc -->
+    <script>
+        let RTC = null
+        const socket = io({
+            auth: {
+                roomID: "feiCar",
+                name: "ctrl",
+            },
+            transports: ["websocket"],
+        });
+        socket.on("connect", async () => {
+            const PeerConnection =
+                window.RTCPeerConnection ||
+                window.mozRTCPeerConnection ||
+                window.webkitRTCPeerConnection;
+            RTC = new PeerConnection();
+
+            RTC.ontrack = (event) => {
+                const remoteVideo = document.getElementById("v2");
+                remoteVideo.srcObject = event.streams;
+                console.log("远程流|dom", event, remoteVideo);
+
+            };
+            // 监听ICE|发送
+            RTC.onicecandidate = (event) => {
+                if (event.candidate) {
+                    socket.emit("msg", {
+                        type: "candidate",
+                        candidate: event.candidate,
+                    });
+                    console.log("己方ICE", event.candidate);
+                }
+            };
+            // 监听ICE状态
+            RTC.oniceconnectionstatechange = () => {
+                console.log("ICE状态", RTC.iceConnectionState);
+            };
+            console.log("连接成功监听webrtc");
+        });
+        //   发送offer及anwser
+        socket.on("msg", async (data) => {
+            console.log("用户信息", data);
+            if (data.type === 'offer') {
+                // 设置本地应答
+                RTC.setRemoteDescription(data.offer);
+                // 返回应答
+                const answer = await RTC.createAnswer();
+                RTC.setLocalDescription(answer);
+                // 发送anwser
+                socket.emit("msg", {
+                    type: "answer",
+                    answer: answer,
+                });
+            } else if (data.type === 'candidate') {
+                // 添加ICE
+                RTC.addIceCandidate(data.candidate);
+            }
+        });
+        //   监听加入
+        socket.on('joined', user => {
+            console.log(`${user.name}加入${user.roomID}房间`)
+            // 向其它用户发送开始webrtc
+            socket.emit('msg', {
+                type: 'startRTC'
+            })
+        })
+        // 监听离开
+        socket.on('leaved', user => {
+            console.log(`${user.name}离开${user.roomID}房间`)
+            RTC.close()
+        })
+        //   断开连接
+        socket.on("connect_error", (err) => {
+            console.log("连接错误", err);
+            if (RTC) RTC.close()
+        });
+    </script>
+</body>
+
+</html>

File diff suppressed because it is too large
+ 5 - 0
web/socket.io.min.js


Some files were not shown because too many files changed in this diff