|
@@ -1,186 +1,13 @@
|
|
|
-const Base64 = window.Base64;
|
|
|
-let timeoutObj = null; // 心跳倒计时
|
|
|
-let serverTimeoutObj = null; // 心跳倒计时
|
|
|
-let reconnetNum = 0; // 重连次数
|
|
|
-let lockReconnect = false; // 是否重新建立连接
|
|
|
-let timeoutnum = null; // 断开 重连倒计时
|
|
|
-let receiveNews = []; // 接受到的消息
|
|
|
-let isOrReceive = false; // 是否在45秒内收到消息
|
|
|
-let webSocketUrl = ""; // websocket创建地址
|
|
|
-let websocket = null;
|
|
|
-var wsModuleObj = ""; // 所属模块
|
|
|
-let wsSystem = ""; // 所属系统
|
|
|
-let messageId = ""; // 消息ID
|
|
|
-const sendObj = {
|
|
|
- load: {},
|
|
|
- path: "internal/heart_beat",
|
|
|
- protocolId: "PROTO_HEART_BEAT",
|
|
|
- sourceType: "request",
|
|
|
- taskId: "",
|
|
|
-};
|
|
|
-/**
|
|
|
- * @description 心跳消息动态生成16位字符串
|
|
|
- */
|
|
|
-function uuid() {
|
|
|
- function S4() {
|
|
|
- return (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1);
|
|
|
- }
|
|
|
- return S4() + S4() + "-" + S4() + "-" + S4() + "-" + S4() + "-" + S4() + S4() + S4();
|
|
|
-}
|
|
|
-
|
|
|
-// 重新连接
|
|
|
-function reconnect(wsUrls) {
|
|
|
- addLog(JSON.stringify("链接已经断开," + new Date().toString()), 2004);
|
|
|
- Message({ message: "websocket链接已断开", type: "error" });
|
|
|
- if (lockReconnect) {
|
|
|
- return;
|
|
|
- }
|
|
|
- lockReconnect = true;
|
|
|
- // 没连接上会一直重连,设置延迟避免请求过多
|
|
|
- timeoutnum && clearTimeout(timeoutnum);
|
|
|
- timeoutnum = setTimeout(function () {
|
|
|
- if (reconnetNum < 4) {
|
|
|
- reconnetNum++;
|
|
|
- websocket = null;
|
|
|
- createWebSocket(wsUrls);
|
|
|
- lockReconnect = false;
|
|
|
- } else {
|
|
|
- timeoutObj && clearInterval(timeoutObj);
|
|
|
- timeoutnum && clearTimeout(timeoutnum);
|
|
|
- websocket.close();
|
|
|
- messageBox();
|
|
|
- return;
|
|
|
- }
|
|
|
- }, 20000);
|
|
|
-}
|
|
|
+// import WebSocket from "ws";
|
|
|
|
|
|
-// 创建两个定时器
|
|
|
-function heartCheck() {
|
|
|
- // 第一个定时器每隔20S给后端发送一个消息
|
|
|
- timeoutObj && clearInterval(timeoutObj);
|
|
|
- timeoutObj = setInterval(() => {
|
|
|
- websocketsend(sendObj);
|
|
|
- }, 20000);
|
|
|
- serverTimeoutObj && clearInterval(serverTimeoutObj);
|
|
|
- // 第二个定时器每隔45S监测 后端是否给我发送了心跳
|
|
|
- serverTimeoutObj = setInterval(() => {
|
|
|
- if (isOrReceive) {
|
|
|
- isOrReceive = false;
|
|
|
- } else {
|
|
|
- reconnect(webSocketUrl);
|
|
|
- }
|
|
|
- }, 45000);
|
|
|
-}
|
|
|
-// 连接成功
|
|
|
-function wsOnopen() {
|
|
|
- reconnetNum = 0;
|
|
|
- heartCheck(); // 开启心跳监测
|
|
|
-}
|
|
|
-// 发生异常
|
|
|
-function wsOnerror(e) {
|
|
|
- reconnect(webSocketUrl); // 发生异常,重新链接
|
|
|
-}
|
|
|
-// 关闭链接
|
|
|
-export function websocketclose(e) {
|
|
|
- websocket.close();
|
|
|
-}
|
|
|
-/**
|
|
|
- * @description 接收服务器推送的信息
|
|
|
- * @param {消息事件参数} e
|
|
|
- * @param {所属模块} moduleObj
|
|
|
- * @param {所属系统} system
|
|
|
- */
|
|
|
-export function websocketonmessage(e, moduleObj, system) {
|
|
|
- if (e && e.data) {
|
|
|
- // 发送消息的模块
|
|
|
- const tempData = JSON.parse(e.data);
|
|
|
- const redata = tempData.load;
|
|
|
- if (tempData.path === "internal/heart_beat") {
|
|
|
- // 如果是心跳消息
|
|
|
- isOrReceive = true; // 告诉45s定时器,收到了心跳消息
|
|
|
- sendObj.load = redata;
|
|
|
- } else {
|
|
|
- for (let i = 0; i < moduleObj.length; i++) {
|
|
|
- if (tempData.path !== "internal/heart_beat") {
|
|
|
- const logInfo = { "messageId:": redata.msgId, content: redata.content, all: redata };
|
|
|
- receiveNews.unshift(redata);
|
|
|
- if (messageId !== redata.messageId) {
|
|
|
- moduleObj[i].val(redata);
|
|
|
- messageId = redata.messageId;
|
|
|
- }
|
|
|
- receiveNews = [];
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
-}
|
|
|
-/**
|
|
|
- * @description 向服务器发送信息
|
|
|
- * @param {消息内容}} msg
|
|
|
- */
|
|
|
-function websocketsend(msg) {
|
|
|
- var jsonString = JSON.stringify(msg);
|
|
|
- if (websocket.readyState === 1) {
|
|
|
- // 如果连接正常
|
|
|
- websocket.send(jsonString);
|
|
|
- }
|
|
|
-}
|
|
|
-
|
|
|
-/**
|
|
|
- * @description webSocket 对外注册方法
|
|
|
- * @param {环境变量} env
|
|
|
- * @param {所属模块} moduleObj
|
|
|
- * @param {所属系统} system
|
|
|
- * @param {UM账号} um
|
|
|
- * @param {令牌身份}} token
|
|
|
- */
|
|
|
-export function register(env, moduleObj, system, um) {
|
|
|
- addLog(JSON.stringify("初始化长链接:" + um + "," + system + "," + moduleObj + "," + env + "," + new Date().toString()), 2002);
|
|
|
- initWebSocket(env, moduleObj, system, um);
|
|
|
-}
|
|
|
-// 初始化创建webSocketUrl
|
|
|
-function initWebSocket(env, moduleObj, system, umId) {
|
|
|
- wsModuleObj = moduleObj;
|
|
|
- wsSystem = system || window.localStorage.getItem("systemId");
|
|
|
- let wsUrls= "wss://mstutest.dayaedu.com/audioAnalysis";
|
|
|
- const um = umId || window.localStorage.getItem("umId");
|
|
|
- const base64 = new Base64(um.toUpperCase());
|
|
|
- let token = base64.encode(um);
|
|
|
- // if (env === "stg") {
|
|
|
- // wsUrls = "ws://xxxxx";
|
|
|
- // } else if (env === "prd") {
|
|
|
- // wsUrls = "ws://xxxxx";
|
|
|
- // } else if (env === "dev") {
|
|
|
- // wsUrls = "ws://xxxxx";
|
|
|
- // }
|
|
|
- wsUrls = wsUrls + "?token=" + token + "&system=" + wsSystem;
|
|
|
- webSocketUrl = wsUrls;
|
|
|
- createWebSocket(webSocketUrl);
|
|
|
-}
|
|
|
-/**
|
|
|
- * @description 创建长链接,并初始化方法
|
|
|
- * @param {长链接地址} url
|
|
|
- */
|
|
|
-function createWebSocket(url) {
|
|
|
- if ("WebSocket" in window) {
|
|
|
- // 判断当前浏览器是否支持WebSocket
|
|
|
- if (!websocket) {
|
|
|
- websocket = new WebSocket(url); // 初始化webSocket
|
|
|
- init(); // 初始化websocket的方法
|
|
|
- }
|
|
|
- } else {
|
|
|
- alert("当前浏览器 Not support websocket");
|
|
|
- }
|
|
|
-}
|
|
|
-/**
|
|
|
- * @description 初始化websocket事件
|
|
|
- */
|
|
|
-function init() {
|
|
|
- sendObj.taskId = uuid(); // 初始化taskId
|
|
|
- websocket.onerror = wsOnerror; // 链接错误
|
|
|
- websocket.onclose = websocketclose; // 链接关闭
|
|
|
- websocket.onopen = wsOnopen; // 链接成功
|
|
|
- websocket.onmessage = (e) => {
|
|
|
- websocketonmessage(e, wsModuleObj, wsSystem);
|
|
|
- }; // 接受信息
|
|
|
-}
|
|
|
+let ws: WebSocket;
|
|
|
+export const createSocket = () => {
|
|
|
+ ws = new WebSocket("wss://mstutest.dayaedu.com/audioAnalysis");
|
|
|
+ ws.onopen = function () {
|
|
|
+ console.log("连接成功");
|
|
|
+ ws.send("发送数据");
|
|
|
+ };
|
|
|
+ ws.onmessage = function (evt) {
|
|
|
+ console.log("接收到的数据:" + evt.data);
|
|
|
+ };
|
|
|
+};
|