@jxstjh/jhvideo
Version:
HTML5 jhvideo base on MPEG2-TS Stream Player
46 lines • 1.53 kB
JavaScript
import httpClient from '../core/httpClient'
let keepAlive$; // 定时器
let keepAliveFaildCount = 0; // 错误计数器
function keepAliveWoker(token, tokenId, url, aisleId) {
const body = { tokenId, aisleId }
const headers = {
'Content-Type': 'application/x-www-form-urlencoded',
'Authorization': 'Bearer ' + token
}
httpClient.ajax('post', url, body, headers).then(function (res) {
if (res.code === 0 && res.data.code === 200) {
keepAliveFaildCount = 0;
} else {
keepAliveFaildCount += 1;
if (keepAliveFaildCount > 3) {
postMessage('KEEPALIVE_ERROR')
}
}
}).catch(function (err) {
keepAliveFaildCount += 1;
if (keepAliveFaildCount > 3) {
postMessage('KEEPALIVE_ERROR')
}
})
}
onmessage = function (event) {
let aisleId = event.data.aisleId
const arr = aisleId.split('#')
if (arr.length > 1) {
aisleId = arr[0]
}
switch (event.data.cmd) {
case 'START_KEEPALIVE':
const tokenId = event.data.tokenId
const url = event.data.url
const token = event.data.token
keepAliveFaildCount = 0;
keepAlive$ = setInterval(keepAliveWoker.bind(null, token, tokenId, url, aisleId), 8 * 1000);
break;
case 'DESTROY_KEEPALIVE':
clearInterval(keepAlive$)
break;
default:
break;
}
};