@jxstjh/jhvideo
Version:
HTML5 jhvideo base on MPEG2-TS Stream Player
194 lines • 8.44 kB
JavaScript
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __generator = (this && this.__generator) || function (thisArg, body) {
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
function verb(n) { return function (v) { return step([n, v]); }; }
function step(op) {
if (f) throw new TypeError("Generator is already executing.");
while (g && (g = 0, op[0] && (_ = 0)), _) try {
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
if (y = 0, t) op = [op[0] & 2, t.value];
switch (op[0]) {
case 0: case 1: t = op; break;
case 4: _.label++; return { value: op[1], done: false };
case 5: _.label++; y = op[1]; op = [0]; continue;
case 7: op = _.ops.pop(); _.trys.pop(); continue;
default:
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
if (t[2]) _.ops.pop();
_.trys.pop(); continue;
}
op = body.call(thisArg, _);
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
}
};
import httpClient from './core/httpClient';
import Worker from "./utils/keepAlive.worker.js";
import { serverName } from './globalClient';
import { filterAisleIdChart } from './utils/utils';
var GLOBAL_NAME_SPACE = "JXST_JH_VIDEO_CLIENT_";
/*
*@description
*@author jsyang
*@date 2021-06-03 14:55:19
*@variable LoginUrl 登录接口
*@variable 变量2
*@variable 变量3
*/
export var STATUS_TYPE = {
'OnInitSuccess': 'OnInitSuccess',
'OnInitError': 'OnInitError',
'OnReloginError': 'OnReloginError'
};
var LoginUrl = '/video-h5ms/Login';
var KeepAliveUrl = '/video-h5ms/keepAlive';
/**
* H5msClient
* @param 参数1
* @param 参数2
* @return
* @description
* @author jsyang
* @date 2021-06-03 14:20:07
*/
var H5msClient = /** @class */ (function () {
function H5msClient(globalClient, aisleId, onStatusChanged) {
if (onStatusChanged === void 0) { onStatusChanged = function (statusMsg, error) {
if (error === void 0) { error = null; }
}; }
var _this = this;
this._globalClient = globalClient;
this.aisleId = aisleId;
this._onStatusChanged = onStatusChanged;
this.login(globalClient.config.endPoint + serverName + LoginUrl, aisleId).then(function (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(function () {
_this._onStatusChanged(STATUS_TYPE.OnInitSuccess);
}).catch(function (err) {
_this._onStatusChanged(STATUS_TYPE.OnInitError, err);
});
}
H5msClient.prototype.login = function (url, aisleId) {
aisleId = filterAisleIdChart(aisleId);
var headers = {
"Content-Type": "application/x-www-form-urlencoded",
"Authorization": 'Bearer ' + this._globalClient.accessToken,
};
return httpClient.post(url, { aisleId: aisleId }, headers).then(function (res) {
return res;
});
};
H5msClient.prototype.reLogin = function () {
return __awaiter(this, void 0, void 0, function () {
var res;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this._globalClient.reLogin()]; // 客户端重连
case 1:
_a.sent(); // 客户端重连
return [4 /*yield*/, this.login(this._globalClient.config.endPoint + serverName + LoginUrl, this._aisleId)];
case 2:
res = _a.sent();
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 [2 /*return*/, this.token];
}
}
return [2 /*return*/];
}
});
});
};
H5msClient.prototype.initKeepAlive = function (tenantId) {
var tokenId = this.token;
var aisleId = this._aisleId;
var token = this._globalClient.accessToken;
this._worker = new Worker();
this._worker.postMessage({ cmd: 'START_KEEPALIVE', aisleId: aisleId, tokenId: tokenId, token: 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自动重连策略
};
};
H5msClient.prototype.destroy = function () {
this._worker && this._worker.postMessage({ cmd: 'DESTROY_KEEPALIVE' });
this._worker && this._worker.terminate();
};
Object.defineProperty(H5msClient.prototype, "globalClient", {
get: function () {
return this._globalClient;
},
set: function (_globalClient) {
this._globalClient = _globalClient;
},
enumerable: false,
configurable: true
});
Object.defineProperty(H5msClient.prototype, "token", {
get: function () {
return this._token;
},
set: function (token) {
this._token = token;
},
enumerable: false,
configurable: true
});
Object.defineProperty(H5msClient.prototype, "aisleId", {
get: function () {
return this._aisleId;
},
set: function (aisleId) {
this._aisleId = aisleId;
},
enumerable: false,
configurable: true
});
return H5msClient;
}());
;
export default H5msClient;
//# sourceMappingURL=h5msClient.js.map