private int GetReadyCode(ProtoCmd subcmd) { if (!SdkStatus.IsInited() && subcmd != ProtoCmd.ECmdLoginReq) { // 发送失败: 没有初始化 (login不需要初始化) var info = new PlayerInfo { Id = "" }; GamePlayerInfo.SetInfo(info); UserStatus.SetStatus(UserStatus.StatusType.Logout); return(ErrCode.EcSdkUninit); } // 检测 socket if (Socket == null || string.IsNullOrEmpty(Socket.Url)) { return((int)QAppProtoErrCode.EcSdkSendFail); } if (!UserStatus.IsStatus(UserStatus.StatusType.Login) && subcmd != ProtoCmd.ECmdLoginReq && subcmd != ProtoCmd.ECmdLogoutReq) { return((int)QAppProtoErrCode.EcSdkNoLogin); } if (Socket.Id == (int)ConnectionType.Relay && !CheckLoginStatus.IsChecked() && (subcmd == ProtoCmd.ECmdRelaySendFrameReq || subcmd == ProtoCmd.ECmdRelayRequestFrameReq || subcmd == ProtoCmd.ECmdHeartBeatReq || subcmd == ProtoCmd.ECmdRelayClientSendtoGamesvrReq)) { return((int)QAppProtoErrCode.EcSdkNoCheckLogin); } // 发送消息 return(0); }
private static void ListenSocketConnect() { // 联网 Socket1.OnEvent("connect", (SocketEvent socketEvent) => { // 联网时自动Login if (!UserStatus.IsStatus(UserStatus.StatusType.Logining)) { UserUtil.Login(null); } if (string.IsNullOrEmpty(Socket1.Url)) { return; } var eve = new ResponseEvent(ErrCode.EcOk) { Data = Socket1.Id }; Sdk.Responses.OnNetwork(eve); }); Socket2.OnEvent("connect", (SocketEvent socketEvent) => { // check login 成功后发送业务数据 FrameSender.CheckLogin(null, "connect " + !!Socket2.IsSocketStatus("connect")); if (!string.IsNullOrEmpty(Socket2.Url)) { var eve = new ResponseEvent(ErrCode.EcOk) { Data = Socket2.Id }; Sdk.Responses.OnNetwork(eve); } Pinger2.Ping(null); }); // 断网 Socket1.OnEvent("connectClose", (SocketEvent socketEvent) => { // 初始化失败 SdkInitCallback(false, new ResponseEvent(ErrCode.EcSdkSocketClose)); if (!SdkStatus.IsInited()) { return; } // 断网时自动 Logout UserStatus.SetStatus(UserStatus.StatusType.Logout); if (string.IsNullOrEmpty(Socket1.Url)) { return; } var eve = new ResponseEvent(ErrCode.EcSdkSocketClose, "Socket 断开", null, null); Sdk.Responses.OnNetwork(eve); }); Socket2.OnEvent("connectClose", (SocketEvent socketEvent) => { if (!SdkStatus.IsInited()) { return; } Debugger.Log("socket2 on connect close"); CheckLoginStatus.SetStatus(CheckLoginStatus.StatusType.Offline); if (!string.IsNullOrEmpty(Socket2.Url)) { var eve = new ResponseEvent(ErrCode.EcSdkSocketClose, "Socket 断开", null, null); Sdk.Responses.OnNetwork(eve); } ; Pinger2.Stop(); }); // socket 错误 Socket1.OnEvent("connectError", (SocketEvent socketEvent) => { // 初始化失败 SdkInitCallback(false, new ResponseEvent(ErrCode.EcSdkSocketError)); if (!SdkStatus.IsInited()) { return; } if (string.IsNullOrEmpty(Socket1.Url)) { return; } var eve = new ResponseEvent(ErrCode.EcSdkSocketError, "Socket 错误", null, null); Sdk.Responses.OnNetwork(eve); }); Socket2.OnEvent("connectError", (SocketEvent socketEvent) => { if (!SdkStatus.IsInited()) { return; } if (string.IsNullOrEmpty(Socket2.Url)) { return; } var eve = new ResponseEvent(ErrCode.EcSdkSocketError, "Socket 错误", null, null); Sdk.Responses.OnNetwork(eve); }); // 需要自动登录 Socket1.OnEvent("autoAuth", (SocketEvent socketEvent) => { if (!SdkStatus.IsInited()) { return; } var timer = new Timer(); timer.SetTimeout(() => { var isLogout = UserStatus.IsStatus(UserStatus.StatusType.Logout); if (!string.IsNullOrEmpty(Socket1.Url) && isLogout) { UserUtil.Login(null); } ; }, 1000); }); Socket2.OnEvent("autoAuth", (SocketEvent socketEvent) => { if (!SdkStatus.IsInited()) { return; } if (string.IsNullOrEmpty(Socket2.Url)) { return; } var timer = new Timer(); timer.SetTimeout(() => { // Debugger.Log("auto auth check 1"); // 检查是否需要重登录 if (UserStatus.IsStatus(UserStatus.StatusType.Logout)) { UserUtil.Login(null); } // 检查是否需要 checkLogin var info = FrameSender.RoomInfo ?? new RoomInfo { RouteId = "" }; // Debugger.Log("auto auth check 2: {0}", CheckLoginStatus.GetRouteId() != info.RouteId); if (CheckLoginStatus.IsOffline() || CheckLoginStatus.GetRouteId() != info.RouteId) { FrameSender.CheckLogin((ResponseEvent eve) => { if (eve.Code == ErrCode.EcOk) { Pinger2.Ping(null); } }, "autoAuth"); } }, 1000); }); }