Commit 5986b19e by kaisa

feat:更新

parent 3048b056
Showing with 0 additions and 90 deletions
import { usePkStore } from '@/stores/pkStore';
const pkStore = usePkStore();
function url_get_params(url_path) {
let url = url_path;
let theRequest = new Object();
if (url.indexOf("?") != -1) {
let str = url.substr(url.indexOf("?") + 1);
strs = str.split("&");
for (let i = 0; i < strs.length; i++) {
theRequest[strs[i].split("=")[0]] = unescape(strs[i].split("=")[1]);
}
}
return theRequest;
}
let wsTimer = null;
let wsReconnect = null;
let mUrlParam = url_get_params(location.href);
pkStore.setMurlParam(mUrlParam);
console.log(mUrlParam, 'mUrlParam');
let ws = null;
export function WebSocketGo() {
/* 測試: ws://47.75.50.13:5300/ws */
/* 正式: wss://firefly-cloud-websocket.yabolive.net/ws */
ws = new WebSocket(
`${import.meta.env.VITE_APP_WS_URL}?type=4&uid=` +
mUrlParam.uid +
"&token=debug" +
"&roomid=" +
mUrlParam.roomId +
"&lang=" +
mUrlParam.lang
);
console.log("建立链接");
pkStore.setWsLocation("WS建立连接中...")
ws.onopen = function () {
clearInterval(wsReconnect);
clearInterval(wsTimer);
wsTimer = setInterval(function () {
heartbeat();
}, 4500);
};
ws.onmessage = function (event) {
let msg = JSON.parse(event.data);
console.log("推流", msg);
// 存储游戏推流数据
if (msg.content) {
const parseData = JSON.parse(msg.content);
if (parseData.wsdata) {
pkStore.setGameData(JSON.parse(msg.content).wsdata);
}
}
};
ws.onclose = function (error) {
clearInterval(wsReconnect);
wsReconnect = setInterval(function () {
console.log("断了", error);
pkStore.setWsLocation("WS断开连接...")
WebSocketGo();
}, 3000);
};
function heartbeat() {
if (ws.readyState == WebSocket.OPEN) {
pkStore.setWsLocation("WS发送心跳...")
var msg = new WsMsg(1000, "ping", 0, mUrlParam.uid);
ws.send(JSON.stringify(msg));
}
}
function WsMsg(cid, content, extend, uid) {
this.time = new Date().getTime();
this.msgid = Math.random().toString(36).slice(-8);
this.cid = cid;
this.content = content;
this.extend = extend;
this.uid = uid;
}
}
export function closeWebSocket() {
if (ws) {
clearInterval(wsTimer);
clearInterval(wsReconnect);
ws.close();
ws = null;
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment