| | |
| | | /* |
| | | * @Descripttion: 工具集 |
| | | * @version: 1.2 |
| | | * @LastEditors: sakuya |
| | | * @LastEditTime: 2022年5月24日00:28:56 |
| | | * @LastEditors: lzhe lzhe@example.com |
| | | * @LastEditTime: 2024-06-17 18:21:44 |
| | | */ |
| | | |
| | | import CryptoJS from 'crypto-js'; |
| | |
| | | |
| | | const tool = {} |
| | | |
| | | var currentLocation = window.location.href; // 获取完整的URL |
| | | var protocol = window.location.protocol; // 获取协议(如http:或https:) |
| | | var hostname = window.location.hostname; // 获取主机名 |
| | | var port = window.location.port; // 获取端口号 |
| | | var pathname = window.location.pathname; // 获取路径名 |
| | | if (process.env.NODE_ENV=='development') { |
| | | var path = "120.46.212.231:84"; |
| | | } else { |
| | | var path = hostname+":"+port; |
| | | } |
| | | /* localStorage */ |
| | | tool.data = { |
| | | set(key, data, datetime = 0) { |
| | |
| | | } |
| | | } |
| | | |
| | | /* socket */ |
| | | tool.socket = { |
| | | url:'ws://'+path+'/ws/info', |
| | | websocket: null, |
| | | connectToWebSocket(token) { //建立链接 |
| | | this.websocket = new WebSocket(this.url + "?access_token=" + token); |
| | | }, |
| | | sendDataToWebSocket(data) { //发送 |
| | | if(!data) return; //没有入参不发送 |
| | | if (this.websocket.readyState === this.websocket.OPEN) { |
| | | this.websocket.send(JSON.stringify(data)); |
| | | } |
| | | // this.websocket.onmessage = function(event) { |
| | | // // 当从服务器收到消息时 |
| | | // console.error("WebSocket Error: ", event.data); |
| | | // }; |
| | | |
| | | // this.websocket.onerror = function(error) { |
| | | // // 当WebSocket连接发生错误时 |
| | | // console.error("WebSocket Error: ", error); |
| | | // }; |
| | | |
| | | // this.websocket.onclose = function(event) { |
| | | // // 当WebSocket连接关闭时 |
| | | // console.log("WebSocket is closed now."); |
| | | // // 可以在这里重试连接或其他逻辑... |
| | | // }; |
| | | return this; |
| | | } |
| | | } |
| | | /* Fullscreen */ |
| | | tool.screen = function (element) { |
| | | var isFull = !!(document.webkitIsFullScreen || document.mozFullScreen || document.msFullscreenElement || document.fullscreenElement); |