Fixed frontend reconnection on page refresh for Firefox and Safari.

This commit is contained in:
genxium 2022-10-25 09:52:38 +08:00
parent fe826b393b
commit 6d075877ec
3 changed files with 34 additions and 38 deletions

View File

@ -440,7 +440,7 @@
"array": [ "array": [
0, 0,
0, 0,
344.75930058781137, 216.05530045313827,
0, 0,
0, 0,
0, 0,

View File

@ -472,7 +472,7 @@ cc.Class({
pts.push([boundaryObj[i].x - x0, boundaryObj[i].y - y0]); pts.push([boundaryObj[i].x - x0, boundaryObj[i].y - y0]);
} }
const newBarrierLatest = self.latestCollisionSys.createPolygon(x0, y0, pts); const newBarrierLatest = self.latestCollisionSys.createPolygon(x0, y0, pts);
console.log("Created barrier: ", newBarrierLatest); // console.log("Created barrier: ", newBarrierLatest);
const newBarrierChaser = self.chaserCollisionSys.createPolygon(x0, y0, pts); const newBarrierChaser = self.chaserCollisionSys.createPolygon(x0, y0, pts);
++barrierIdCounter; ++barrierIdCounter;
const collisionBarrierIndex = (self.collisionBarrierIndexPrefix + barrierIdCounter); const collisionBarrierIndex = (self.collisionBarrierIndexPrefix + barrierIdCounter);

View File

@ -131,23 +131,22 @@ window.initPersistentSessionClient = function(onopenCb, expectedRoomId) {
const currentHistoryState = window.history && window.history.state ? window.history.state : {}; const currentHistoryState = window.history && window.history.state ? window.history.state : {};
window.clientSession = null; // Important for checking whether the "onclose" event is still relevant!
const clientSession = new WebSocket(urlToConnect); const clientSession = new WebSocket(urlToConnect);
clientSession.binaryType = 'arraybuffer'; // Make 'event.data' of 'onmessage' an "ArrayBuffer" instead of a "Blob" clientSession.binaryType = 'arraybuffer'; // Make 'event.data' of 'onmessage' an "ArrayBuffer" instead of a "Blob"
clientSession.onopen = function(event) { clientSession.onopen = function(evt) {
console.log("The WS clientSession is opened. clientSession.id=", clientSession.id); console.log("The WS clientSession is opened. clientSession.id=", clientSession.id);
window.clientSession = clientSession; window.clientSession = clientSession;
if (null == onopenCb) return; if (null == onopenCb) return;
onopenCb(); onopenCb();
}; };
clientSession.onmessage = function(event) { clientSession.onmessage = function(evt) {
if (null == event || null == event.data) { if (null == evt || null == evt.data) {
return; return;
} }
try { try {
const resp = window.WsResp.decode(new Uint8Array(event.data)); const resp = window.WsResp.decode(new Uint8Array(evt.data));
switch (resp.act) { switch (resp.act) {
case window.DOWNSYNC_MSG_ACT_HB_REQ: case window.DOWNSYNC_MSG_ACT_HB_REQ:
window.handleHbRequirements(resp); // 获取boundRoomId并存储到localStorage window.handleHbRequirements(resp); // 获取boundRoomId并存储到localStorage
@ -194,47 +193,44 @@ window.initPersistentSessionClient = function(onopenCb, expectedRoomId) {
break; break;
} }
} catch (e) { } catch (e) {
console.error("Unexpected error when parsing data of:", event.data, e); console.error("Unexpected error when parsing data of:", evt.data, e);
} }
}; };
clientSession.onerror = function(event) { clientSession.onerror = function(evt) {
console.error("Error caught on the WS clientSession: ", event); console.error("Error caught on the WS clientSession: ", evt);
if (window.clientSessionPingInterval) { if (window.handleClientSessionError) {
clearInterval(window.clientSessionPingInterval); window.handleClientSessionError();
}
if (window.handleClientSessionCloseOrError) {
window.handleClientSessionCloseOrError();
} }
}; };
clientSession.onclose = function(event) { clientSession.onclose = function(evt) {
if (null == window.clientSession) { // [WARNING] The callback "onclose" might be called AFTER the webpage is refreshed with "1001 == evt.code".
console.log("Received an outdated WS clientSession onclose event: ", event, clientSession); console.warn("The WS clientSession is closed: ", evt, clientSession);
return; if (false == evt.wasClean) {
} /*
console.warn("The WS clientSession is closed: ", event, clientSession); Chrome doesn't allow the use of "CustomCloseCode"s (yet) and will callback with a "WebsocketStdCloseCode 1006" and "false == evt.wasClean" here. See https://tools.ietf.org/html/rfc6455#section-7.4 for more information.
if (window.clientSessionPingInterval) { */
clearInterval(window.clientSessionPingInterval); if (window.handleClientSessionError) {
} window.handleClientSessionError();
if (false == event.wasClean) {
// Chrome doesn't allow the use of "CustomCloseCode"s (yet) and will callback with a "WebsocketStdCloseCode 1006" and "false == event.wasClean" here. See https://tools.ietf.org/html/rfc6455#section-7.4 for more information.
if (window.handleClientSessionCloseOrError) {
window.handleClientSessionCloseOrError();
} }
} else { } else {
switch (event.code) { switch (evt.code) {
case constants.RET_CODE.PLAYER_NOT_ADDABLE_TO_ROOM:
case constants.RET_CODE.PLAYER_NOT_READDABLE_TO_ROOM:
window.clearBoundRoomIdInBothVolatileAndPersistentStorage();
break;
case constants.RET_CODE.UNKNOWN_ERROR:
case constants.RET_CODE.MYSQL_ERROR:
case constants.RET_CODE.PLAYER_NOT_FOUND: case constants.RET_CODE.PLAYER_NOT_FOUND:
case constants.RET_CODE.PLAYER_CHEATING: case constants.RET_CODE.PLAYER_CHEATING:
window.clearBoundRoomIdInBothVolatileAndPersistentStorage(); if (window.handleClientSessionError) {
window.handleClientSessionError();
}
break; break;
default: default:
break; break;
} }
if (window.handleClientSessionCloseOrError) {
window.handleClientSessionCloseOrError();
}
} }
}; };
}; };
@ -246,17 +242,17 @@ window.clearLocalStorageAndBackToLoginScene = function(shouldRetainBoundRoomIdIn
window.mapIns.musicEffectManagerScriptIns.stopAllMusic(); window.mapIns.musicEffectManagerScriptIns.stopAllMusic();
} }
/** /**
* Here I deliberately removed the callback in the "common `handleClientSessionCloseOrError` callback" * Here I deliberately removed the callback in the "common `handleClientSessionError` callback"
* within which another invocation to `clearLocalStorageAndBackToLoginScene` will be made. * within which another invocation to `clearLocalStorageAndBackToLoginScene` will be made.
* *
* It'll be re-assigned to the common one upon reentrance of `Map.onLoad`. * It'll be re-assigned to the common one upon reentrance of `Map.onLoad`.
* *
* -- YFLu 2019-04-06 * -- YFLu 2019-04-06
*/ */
window.handleClientSessionCloseOrError = () => { window.handleClientSessionError = () => {
console.warn("+++++++ Special handleClientSessionCloseOrError() assigned within `clearLocalStorageAndBackToLoginScene`"); console.warn("+++++++ Special handleClientSessionError() assigned within `clearLocalStorageAndBackToLoginScene`");
// TBD. // TBD.
window.handleClientSessionCloseOrError = null; // To ensure that it's called at most once. window.handleClientSessionError = null; // To ensure that it's called at most once.
}; };
window.closeWSConnection(); window.closeWSConnection();
window.clearSelfPlayer(); window.clearSelfPlayer();