|
@@ -16,12 +16,7 @@ export class LiveWS {
|
|
|
* @param {number} reconnectDelay - 连接断开后重连的延迟,单位毫秒
|
|
* @param {number} reconnectDelay - 连接断开后重连的延迟,单位毫秒
|
|
|
*/
|
|
*/
|
|
|
constructor(url, liveId, userId, checkInterval = 5000, reconnectDelay = 3000) {
|
|
constructor(url, liveId, userId, checkInterval = 5000, reconnectDelay = 3000) {
|
|
|
- let timestamp = new Date().getTime()
|
|
|
|
|
- let userType = 1
|
|
|
|
|
- let signature = CryptoJS.HmacSHA256(
|
|
|
|
|
- CryptoJS.enc.Utf8.parse('' + liveId + userId + userType + timestamp),
|
|
|
|
|
- CryptoJS.enc.Utf8.parse(timestamp)).toString(CryptoJS.enc.Hex)
|
|
|
|
|
- this.url = url + `?liveId=${liveId}&userId=${userId}&userType=${userType}×tamp=${timestamp}&signature=${signature}`;
|
|
|
|
|
|
|
+ this.baseUrl = url;
|
|
|
this.liveId = liveId;
|
|
this.liveId = liveId;
|
|
|
this.userId = userId;
|
|
this.userId = userId;
|
|
|
this.checkInterval = checkInterval;
|
|
this.checkInterval = checkInterval;
|
|
@@ -31,16 +26,28 @@ export class LiveWS {
|
|
|
this.heartbeatTimer = null;
|
|
this.heartbeatTimer = null;
|
|
|
this.isManualClose = false;
|
|
this.isManualClose = false;
|
|
|
this._messageHandlers = new Set();
|
|
this._messageHandlers = new Set();
|
|
|
|
|
+ this.refreshSignedUrl();
|
|
|
this.connect();
|
|
this.connect();
|
|
|
this.startHeartbeat();
|
|
this.startHeartbeat();
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ /** 每次建连/重连刷新 timestamp+signature,避免验签过期 */
|
|
|
|
|
+ refreshSignedUrl() {
|
|
|
|
|
+ const timestamp = new Date().getTime();
|
|
|
|
|
+ const userType = 1;
|
|
|
|
|
+ const signature = CryptoJS.HmacSHA256(
|
|
|
|
|
+ CryptoJS.enc.Utf8.parse('' + this.liveId + this.userId + userType + timestamp),
|
|
|
|
|
+ CryptoJS.enc.Utf8.parse(String(timestamp))).toString(CryptoJS.enc.Hex);
|
|
|
|
|
+ this.url = this.baseUrl + `?liveId=${this.liveId}&userId=${this.userId}&userType=${userType}×tamp=${timestamp}&signature=${signature}`;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
connect() {
|
|
connect() {
|
|
|
// 如果已经有一个连接处于 OPEN 或 CONNECTING 状态,则不再创建新连接
|
|
// 如果已经有一个连接处于 OPEN 或 CONNECTING 状态,则不再创建新连接
|
|
|
if (this.ws && (this.ws.readyState === WebSocket.OPEN || this.ws.readyState === WebSocket.CONNECTING)) {
|
|
if (this.ws && (this.ws.readyState === WebSocket.OPEN || this.ws.readyState === WebSocket.CONNECTING)) {
|
|
|
return;
|
|
return;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ this.refreshSignedUrl();
|
|
|
this.ws = new WebSocket(this.url);
|
|
this.ws = new WebSocket(this.url);
|
|
|
|
|
|
|
|
// 绑定事件
|
|
// 绑定事件
|