123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282 |
- /**
- * 资源加载控制器
- */
- cc.Class({
- extends: cc.Component,
- editor: {
- menu: 'Ctrl/ReconnCtrl'
- },
- properties: {
- },
- onLoad () {
- cc.game.on('e_socket_on_logined', this._handleSocketOnLogined, this);
- cc.game.on('e_socket_on_closed', this._handleSocketOnClosed, this);
- },
- onDestroy () {
- cc.game.targetOff(this);
- },
- _handleSocketOnLogined () {
- G.AppUtils.getSceneCtrl().loadingCtrl.hideTips();
- let antiaddictionInfo = G.AntiaddictionMgr.getAntiaddictionInfoForTime();
- if (antiaddictionInfo) {
- G.AppUtils.getSceneCtrl().showAntiaddiction(antiaddictionInfo);
- return;
- }
- if (G.SingleRoomMgr.isOpen()) {
- // 有单机场
- G.AppUtils.getSceneCtrl().closeAlert(JMC.ALERT_ID.HOME_NETWORK_ANOMALY);
- } else {
- // 无单机场
- if (G.AppUtils.getSceneCtrl().sceneName == '大厅') {
- G.AppUtils.getSceneCtrl().closeAlert(JMC.ALERT_ID.HOME_SCENE_NETWORK_ANOMALY);
- } else {
- G.AppUtils.getSceneCtrl().closeAlert(JMC.ALERT_ID.OTHER_SCENE_NETWORK_ANOMALY);
- }
- }
- },
- _handleSocketOnClosed (eventKey) {
- if (eventKey == 'call_close') {
- // 主动断开网络,不重连
- return;
- }
- if (G.LoginMgr.reconnectTimes == 0) {
- // 第一次断线直接重连socket
- G.AppUtils.getSceneCtrl().loadingCtrl.showTips('连接中...');
- this.scheduleOnce(() => {
- if (!G.LoginMgr.isOnline) {
- this._handleSocketOnClosed('call_reconnect');
- }
- }, 5);
- this.scheduleOnce(() => {
- // 重连计数
- G.LoginMgr.reconnectTimes += 1;
- // 重连
- G.NetworkMgr.reconnectSocket();
- }, 1);
- return;
- }
- if (G.LoginMgr.reconnectTimes >= G.LoginMgr.reconnectMaxTimes) {
- // 重连次数过多,让玩家回登录页走登录流程
- this.showServerMaintain(JMC.ALERT_ID.SERVER_MAINTAIN_ALERT);
- return;
- }
- if (G.SingleRoomMgr.isOpen()) {
- // 有单机场
- this.showNetworkAnomalySingle(JMC.ALERT_ID.HOME_NETWORK_ANOMALY);
- } else {
- // 无单机场
- if (G.AppUtils.getSceneCtrl().sceneName == '大厅') {
- this.showNetworkAnomaly(JMC.ALERT_ID.HOME_SCENE_NETWORK_ANOMALY);
- } else {
- this.showNetworkAnomaly(JMC.ALERT_ID.OTHER_SCENE_NETWORK_ANOMALY);
- }
- }
- },
- showServerMaintain (alertId) {
- if (G.AppUtils.getSceneCtrl().getAlert(alertId)) {
- return;
- }
- G.AppUtils.getSceneCtrl().popAllAlert();
- let cb = (alert, eventKey, eventData) => {
- switch (eventKey) {
- case 'clickedRight': {
- G.AppUtils.loadScene('scene_login');
- break;
- }
- case 'willShow': {
- // 兼容代码
- // 避免出现两个网络异常弹窗
- G.AppUtils.getSceneCtrl().popAllAlert(true);
- // ZIndex 设置100避免popAllAlert关闭
- alert.alertZIndex = 100;
- break;
- }
- default: {
- alert.alertDefaultCallback(eventKey, eventData);
- break;
- }
- }
- };
- let alertData = {};
- G.AppUtils.getSceneCtrl().showNormalAlert(alertId, alertData, cb);
- },
- showNetworkAnomaly (alertId) {
- if (G.AppUtils.getSceneCtrl().getAlert(alertId)) {
- return;
- }
- G.AppUtils.getSceneCtrl().popAllAlert();
- let cb = (alert, eventKey, eventData) => {
- switch (eventKey) {
- case 'clickedLeft': {
- G.AppUtils.loadScene('scene_home');
- break;
- }
- case 'clickedRight': {
- this.reconnect(alert);
- break;
- }
- case 'willShow': {
- // 兼容代码
- // 避免出现两个网络异常弹窗
- G.AppUtils.getSceneCtrl().popAllAlert(true);
- // ZIndex 设置100避免popAllAlert关闭
- alert.alertZIndex = 100;
- break;
- }
- default: {
- alert.alertDefaultCallback(eventKey, eventData);
- break;
- }
- }
- };
- let alertData = {};
- G.AppUtils.getSceneCtrl().showNormalAlert(alertId, alertData, cb);
- },
- showNetworkAnomalySingle (alertId) {
- if (G.AppUtils.getSceneCtrl().getAlert(alertId)) {
- return;
- }
- G.AppUtils.getSceneCtrl().popAllAlert();
- let cb = (alert, eventKey, eventData) => {
- switch (eventKey) {
- case 'clickedLeft': {
- G.AppUtils.loadScene('scene_room_single');
- break;
- }
- case 'clickedRight': {
- this.reconnect(alert);
- break;
- }
- case 'willShow': {
- // 兼容代码
- // 避免出现两个网络异常弹窗
- G.AppUtils.getSceneCtrl().popAllAlert(true);
- // ZIndex 设置100避免popAllAlert关闭
- alert.alertZIndex = 100;
- break;
- }
- default: {
- alert.alertDefaultCallback(eventKey, eventData);
- break;
- }
- }
- };
- let alertData = {};
- G.AppUtils.getSceneCtrl().showNormalAlert(alertId, alertData, cb);
- },
- reconnect (alert) {
- if (G.MiddleDevice.getApnType() == -1) {
- if (G.SingleRoomMgr.isOpen()) {
- G.AppUtils.getSceneCtrl().addToast('当前未连接网络或网络异常,可先去单机挑战一下哦');
- } else {
- G.AppUtils.getSceneCtrl().addToast('当前未连接网络或网络异常');
- }
- return;
- }
- let currTime = G.TimeUtils.getCurrentTime();
- let loginTimestamp = G.LoginMgr.loginTimestamp;
- if (!cc.sys.isNative && loginTimestamp && currTime - loginTimestamp >= 12 * 60 * 60) {
- // 非原生平台并且登录时间大于12小时
- // 回到授权场景
- G.AppUtils.loadScene('scene_login');
- return;
- } else {
- if (G.LoginMgr.loginTimestamp) {
- // 本次启动登录成功过
- let uid = G.LoginMgr.getStorageValue('uid');
- let password = G.LoginMgr.getStorageValue('password');
- let isGuest = !G.LoginMgr.isAuthSuccess();
- G.LoginMgr.requestLogin(uid, password, isGuest);
- // 重连计数
- G.LoginMgr.reconnectTimes += 1;
- G.AppUtils.getSceneCtrl().loadingCtrl.showTips('连接中...');
- this.scheduleOnce(() => {
- if (!G.LoginMgr.isOnline) {
- this._handleSocketOnClosed('call_reconnect');
- }
- }, 5);
- alert.close();
- } else {
- // 回到授权场景
- G.AppUtils.loadScene('scene_login');
- return;
- }
- }
- },
- // 处理离线
- handleOffline () {
- if (G.MiddleDevice.getApnType() == -1) {
- // 无网络需要弹出提示
- if (G.SingleRoomMgr.isOpen()) {
- G.AppUtils.getSceneCtrl().addToast('当前未连接网络或网络异常,可先去单机挑战一下哦');
- } else {
- G.AppUtils.getSceneCtrl().addToast('当前未连接网络或网络异常');
- }
- } else {
- if (G.LoginMgr.loginTimestamp) {
- // 登录成功过
- G.AppUtils.getSceneCtrl().loadingCtrl.showTips('连接中...', 5, () => {
- if (!G.LoginMgr.isOnline) {
- // reconnectTimes = 1 避免再次自动重连
- G.LoginMgr.reconnectTimes = 1;
- this._handleSocketOnClosed('call_reconnect');
- }
- });
- if (!G.NetworkMgr.isConnecting()) {
- // websocket为空的时候,发起重连
- // 解决回到大厅没有网络卡 连接中... 的问题
- G.NetworkMgr.reconnectSocket();
- }
- } else {
- // 无网络进入游戏
- // reconnectTimes = 1 避免再次自动重连
- G.LoginMgr.reconnectTimes = 1;
- this._handleSocketOnClosed('call_reconnect');
- }
- }
- },
- // 弹出断线重连弹窗
- showOfflineAlert () {
- if (G.SingleRoomMgr.isOpen()) {
- // 有单机场
- this.showNetworkAnomalySingle(JMC.ALERT_ID.HOME_NETWORK_ANOMALY);
- } else {
- // 无单机场
- if (G.AppUtils.getSceneCtrl().sceneName == '大厅') {
- this.showNetworkAnomaly(JMC.ALERT_ID.HOME_SCENE_NETWORK_ANOMALY);
- } else {
- this.showNetworkAnomaly(JMC.ALERT_ID.OTHER_SCENE_NETWORK_ANOMALY);
- }
- }
- }
- });
|