Browse Source

修正类型

Caner 2 years ago
parent
commit
cd9d8eb7c8
2 changed files with 31 additions and 23 deletions
  1. 28 22
      src/views/index/index.vue
  2. 3 1
      src/vite-env.d.ts

+ 28 - 22
src/views/index/index.vue

@@ -1,9 +1,11 @@
 <script setup lang="ts">
+import { Socket, io } from 'socket.io-client';
 import { onMounted, onUnmounted, watch } from 'vue';
 import { ref } from 'vue';
-const socket = ref(null)
+const sleep = (ms:number) => new Promise((res) => setTimeout(res, ms));
+const socket = ref(null as null | Socket)
 const HOST = ref("wss://car.caner.top")
-const Peer = ref(null)
+const Peer = ref(null as null | RTCPeerConnection | undefined)
 const isLogin = ref(false)
 const error = ref("")
 const remoteVideo = ref()
@@ -26,8 +28,12 @@ const iceServers = ref([
   },
 ])
 const num = ref(0)
-// 网络连接
-function intSoketRtc(host) {
+
+/**
+ * 网络连接
+ * @param host 
+ */
+function intSoketRtc(host: string) {
   // int socket
   socket.value = io(host, {
     autoConnect: false,
@@ -46,10 +52,10 @@ function intSoketRtc(host) {
 
       // listen state
       Peer.value.onicegatheringstatechange = () => {
-        console.log("GatheringState: ", Peer.value.iceGatheringState);
-        if (Peer.value.iceGatheringState === "complete") {
+        console.log("GatheringState: ", Peer.value?.iceGatheringState);
+        if (Peer.value?.iceGatheringState === "complete") {
           const answer = Peer.value.localDescription;
-          socket.value.emit("msg", answer);
+          socket.value?.emit("msg", answer);
         }
       };
 
@@ -61,7 +67,7 @@ function intSoketRtc(host) {
 
       // listen changestate·
       Peer.value.oniceconnectionstatechange = async () => {
-        const state = Peer.value.iceConnectionState;
+        const state = Peer.value?.iceConnectionState;
         console.log("ICE状态", state);
         if (
           state === "failed" ||
@@ -88,9 +94,9 @@ function intSoketRtc(host) {
   socket.value.on("msg", async (data) => {
     const key = data.type
     if (key === 'offer') {
-      await Peer.value.setRemoteDescription(data);
-      const answer = await Peer.createAnswer();
-      await Peer.value.setLocalDescription(answer);
+      await Peer.value?.setRemoteDescription(data);
+      const answer = await Peer.value?.createAnswer();
+      await Peer.value?.setLocalDescription(answer);
     } else if (key === 'power') {
       console.log("电量", data);
     } else if (key === 'speed') {
@@ -100,7 +106,7 @@ function intSoketRtc(host) {
   });
 
   socket.value.on("joined", async () =>
-    socket.value.emit("msg", { type: "startRTC" })
+    socket.value?.emit("msg", { type: "startRTC" })
   );
 
   socket.value.on("leaved", () => close("车端断开"));
@@ -135,16 +141,16 @@ function ControlData() {
 }
 
 // 发送语音
-function sendAudio(blob) {
-  if (!socket.value && !socket.value.connected) return;
-  socket.value.emit("msg", {
+function sendAudio(blob:Blob) {
+  if (!socket.value && !socket.value!.connected) return;
+  socket.value?.emit("msg", {
     type: "Meadia",
     Meadia: blob,
   });
 }
 
 // 挡位
-function Gear(speed, num) {
+function Gear(speed:number, num:number) {
   // 低速档
   if (speed === 1) {
     if (num < 116) {
@@ -171,7 +177,7 @@ function Gear(speed, num) {
 }
 
 // 登录
-function login(data) {
+function login(data: { name: string, roomID: string }) {
   if (socket.value) {
     socket.value.auth = {
       roomID: data.roomID,
@@ -185,16 +191,16 @@ function login(data) {
 
 // 关闭
 function close(err?: string) {
-  if (Peer) Peer.close();
-  if (remoteVideo) remoteVideo.srcObject = null;
-  if (socket) socket.disconnect();
+  if (Peer) Peer.value?.close();
+  if (remoteVideo) remoteVideo.value.srcObject = null;
+  if (socket) socket.value?.disconnect();
   isLogin.value = false;
   showLoading.value = false;
   error.value = err || "";
   socket.value = null;
   Peer.value = null
   num.value = 0
-  cancelAnimationFrame(ControlData);
+  cancelAnimationFrame(ControlData as any);
   window.removeEventListener("gamepadconnected", conControl);
   window.removeEventListener("gamepaddisconnected", disControl);
 }
@@ -208,7 +214,7 @@ function conControl() {
 // 手柄断开连接
 function disControl() {
   contrlState.value = false;
-  cancelAnimationFrame(ControlData);
+  cancelAnimationFrame(ControlData as any);
 }
 
 watch(() => mutedState.value, (v) => {

+ 3 - 1
src/vite-env.d.ts

@@ -8,4 +8,6 @@ declare module '*.vue' {
 
 declare interface Window {
   $electron: Any
-}
+}
+
+declare module 'echarts'