index.vue 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. <template>
  2. <div class="box">
  3. <div v-if="isLogin">
  4. <!-- RTC -->
  5. <video id="v2" autoplay playsinline></video>
  6. <!-- 摇杆 -->
  7. <Contrl class="ctrl" ref="Contrl" @callBack="contrlBack" />
  8. <!-- 电量 -->
  9. <Battery class="battery" :quantity="Battery" />
  10. <!-- GPS -->
  11. <Map :Option="GPS" />
  12. <!-- 信号 -->
  13. <Signal class="signal" :signalValue="Signal" :signalText="SignalText" />
  14. <!-- 音频 -->
  15. <Record class="audio" @callBack="AudioBack" />
  16. <!-- loading -->
  17. <Loading v-if="showLoading" />
  18. </div>
  19. <Login v-else :err="error" @loginBack="login" />
  20. </div>
  21. </template>
  22. <script>
  23. const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
  24. import Contrl from "../components/Contrl.vue";
  25. import Battery from "../components/Battery.vue";
  26. import Map from "../components/Map.vue";
  27. import Signal from "../components/Signal.vue";
  28. import Login from "../components/Login.vue";
  29. import Loading from "../components/loading.vue";
  30. import webRTC from "@/utils/RTC.js";
  31. import Record from "../components/Record.vue";
  32. import io from 'socket.io-client'
  33. export default {
  34. components: {
  35. Battery,
  36. Contrl,
  37. Map,
  38. Signal,
  39. Login,
  40. Loading,
  41. Record,
  42. },
  43. data() {
  44. return {
  45. socket: null,
  46. HOST: "wss://car.caner.top",
  47. error: "",
  48. isLogin: false,
  49. showLoading: true,
  50. GPS: {
  51. position: [104.056608, 30.695942],
  52. zoom: 18,
  53. },
  54. Signal: 3,
  55. SignalText: 0,
  56. Battery: 100,
  57. remoteVideo: null,
  58. };
  59. },
  60. methods: {
  61. // 初始化
  62. intSoketRtc(host) {
  63. // 初始化socket
  64. this.socket = io(host, {
  65. autoConnect: false,
  66. transports: ["websocket"],
  67. });
  68. // 监听socket
  69. this.socket.on("connect", () => {
  70. this.connect();
  71. });
  72. this.socket.on("leaved", () => {
  73. this.close("车端离开");
  74. });
  75. this.socket.on("joined", () => {
  76. // 请求开始RTC
  77. this.socket.emit("msg", {
  78. type: "startRTC",
  79. });
  80. });
  81. this.socket.on("msg", (data) => {
  82. this.msg(data);
  83. });
  84. this.socket.on("connect_error", (err) => {
  85. console.log("连接失败", err);
  86. this.close(err);
  87. });
  88. },
  89. // 连接
  90. async connect() {
  91. console.log("连接成功", this.socket.auth);
  92. this.isLogin = true;
  93. try {
  94. // 初始化RTC
  95. webRTC.init();
  96. await sleep(100);
  97. // 监听视频流
  98. this.remoteVideo = document.getElementById("v2");
  99. webRTC.ontrack((event) => {
  100. this.remoteVideo.srcObject = event.streams[0];
  101. });
  102. // 监听ICE|发送
  103. webRTC.onicecandidate((event) => {
  104. if (event.candidate) {
  105. this.socket.emit("msg", {
  106. type: "candidate",
  107. candidate: event.candidate,
  108. });
  109. }
  110. });
  111. // 监听ICE状态
  112. webRTC.oniceconnectionstatechange(async () => {
  113. // 失败
  114. if (
  115. webRTC.RTC.iceConnectionState === "failed" ||
  116. webRTC.RTC.iceConnectionState === "disconnected" ||
  117. webRTC.RTC.iceConnectionState === "closed"
  118. ) {
  119. this.close("ICE通信失败");
  120. }
  121. // ICE连接成功|初始化摇杆
  122. if (webRTC.RTC.iceConnectionState === "connected") {
  123. await sleep(1000 * 2);
  124. this.$refs.Contrl.initContrl();
  125. this.showLoading = false;
  126. }
  127. });
  128. // RTC通信信号
  129. webRTC.getReaport((data) => {
  130. this.SignalText = data.size;
  131. });
  132. } catch (error) {
  133. this.socket.disconnect();
  134. this.isLogin = false
  135. alert('不支持Webrtc')
  136. }
  137. },
  138. // 通信
  139. async msg(data) {
  140. if (data.type === "offer") {
  141. // 设置本地应答
  142. webRTC.setRemoteDescription(data.offer);
  143. // 返回应答
  144. const answer = await webRTC.createAnswer();
  145. webRTC.setLocalDescription(answer);
  146. // 发送anwser
  147. this.socket.emit("msg", {
  148. type: "answer",
  149. answer: answer,
  150. });
  151. } else if (data.type === "candidate") {
  152. // 添加ICE
  153. webRTC.addIceCandidate(data.candidate);
  154. } else if (data.type === "power") {
  155. // 电量
  156. this.Battery = data.power;
  157. } else if (data.type === "LTE") {
  158. // 信号
  159. this.Signal = data.LTE;
  160. } else if (data.type === "GPS") {
  161. // gps
  162. this.GPS.position = data.GPS;
  163. }
  164. },
  165. // 登录
  166. login(data) {
  167. // 设置登录头
  168. if (this.socket) {
  169. this.socket.auth = {
  170. roomID: data.roomID,
  171. name: data.name,
  172. };
  173. this.socket.connect();
  174. } else {
  175. this.error = "服务器连接失败";
  176. }
  177. },
  178. // 摇杆
  179. contrlBack(data) {
  180. if (!this.socket && !this.socket.connected) return;
  181. this.socket.emit("msg", {
  182. type: "conctrl",
  183. conctrl: data,
  184. });
  185. },
  186. // 音频
  187. AudioBack(data) {
  188. if (!this.socket && !this.socket.connected) return;
  189. console.log("回调", data);
  190. this.socket.emit("msg", {
  191. type: "Meadia",
  192. Meadia: data,
  193. });
  194. },
  195. // 清除
  196. close(err) {
  197. if (webRTC) webRTC.close();
  198. if (this.remoteVideo) this.remoteVideo.srcObject = null;
  199. if (this.$refs.Contrl) this.$refs.Contrl.clearContrl();
  200. this.socket.disconnect();
  201. this.isLogin = false;
  202. this.showLoading = true;
  203. this.error = err || "";
  204. },
  205. },
  206. mounted() {
  207. this.intSoketRtc(this.HOST);
  208. },
  209. };
  210. </script>
  211. <style scoped>
  212. .box {
  213. width: 100vw;
  214. height: 100vh;
  215. position: relative;
  216. overflow: hidden;
  217. }
  218. .ctrl,
  219. .battery,
  220. .signal {
  221. position: fixed;
  222. overflow: hidden;
  223. z-index: 99;
  224. }
  225. .ctrl {
  226. width: 100vw;
  227. height: 100vh;
  228. top: 0;
  229. left: 0;
  230. }
  231. .battery {
  232. top: 5px;
  233. right: 5px;
  234. }
  235. .signal {
  236. right: 50px;
  237. top: 3px;
  238. }
  239. video {
  240. width: 100vw;
  241. height: 100vh;
  242. background: none;
  243. object-fit: fill;
  244. }
  245. .audio {
  246. position: fixed;
  247. top: 3px;
  248. right: 90px;
  249. z-index: 99;
  250. }
  251. </style>