From b15524bf3100df9810669c26e48b65f48a5698f1 Mon Sep 17 00:00:00 2001 From: 221900304 <1480466053@qq.com> Date: Tue, 10 May 2022 00:03:39 +0800 Subject: [PATCH] 304_fix feature websockcet --- component/OnlineStudy/2-PlayerPKHome/index.js | 1 + component/OnlineStudy/MyTest/index.js | 4 +- component/OnlineStudy/MyUtilities/index.js | 102 +++++++++--------- 3 files changed, 55 insertions(+), 52 deletions(-) diff --git a/component/OnlineStudy/2-PlayerPKHome/index.js b/component/OnlineStudy/2-PlayerPKHome/index.js index 7d46b4a..18d183c 100644 --- a/component/OnlineStudy/2-PlayerPKHome/index.js +++ b/component/OnlineStudy/2-PlayerPKHome/index.js @@ -47,6 +47,7 @@ class TwoPlayerPKHome extends Component { joinMatch = () => { this.setState({isStarted: true, isCanceling: false}); console.log('websocket 已打开'); + // console.log(websocket.get().readyState); websocket.joinMatch(); }; diff --git a/component/OnlineStudy/MyTest/index.js b/component/OnlineStudy/MyTest/index.js index 465b08c..ee8dbff 100644 --- a/component/OnlineStudy/MyTest/index.js +++ b/component/OnlineStudy/MyTest/index.js @@ -14,7 +14,7 @@ export function login() { const resolveData = data => { console.log(data); }; - console.log('[Login] sno: ' + data.sno + ' password: ' + data.password); + // console.log('[Login] sno: ' + data.sno + ' password: ' + data.password); post(URL_LOGIN, data).then(resolveData).catch(); } @@ -22,7 +22,7 @@ export function logout() { const resolveData = data => { console.log(data); }; - console.log('[Logout]'); + // console.log('[Logout]'); get(URL_LOGOUT, {}).then(resolveData).catch(); } diff --git a/component/OnlineStudy/MyUtilities/index.js b/component/OnlineStudy/MyUtilities/index.js index c3169eb..aabd9e2 100644 --- a/component/OnlineStudy/MyUtilities/index.js +++ b/component/OnlineStudy/MyUtilities/index.js @@ -87,55 +87,57 @@ export class websocket { static connect(functions) { const url = URL_2PLAYER_PK_MATCH + sno.get(); - console.log(`[url_2player_PK_match] ${url}`); - getCookies().then(cookies => { - console.log('[cookies] ', cookies); - let cookie = ''; - for (let key in cookies) { - cookie += `${cookies[key].name}=${cookies[key].value}&`; - } - cookie = cookie.substring(0, cookie.length - 1); - this.set( - new WebSocket(url, null, { - headers: { - cookie: cookie, - }, - }), - ); - //打开事件 - if (functions.hasOwnProperty('onopen')) { - this.get().onopen = functions.onopen; - } else { - this.get().onopen = function () { - console.log('websocket 已打开'); - }; - } - //获得消息事件 - if (functions.hasOwnProperty('onmessage')) { - this.get().onmessage = functions.onmessage; - } else { - this.get().onmessage = function (msg) { - const serverMsg = '收到服务端信息: ' + msg.data; - console.log(serverMsg); - }; - } - //关闭事件 - if (functions.hasOwnProperty('onclose')) { - this.get().onclose = functions.onclose; - } else { - this.get().onclose = function () { - console.log('websocket 已关闭'); - }; - } - //发生了错误事件 - if (functions.hasOwnProperty('onerror')) { - this.get().onerror = functions.onerror; - } else { - this.get().onerror = function () { - console.log('websocket 发生了错误'); - }; - } - }); + // console.log(`[url_2player_PK_match] ${url}`); + getCookies() + .then(cookies => { + // console.log('[cookies] ', cookies); + let cookie = ''; + for (let key in cookies) { + cookie += `${cookies[key].name}=${cookies[key].value}&`; + } + cookie = cookie.substring(0, cookie.length - 1); + this.set( + new WebSocket(url, null, { + headers: { + cookie: cookie, + }, + }), + ); + //打开事件 + if (functions.hasOwnProperty('onopen')) { + this.get().onopen = functions.onopen; + } else { + this.get().onopen = function () { + console.log('websocket 已打开'); + }; + } + //获得消息事件 + if (functions.hasOwnProperty('onmessage')) { + this.get().onmessage = functions.onmessage; + } else { + this.get().onmessage = function (msg) { + const serverMsg = '收到服务端信息: ' + msg.data; + console.log(serverMsg); + }; + } + //关闭事件 + if (functions.hasOwnProperty('onclose')) { + this.get().onclose = functions.onclose; + } else { + this.get().onclose = function () { + console.log('websocket 已关闭'); + }; + } + //发生了错误事件 + if (functions.hasOwnProperty('onerror')) { + this.get().onerror = functions.onerror; + } else { + this.get().onerror = function () { + console.log('websocket 发生了错误'); + }; + } + }) + .catch(); } //打开事件 @@ -332,7 +334,7 @@ export function post(url, data = {}) { async function getCookies() { return await CookieManager.get(URL_SHORT).then(res => { - console.log('CookieManager.get =>', res); + // console.log('CookieManager.get =>', res); return res; }); } -- GitLab