Pre Merge pull request !112 from 周旭湘/dev
This commit is contained in:
commit
0b2de0af31
@ -42,6 +42,8 @@ VXETable.config({
|
||||
import { ElDialog } from 'element-plus';
|
||||
ElDialog.props.closeOnClickModal.default = false;
|
||||
|
||||
import "@/utils/hackTimer"
|
||||
|
||||
const app = createApp(App);
|
||||
|
||||
app.use(HighLight);
|
||||
|
147
src/utils/hackTimer.js
Normal file
147
src/utils/hackTimer.js
Normal file
@ -0,0 +1,147 @@
|
||||
(function (workerScript) {
|
||||
if (!/MSIE 10/i.test (navigator.userAgent)) {
|
||||
try {
|
||||
let blob = new Blob (["\
|
||||
let fakeIdToId = {};\
|
||||
onmessage = function (event) {\
|
||||
let data = event.data,\
|
||||
name = data.name,\
|
||||
fakeId = data.fakeId,\
|
||||
time;\
|
||||
if(data.hasOwnProperty('time')) {\
|
||||
time = data.time;\
|
||||
}\
|
||||
switch (name) {\
|
||||
case 'setInterval':\
|
||||
fakeIdToId[fakeId] = setInterval(function () {\
|
||||
postMessage({fakeId: fakeId});\
|
||||
}, time);\
|
||||
break;\
|
||||
case 'clearInterval':\
|
||||
if (fakeIdToId.hasOwnProperty (fakeId)) {\
|
||||
clearInterval(fakeIdToId[fakeId]);\
|
||||
delete fakeIdToId[fakeId];\
|
||||
}\
|
||||
break;\
|
||||
case 'setTimeout':\
|
||||
fakeIdToId[fakeId] = setTimeout(function () {\
|
||||
postMessage({fakeId: fakeId});\
|
||||
if (fakeIdToId.hasOwnProperty (fakeId)) {\
|
||||
delete fakeIdToId[fakeId];\
|
||||
}\
|
||||
}, time);\
|
||||
break;\
|
||||
case 'clearTimeout':\
|
||||
if (fakeIdToId.hasOwnProperty (fakeId)) {\
|
||||
clearTimeout(fakeIdToId[fakeId]);\
|
||||
delete fakeIdToId[fakeId];\
|
||||
}\
|
||||
break;\
|
||||
}\
|
||||
}\
|
||||
"]);
|
||||
// Obtain a blob URL reference to our worker 'file'.
|
||||
workerScript = window.URL.createObjectURL(blob);
|
||||
} catch (error) {
|
||||
/* Blob is not supported, use external script instead */
|
||||
}
|
||||
}
|
||||
let worker,
|
||||
fakeIdToCallback = {},
|
||||
lastFakeId = 0,
|
||||
maxFakeId = 0x7FFFFFFF, // 2 ^ 31 - 1, 31 bit, positive values of signed 32 bit integer
|
||||
logPrefix = 'HackTimer.js by turuslan: ';
|
||||
if (typeof (Worker) !== 'undefined') {
|
||||
function getFakeId () {
|
||||
do {
|
||||
if (lastFakeId == maxFakeId) {
|
||||
lastFakeId = 0;
|
||||
} else {
|
||||
lastFakeId ++;
|
||||
}
|
||||
} while (fakeIdToCallback.hasOwnProperty (lastFakeId));
|
||||
return lastFakeId;
|
||||
}
|
||||
try {
|
||||
worker = new Worker (workerScript);
|
||||
window.reliableSetInterval = function (callback, time /* , parameters */) {
|
||||
let fakeId = getFakeId ();
|
||||
fakeIdToCallback[fakeId] = {
|
||||
callback: callback,
|
||||
parameters: Array.prototype.slice.call(arguments, 2)
|
||||
};
|
||||
worker.postMessage ({
|
||||
name: 'setInterval',
|
||||
fakeId: fakeId,
|
||||
time: time
|
||||
});
|
||||
return fakeId;
|
||||
};
|
||||
window.reliableClearInterval = function (fakeId) {
|
||||
if (fakeIdToCallback.hasOwnProperty(fakeId)) {
|
||||
delete fakeIdToCallback[fakeId];
|
||||
worker.postMessage ({
|
||||
name: 'clearInterval',
|
||||
fakeId: fakeId
|
||||
});
|
||||
}
|
||||
};
|
||||
window.reliableSetTimeout = function (callback, time /* , parameters */) {
|
||||
let fakeId = getFakeId ();
|
||||
fakeIdToCallback[fakeId] = {
|
||||
callback: callback,
|
||||
parameters: Array.prototype.slice.call(arguments, 2),
|
||||
isTimeout: true
|
||||
};
|
||||
worker.postMessage ({
|
||||
name: 'setTimeout',
|
||||
fakeId: fakeId,
|
||||
time: time
|
||||
});
|
||||
return fakeId;
|
||||
};
|
||||
window.reliableClearTimeout = function (fakeId) {
|
||||
if (fakeIdToCallback.hasOwnProperty(fakeId)) {
|
||||
delete fakeIdToCallback[fakeId];
|
||||
worker.postMessage ({
|
||||
name: 'clearTimeout',
|
||||
fakeId: fakeId
|
||||
});
|
||||
}
|
||||
};
|
||||
worker.onmessage = function (event) {
|
||||
let data = event.data,
|
||||
fakeId = data.fakeId,
|
||||
request,
|
||||
parameters,
|
||||
callback;
|
||||
if (fakeIdToCallback.hasOwnProperty(fakeId)) {
|
||||
request = fakeIdToCallback[fakeId];
|
||||
callback = request.callback;
|
||||
parameters = request.parameters;
|
||||
if (request.hasOwnProperty ('isTimeout') && request.isTimeout) {
|
||||
delete fakeIdToCallback[fakeId];
|
||||
}
|
||||
}
|
||||
if (typeof (callback) === 'string') {
|
||||
try {
|
||||
callback = new Function (callback);
|
||||
} catch (error) {
|
||||
console.log (logPrefix + 'Error parsing callback code string: ', error);
|
||||
}
|
||||
}
|
||||
if (typeof (callback) === 'function') {
|
||||
callback.apply (window, parameters);
|
||||
}
|
||||
};
|
||||
worker.onerror = function (event) {
|
||||
console.log (event);
|
||||
};
|
||||
} catch (error) {
|
||||
console.log (logPrefix + 'Initialisation failed');
|
||||
console.error (error);
|
||||
}
|
||||
} else {
|
||||
console.log (logPrefix + 'Initialisation failed - HTML5 Web Worker is not supported');
|
||||
}
|
||||
}) ('HackTimerWorker.js');
|
@ -19,8 +19,10 @@
|
||||
*/
|
||||
|
||||
import { getToken } from '@/utils/auth';
|
||||
import { ElNotification } from 'element-plus';
|
||||
import useNoticeStore from '@/store/modules/notice';
|
||||
import { ElNotification } from "element-plus";
|
||||
|
||||
const { addNotice } = useNoticeStore();
|
||||
|
||||
let socketUrl: any = ''; // socket地址
|
||||
let websocket: any = null; // websocket 实例
|
||||
@ -71,13 +73,13 @@ export const websocketclose = () => {
|
||||
export const resetHeart = () => {
|
||||
socketHeart = 0;
|
||||
socketError = 0;
|
||||
clearInterval(heartTime);
|
||||
(window as any).reliableClearInterval(heartTime);
|
||||
sendSocketHeart();
|
||||
};
|
||||
|
||||
// socket心跳发送
|
||||
export const sendSocketHeart = () => {
|
||||
heartTime = setInterval(() => {
|
||||
heartTime = (window as any).reliableSetInterval(() => {
|
||||
// 如果连接正常则发送心跳
|
||||
if (websocket.readyState == 1) {
|
||||
// if (socketHeart <= 30) {
|
||||
@ -97,7 +99,7 @@ export const sendSocketHeart = () => {
|
||||
// socket重连
|
||||
export const reconnect = () => {
|
||||
if (socketError <= 2) {
|
||||
clearInterval(heartTime);
|
||||
(window as any).reliableClearInterval(heartTime);
|
||||
initWebSocket(socketUrl);
|
||||
socketError = socketError + 1;
|
||||
// eslint-disable-next-line prettier/prettier
|
||||
@ -105,7 +107,7 @@ export const reconnect = () => {
|
||||
} else {
|
||||
// eslint-disable-next-line prettier/prettier
|
||||
console.log('重试次数已用完');
|
||||
clearInterval(heartTime);
|
||||
(window as any).reliableClearInterval(heartTime);
|
||||
}
|
||||
};
|
||||
|
||||
@ -123,7 +125,7 @@ export const websocketonmessage = () => {
|
||||
if (e.data.indexOf('ping') > 0) {
|
||||
return;
|
||||
}
|
||||
useNoticeStore().addNotice({
|
||||
addNotice({
|
||||
message: e.data,
|
||||
read: false,
|
||||
time: new Date().toLocaleString()
|
||||
@ -133,7 +135,7 @@ export const websocketonmessage = () => {
|
||||
message: e.data,
|
||||
type: 'success',
|
||||
duration: 3000
|
||||
});
|
||||
})
|
||||
return e.data;
|
||||
};
|
||||
};
|
||||
|
Loading…
x
Reference in New Issue
Block a user