|
@@ -1,7 +1,5 @@
|
|
import { _decorator, Component, Node } from 'cc';
|
|
import { _decorator, Component, Node } from 'cc';
|
|
-import { userDataManager } from '../manager/userDataManager';
|
|
|
|
import { msgManager } from './msgManager';
|
|
import { msgManager } from './msgManager';
|
|
-import { config } from '../config';
|
|
|
|
const { ccclass, property } = _decorator;
|
|
const { ccclass, property } = _decorator;
|
|
|
|
|
|
@ccclass('gameSocket')
|
|
@ccclass('gameSocket')
|
|
@@ -13,16 +11,22 @@ export class gameSocket extends Component {
|
|
return gameSocket._instance;
|
|
return gameSocket._instance;
|
|
}
|
|
}
|
|
sock:WebSocket = null;
|
|
sock:WebSocket = null;
|
|
|
|
+ private current_url:string = '' //当前url
|
|
is_connected:Boolean = false;
|
|
is_connected:Boolean = false;
|
|
stopConnected:Boolean = false;
|
|
stopConnected:Boolean = false;
|
|
check_heartbeat_succeed:boolean = false;
|
|
check_heartbeat_succeed:boolean = false;
|
|
is_online_status:boolean = false; //是否是在线状态
|
|
is_online_status:boolean = false; //是否是在线状态
|
|
|
|
|
|
|
|
+ private reconnectInterval = 1000; // 重连间隔时间(毫秒)
|
|
|
|
+ private reconnectMaxAttempts = 30; // 最大重连尝试次数
|
|
|
|
+ private reconnectAttempts = 0; // 当前重连尝试次数
|
|
|
|
+
|
|
private static RECV_HEART_BEAT ='1' //收到服务器心跳结果
|
|
private static RECV_HEART_BEAT ='1' //收到服务器心跳结果
|
|
private static RECV_CHEAK_HEART_BEAT = '2' //服务器查询我的心跳
|
|
private static RECV_CHEAK_HEART_BEAT = '2' //服务器查询我的心跳
|
|
|
|
|
|
_on_opened(event:Event):void {
|
|
_on_opened(event:Event):void {
|
|
console.log('_on_opened=',event)
|
|
console.log('_on_opened=',event)
|
|
|
|
+ this.reconnectAttempts = 0
|
|
if(this.is_connected===true){
|
|
if(this.is_connected===true){
|
|
|
|
|
|
}else{
|
|
}else{
|
|
@@ -65,16 +69,35 @@ export class gameSocket extends Component {
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
- _on_socket_close(event:any):void {
|
|
|
|
|
|
+ _on_socket_close(event:any):void {
|
|
console.log("_on_socket_close",event)
|
|
console.log("_on_socket_close",event)
|
|
- this.is_connected = false;
|
|
|
|
- setTimeout(()=>{ msgManager.reConnect() },1000)
|
|
|
|
|
|
+ this.reConnectOperation()
|
|
|
|
+ this.is_connected = false;
|
|
}
|
|
}
|
|
|
|
|
|
_on_socket_err(event:any):void {
|
|
_on_socket_err(event:any):void {
|
|
console.log("_on_socket_err",event)
|
|
console.log("_on_socket_err",event)
|
|
|
|
+ this.reConnectOperation()
|
|
this.is_connected = false;
|
|
this.is_connected = false;
|
|
- // this.close()
|
|
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ private reConnectOperation() {
|
|
|
|
+ if (this.reconnectAttempts < this.reconnectMaxAttempts) {
|
|
|
|
+ console.log('正在尝试重连...');
|
|
|
|
+ setTimeout(()=>{
|
|
|
|
+ this.reonnect()
|
|
|
|
+ },this.reconnectInterval)
|
|
|
|
+ this.reconnectAttempts++;
|
|
|
|
+ } else {
|
|
|
|
+ console.log('重连尝试次数已达上限');
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ reonnect() {
|
|
|
|
+ if(this.current_url.length<=0) {
|
|
|
|
+ return
|
|
|
|
+ }
|
|
|
|
+ this.connect(this.current_url)
|
|
}
|
|
}
|
|
|
|
|
|
connect(url:any):void {
|
|
connect(url:any):void {
|
|
@@ -82,6 +105,7 @@ export class gameSocket extends Component {
|
|
console.log("客户端退出链接,暂不提供重连")
|
|
console.log("客户端退出链接,暂不提供重连")
|
|
return
|
|
return
|
|
}
|
|
}
|
|
|
|
+ this.current_url = url
|
|
if(!this.is_connected){
|
|
if(!this.is_connected){
|
|
try{
|
|
try{
|
|
this.sock = new WebSocket(url);
|
|
this.sock = new WebSocket(url);
|