Browse Source

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

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

+ 5 - 5
contrl.js

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

+ 6 - 10
index.js

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

+ 0 - 1
package.json

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

+ 0 - 104
web/index.html

@@ -1,104 +0,0 @@
-<!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
+ 0 - 5
web/socket.io.min.js


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