@jxstjh/jhvideo
Version:
HTML5 jhvideo base on MPEG2-TS Stream Player
132 lines (129 loc) • 4.43 kB
JavaScript
import httpClient from './core/httpClient'
import Worker from "./utils/keepAlive.worker.js";
import { serverName } from './globalClient'
import { filterAisleIdChart } from './utils/utils';
const GLOBAL_NAME_SPACE = "JXST_JH_VIDEO_CLIENT_"
/*
*@description
*@author jsyang
*@date 2021-06-03 14:55:19
*@variable LoginUrl 登录接口
*@variable 变量2
*@variable 变量3
*/
export const STATUS_TYPE = {
'OnInitSuccess': 'OnInitSuccess',
'OnInitError': 'OnInitError',
'OnReloginError': 'OnReloginError'
}
const LoginUrl = '/video-h5ms/Login'
const KeepAliveUrl = '/video-h5ms/keepAlive'
/**
* H5msClient
* @param 参数1
* @param 参数2
* @return
* @description
* @author jsyang
* @date 2021-06-03 14:20:07
*/
class H5msClient {
_tokenId
_aisleId
_tenantId
_worker
_globalClient
_onStatusChanged
constructor(globalClient, aisleId, onStatusChanged = (statusMsg, error = null) => { }) {
this._globalClient = globalClient
this.aisleId = aisleId
this._onStatusChanged = onStatusChanged
this.login(globalClient.config.endPoint + serverName + LoginUrl, aisleId).then(res => {
if (res.code !== 0) {
throw new Error(res.msg)
// this._onStatusChanged(STATUS_TYPE.OnInitError, res)
} else {
if (res.data && res.data.code !== 200) {
throw new Error(res.data.msg)
// this._onStatusChanged(STATUS_TYPE.OnInitError, res.data)
} else {
this.token = res.data.msg.tokenId
this._tenantId = res.data.msg.tenantId
return this._tokenId
}
}
})
// .then((tenantId) => {
// this.initKeepAlive(tenantId)
// })
.then(() => {
this._onStatusChanged(STATUS_TYPE.OnInitSuccess)
}).catch(err => {
this._onStatusChanged(STATUS_TYPE.OnInitError, err)
})
}
login(url, aisleId) {
aisleId = filterAisleIdChart(aisleId)
const headers = {
"Content-Type": "application/x-www-form-urlencoded",
"Authorization": 'Bearer ' + this._globalClient.accessToken,
}
return httpClient.post(url, { aisleId }, headers).then(res => {
return res
})
}
async reLogin() {
await this._globalClient.reLogin() // 客户端重连
const res = await this.login(this._globalClient.config.endPoint + serverName + LoginUrl, this._aisleId)
if (res.code !== 0) {
this._onStatusChanged(STATUS_TYPE.OnReloginError, res)
throw new Error(res.msg)
} else {
if (res.data && res.data.code !== 200) {
this._onStatusChanged(STATUS_TYPE.OnReloginError, res.data)
throw new Error(res.data.msg)
} else {
this.token = res.data.msg.tokenId
this._tenantId = res.data.msg.tenantId
return this.token
}
}
}
initKeepAlive(tenantId) {
const tokenId = this.token
const aisleId = this._aisleId
const token = this._globalClient.accessToken
this._worker = new Worker();
this._worker.postMessage({ cmd: 'START_KEEPALIVE', aisleId, tokenId, token, url: this._globalClient.config.endPoint + serverName + KeepAliveUrl });
// setTimeout(function () {
// this.worker.postMessage({ cmd: 'DESTROY_KEEPALIVE'});
// }.bind(this),5000)
this._worker.onmessage = function (event) {
console.log('worker.event==>',event)
// TODO: h5msClient自动重连策略
}
}
destroy() {
this._worker && this._worker.postMessage({ cmd: 'DESTROY_KEEPALIVE'});
this._worker && this._worker.terminate();
}
get globalClient() {
return this._globalClient
}
set globalClient(_globalClient) {
this._globalClient = _globalClient
}
get token() {
return this._token
}
set token(token) {
this._token = token
}
get aisleId() {
return this._aisleId
}
set aisleId(aisleId) {
this._aisleId = aisleId
}
};
export default H5msClient;