瀏覽代碼

no message

zhejiushizhuce 2 年之前
父節點
當前提交
256630d8f4

+ 9 - 5
src/App.js

@@ -219,14 +219,15 @@ const App = () => {
     const onCloseHallWheel=()=>{
         checkOpenCheckin();
     }
+    //app Effect
     //初始化
     useEffect(() => {
         appInitData();
-
         return ()=>{
 
         }
     }, []);
+    //用户信息改变
     useEffect(()=>{
         reqInitData();
     },[userID])
@@ -245,6 +246,7 @@ const App = () => {
         }
         checkOpenCheckin();
     },[turnplate]);
+    //监听上一次的路由
     useEffect(()=>{
         if (preLocation){
             let pathname=preLocation.pathname;
@@ -255,19 +257,21 @@ const App = () => {
                 }
             }
         }
-    },[preLocation,gameID])
+    },[preLocation])
+    //监听gameID
     useEffect(()=>{
         dispatch(updateCurrGame(gameID))
         dispatch(hallSendState());
     },[gameID])
+    //监听被服务器踢掉
     useEffect(()=>{
-        console.log("serverKick___________"+serverKick)
         if (serverKick===1){
             if (pathname.startsWith(ROUTE_PATH.GAME)){
-                navigate("/");
+                navigate(ROUTE_PATH.HOME,{replace:true});
             }
         }
-    },serverKick)
+    },[serverKick])
+    //app Effect
 
     const renderLayout=()=>{
         return (

+ 26 - 12
src/components/MsgBox/index.jsx

@@ -16,10 +16,12 @@ class MsgBox extends Component{
              title:"",
              content:'',
              sureLabel:'',
-             cancelLabel:''
+             cancelLabel:'',
+             onlySure:false,
+             isLockClose:false,
             })
     }
-    show({title='',content='',sureCB=null,cancelCB=null,sureLabel='',cancelLabel=''}){
+    show({title='',content='',sureCB=null,cancelCB=null,sureLabel='',cancelLabel='',onlySure=false,isLockClose=false}){
         if (title==='')title=i18n.t('msgbox.title');
         if (sureLabel==='')sureLabel=i18n.t('msgbox.sure_label');
         if (cancelLabel==='')cancelLabel=i18n.t('msgbox.cancel_label');
@@ -30,7 +32,9 @@ class MsgBox extends Component{
             title:title,
             content:content,
             sureLabel:sureLabel,
-            cancelLabel:cancelLabel
+            cancelLabel:cancelLabel,
+            onlySure:onlySure,
+            isLockClose:isLockClose
         })
     }
     hide(){
@@ -39,13 +43,18 @@ class MsgBox extends Component{
         })
     }
     onClickBtn(type){
-        if (type==0){
-            if (this.cancelCB!=null)this.cancelCB();
-        }
-        else if (type==1){
+        // if (type==0){
+        //     if (this.cancelCB!=null)this.cancelCB();
+        // }
+        if (type===1){
             if (this.sureCB!=null)this.sureCB();
+            this.hide();
+        }
+        else
+        {
+            if (this.cancelCB!=null)this.cancelCB();
+            if (!this.state.isLockClose)this.hide();
         }
-        this.hide();
     }
     closeMsgBox(){
         this.onClickBtn(0);
@@ -65,11 +74,16 @@ class MsgBox extends Component{
                                         <div className={styles.title}>{this.state.title}</div>
                                         <div className={styles.content}>{this.state.content}</div>
                                         <div className={styles.msg_btn}>
-                                            <button className={"button-gray"} onClick={this.closeMsgBox.bind(this)}>
-                                                {this.state.cancelLabel}
-                                            </button>
+                                            {
+                                                this.state.onlySure?(<></>): (
+                                                    <button className={"button-gray"}
+                                                            onClick={this.closeMsgBox.bind(this)}>
+                                                        {this.state.cancelLabel}
+                                                    </button>
+                                                )
+                                            }
                                             <button className={"button-green"} onClick={this.onClickSure.bind(this)}>
-                                                {this.state.sureLabel}
+                                            {this.state.sureLabel}
                                             </button>
                                         </div>
                                     </div>

+ 1 - 0
src/constants/routes.js

@@ -12,6 +12,7 @@ const SIDE_TYPE_SPECIAL=2;
 
 const ROUTE_PATH={
     BACK:-1,
+    HOME:'/',
     SIGN_IN:"#/signin",
     SIGN_UP:"#/signup",
     SETTINGS:"#/settings",

+ 66 - 22
src/features/actions/hallClient.js

@@ -20,6 +20,34 @@ const handlePayFinish=(message,dispatch)=>{
     let {data}=message;
     if (data&&data.PayNum>=200)global.showDropCoins();
 }
+const handleKick=(message,dispatch)=>{
+    let onSure=()=>{
+        dispatch(updateServerKick(0));
+        dispatch(connectHall());
+    }
+    let data={
+        content:i18n.t("hall.offline"),
+        sureCB:onSure,
+        onlySure: true,
+        isLockClose:true,
+        sureLabel:i18n.t("hall.reconnect"),
+    }
+    global.msgBoxShow(data)
+    dispatch(updateServerKick(1));
+}
+const handleHallTimeout=(message,dispatch)=>{
+    let onSure=()=>{
+        dispatch(connectHall());
+    }
+    let data={
+        content:i18n.t("hall.disconnected"),
+        sureCB:onSure,
+        onlySure: true,
+        isLockClose:true,
+        sureLabel:i18n.t("hall.reconnect"),
+    }
+    global.msgBoxShow(data)
+}
 const roger=(message,dispatch)=>{
     // console.log(message);
     if (message){
@@ -50,34 +78,40 @@ const roger=(message,dispatch)=>{
                 }
                 break;
             }
-            //踢
-            case "kick":{
-                // send("operate",{cmd:"kick",data:{reason:"hall.other_online"}});
-                if(!!message.data){
-                    let {data}=message;
-                    let reason=data.reason;
-                    if (reason!=null){
-                        toast.error(i18n.t("hall.offline"));
-                    }
-                }
-                dispatch(updateServerKick(1));
-            }
         }
     }
 }
 const chat=(message,dispatch)=>{
-    console.log(message);
+
+}
+const operate=(message,dispatch)=>{
+    let {cmd}=message;
+    switch (cmd){
+        case "id":{
+            dispatch(hallSendState());
+            break;
+        }
+        case "state":{
+            break;
+        }
+        //踢
+        case "kick":{
+            // send("operate",{cmd:"kick",data:{reason:"hall.other_online"}});
+            handleKick(message,dispatch);
+            break;
+        }
+    }
 }
 //信息处理
 const messageHandler={
     __playground_message_types,
     roger,
-    chat
+    chat,
+    operate
 }
 const onHallConnect=()=>{
     return (dispatch, getState) => {
         dispatch(hallSendReg());
-        dispatch(hallSendState());
         // dispatch(hallSendState(0));
         // dispatch(hallSendChat("123456"));
         // setInterval(()=>{
@@ -85,6 +119,11 @@ const onHallConnect=()=>{
         // },1000);
     }
 }
+const onHallTimeoutClose=()=>{
+    return (dispatch, getState) => {
+        handleHallTimeout("",dispatch);
+    }
+}
 const send=(type,message,dispatch)=>{
     dispatch(sendToServer({
         roomName,
@@ -102,19 +141,24 @@ export const onHallMessage=(type,message)=>{
 }
 export const connectHall=(hallServerUrl='')=>{
     if (hallServerUrl==='')hallServerUrl=serverUrl;
-    return connectServer({
-        roomName:roomName,
-        serverUrl:hallServerUrl,
-        messageCB:onHallMessage,
-        onRoomConnect:onHallConnect
-    });
+    return (dispatch, getState) => {
+        let state = getState();
+        let url=state.main.hallServerUrl;
+        if (url==='')url=serverUrl;
+        dispatch(connectServer({
+            roomName:roomName,
+            serverUrl:hallServerUrl,
+            messageCB:onHallMessage,
+            onRoomConnect:onHallConnect,
+            onRoomTimeoutClose:onHallTimeoutClose,
+        }));
+    }
 }
 export const disconnectHall=()=>{
     return disconnectServer(roomName);
 }
 export const hallSendReg=()=>{
     return (dispatch, getState) => {
-        console.log("login user");
         let main=getState().main;
         let userInfo=main.userInfo;
         let {UserID,GlobalUID,FaceID,NickName}=userInfo;

+ 44 - 7
src/features/colyseus/CSClient.js

@@ -10,9 +10,14 @@ class CSClient {
         this.callbacks=[];
         this.retryTimer=null;
         this.retryTimeout=1000;
-        this.maxRetry=15;
+        this.maxRetry=5;
         this.currRetry=0;
         this.isKicked=false;
+        //ping
+        this.pingTimer=null;
+        this.pingInterval=10000;
+        this.pingTimeout=0;
+        this.pingTimeoutMax=3;
         this.unsetRoom();
     }
     clearTimer(){
@@ -20,6 +25,27 @@ class CSClient {
             clearTimeout(this.retryTimer);
             this.retryTimer = null;
         }
+        this.pingTimeout=0;
+    }
+    clearPingTimer(){
+        if (this.pingTimer){
+            clearInterval(this.pingTimer);
+            this.pingTimer=null;
+        }
+    }
+    startRoomPing(){
+        this.clearPingTimer();
+        let self=this;
+        this.pingTimer=setInterval(()=>{
+            if (self.pingTimeout>self.pingTimeoutMax){
+                self.clearPingTimer();
+                console.log("connecting timeout");
+                self.reconnect();
+                return;
+            }
+            self.pingTimeout++;
+            self.send("PING","");
+        },this.pingInterval);
     }
     unsetRoom(){
         console.log("unsetRoomunsetRoom");
@@ -27,6 +53,7 @@ class CSClient {
             this.currentRoom.removeAllListeners();
             this.currentRoom=null;
         }
+        this.clearPingTimer();
     }
     setRoom(room){
         this.currentRoom=room;
@@ -46,7 +73,11 @@ class CSClient {
             if (message!=null&&message.cmd==="kick"){
                 self.isKicked=true;
             }
-            // console.log("Room:"+self.roomName+" onMessage:"+" type:"+type+" message:"+JSON.stringify(message))
+            if (type==="PONG"){
+                self.pingTimeout--;
+                return;
+            }
+            console.log("Room:"+self.roomName+" onMessage:"+" type:"+type+" message:"+JSON.stringify(message))
             if (self.messageCB)self.messageCB(type,message);
         })
         room.onError((code, message) => {
@@ -58,10 +89,12 @@ class CSClient {
             self.unsetRoom();
         });
         if (this.connectedCB)this.connectedCB(true);
+        this.startRoomPing();
     }
     async reconnect(resolve){
         let during=this.retryTimeout+this.currRetry*2000;
         this.clearTimer();
+        this.clearPingTimer();
         if (this.currRetry>=this.maxRetry){
             if (resolve)resolve(false);
             this.currRetry=0;
@@ -78,6 +111,7 @@ class CSClient {
     }
     async connect(options={}){
         let self=this;
+        this.clearPingTimer();
         if(this.isConnected())await this.disconnect();
         return new Promise((resolve, reject)=>{
             self.client.joinOrCreate(this.roomName,options).then((room)=>{
@@ -93,20 +127,23 @@ class CSClient {
         let self=this;
         return new Promise((resolve, reject)=>{
             if (self.currentRoom!=null){
-                self.currentRoom.onLeave(()=>{
-                    self.unsetRoom();
-                    resolve(true);
-                });
+                // self.currentRoom.onLeave(()=>{
+                //     self.unsetRoom();
+                //     resolve(true);
+                // });
                 self.currentRoom.leave();
+                self.unsetRoom();
+                resolve(true);
             }
             else{
+                self.unsetRoom();
                 resolve(true);
             }
         })
     }
     send(type,message){
         if (this.currentRoom){
-            console.log("send:",type,message)
+            // console.log("send:",type,message)
             this.currentRoom.send(type,message);
         }
     }

+ 2 - 2
src/features/socketSlice.js

@@ -50,11 +50,10 @@ export const getHallConnState=(state)=>{
 }
 export default Socket.reducer;
 
-export const connectServer=({roomName, serverUrl,messageCB=null,onRoomConnect=null,options={}})=>{
+export const connectServer=({roomName, serverUrl,messageCB=null,onRoomConnect=null,onRoomTimeoutClose=null,options={}})=>{
     return async (dispatch, getState) => {
         if (CSClientWrap.isConnected(roomName))return ;
         let isConnected=await CSClientWrap.connect(roomName,serverUrl,(type,message)=>{
-            console.log(message);
             dispatch(onMessage({roomName,type,message}));
             if (messageCB!=null)dispatch(messageCB(type,message));
         },(connected)=>{
@@ -62,6 +61,7 @@ export const connectServer=({roomName, serverUrl,messageCB=null,onRoomConnect=nu
             if (onRoomConnect!=null)dispatch(onRoomConnect());
         },()=>{
             dispatch(onDisconnect(roomName));
+            if (onRoomTimeoutClose!=null)dispatch(onRoomTimeoutClose());
         });
     }
 }

+ 3 - 1
src/locales/languages/en_US.json

@@ -573,7 +573,9 @@
     "get_success": "Successfully received"
   },
   "hall": {
-    "offline": "You are already online somewhere else and are forced offline here."
+    "offline": "You are already online somewhere else and are forced offline here.",
+    "reconnect": "Reconnect",
+    "disconnected": "The network is disconnected. Do you want to reconnect?"
   },
   "web": {
     "activity": {

+ 3 - 1
src/locales/languages/es_MX.json

@@ -562,7 +562,9 @@
     "get_success": "Recibido exitosamente"
   },
   "hall": {
-    "offline": "Ya estabas conectado en otro lugar y te obligaron a desconectarte aquí."
+    "offline": "Ya estabas conectado en otro lugar y te obligaron a desconectarte aquí.",
+    "reconnect": "Reconectar",
+    "disconnected": "La red se ha desconectado. ¿Quieres volver a conectarte?"
   },
   "web": {
     "activity": {

+ 3 - 1
src/locales/languages/pt_BR.json

@@ -562,7 +562,9 @@
     "get_success": "Recebido com sucesso"
   },
   "hall": {
-    "offline": "Você já estava online em outro lugar e foi forçado a ficar offline aqui."
+    "offline": "Você já estava online em outro lugar e foi forçado a ficar offline aqui.",
+    "reconnect": "Reconectar",
+    "disconnected": "A rede foi desconectada. Deseja reconectar?"
   },
   "web": {
     "activity": {

+ 3 - 1
src/locales/languages/ru_RU.json

@@ -562,7 +562,9 @@
     "get_success": "Получено успешно"
   },
   "hall": {
-    "offline": "Вы уже были онлайн в другом месте и были вынуждены отключиться здесь."
+    "offline": "Вы уже были онлайн в другом месте и были вынуждены отключиться здесь.",
+    "reconnect": "переподключиться",
+    "disconnected": "Сеть отключена. Хотите восстановить соединение?"
   },
   "web": {
     "activity": {

+ 3 - 1
src/locales/languages/zh_TW.json

@@ -562,7 +562,9 @@
     "get_success": "領取成功"
   },
   "hall": {
-    "offline": "您已經在其他地方上線,此處被迫下線。"
+    "offline": "您已經在其他地方上線,此處被迫下線。",
+    "reconnect": "重新連接",
+    "disconnected": "網路已斷開連接,要重新連接嗎?"
   },
   "web": {
     "activity": {