提交 b15524bf 编写于 作者: ItbGcthate's avatar ItbGcthate

304_fix feature websockcet

上级 2061e024
...@@ -47,6 +47,7 @@ class TwoPlayerPKHome extends Component { ...@@ -47,6 +47,7 @@ class TwoPlayerPKHome extends Component {
joinMatch = () => { joinMatch = () => {
this.setState({isStarted: true, isCanceling: false}); this.setState({isStarted: true, isCanceling: false});
console.log('websocket 已打开'); console.log('websocket 已打开');
// console.log(websocket.get().readyState);
websocket.joinMatch(); websocket.joinMatch();
}; };
......
...@@ -14,7 +14,7 @@ export function login() { ...@@ -14,7 +14,7 @@ export function login() {
const resolveData = data => { const resolveData = data => {
console.log(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(); post(URL_LOGIN, data).then(resolveData).catch();
} }
...@@ -22,7 +22,7 @@ export function logout() { ...@@ -22,7 +22,7 @@ export function logout() {
const resolveData = data => { const resolveData = data => {
console.log(data); console.log(data);
}; };
console.log('[Logout]'); // console.log('[Logout]');
get(URL_LOGOUT, {}).then(resolveData).catch(); get(URL_LOGOUT, {}).then(resolveData).catch();
} }
......
...@@ -87,55 +87,57 @@ export class websocket { ...@@ -87,55 +87,57 @@ export class websocket {
static connect(functions) { static connect(functions) {
const url = URL_2PLAYER_PK_MATCH + sno.get(); const url = URL_2PLAYER_PK_MATCH + sno.get();
console.log(`[url_2player_PK_match] ${url}`); // console.log(`[url_2player_PK_match] ${url}`);
getCookies().then(cookies => { getCookies()
console.log('[cookies] ', cookies); .then(cookies => {
let cookie = ''; // console.log('[cookies] ', cookies);
for (let key in cookies) { let cookie = '';
cookie += `${cookies[key].name}=${cookies[key].value}&`; for (let key in cookies) {
} cookie += `${cookies[key].name}=${cookies[key].value}&`;
cookie = cookie.substring(0, cookie.length - 1); }
this.set( cookie = cookie.substring(0, cookie.length - 1);
new WebSocket(url, null, { this.set(
headers: { new WebSocket(url, null, {
cookie: cookie, headers: {
}, cookie: cookie,
}), },
); }),
//打开事件 );
if (functions.hasOwnProperty('onopen')) { //打开事件
this.get().onopen = functions.onopen; if (functions.hasOwnProperty('onopen')) {
} else { this.get().onopen = functions.onopen;
this.get().onopen = function () { } else {
console.log('websocket 已打开'); this.get().onopen = function () {
}; console.log('websocket 已打开');
} };
//获得消息事件 }
if (functions.hasOwnProperty('onmessage')) { //获得消息事件
this.get().onmessage = functions.onmessage; if (functions.hasOwnProperty('onmessage')) {
} else { this.get().onmessage = functions.onmessage;
this.get().onmessage = function (msg) { } else {
const serverMsg = '收到服务端信息: ' + msg.data; this.get().onmessage = function (msg) {
console.log(serverMsg); const serverMsg = '收到服务端信息: ' + msg.data;
}; console.log(serverMsg);
} };
//关闭事件 }
if (functions.hasOwnProperty('onclose')) { //关闭事件
this.get().onclose = functions.onclose; if (functions.hasOwnProperty('onclose')) {
} else { this.get().onclose = functions.onclose;
this.get().onclose = function () { } else {
console.log('websocket 已关闭'); this.get().onclose = function () {
}; console.log('websocket 已关闭');
} };
//发生了错误事件 }
if (functions.hasOwnProperty('onerror')) { //发生了错误事件
this.get().onerror = functions.onerror; if (functions.hasOwnProperty('onerror')) {
} else { this.get().onerror = functions.onerror;
this.get().onerror = function () { } else {
console.log('websocket 发生了错误'); this.get().onerror = function () {
}; console.log('websocket 发生了错误');
} };
}); }
})
.catch();
} }
//打开事件 //打开事件
...@@ -332,7 +334,7 @@ export function post(url, data = {}) { ...@@ -332,7 +334,7 @@ export function post(url, data = {}) {
async function getCookies() { async function getCookies() {
return await CookieManager.get(URL_SHORT).then(res => { return await CookieManager.get(URL_SHORT).then(res => {
console.log('CookieManager.get =>', res); // console.log('CookieManager.get =>', res);
return res; return res;
}); });
} }
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册