diff --git a/FunGame.Core.Api/Model/Enum/CommonEnums.cs b/FunGame.Core.Api/Model/Enum/CommonEnums.cs index ceb0225..b5b3e19 100644 --- a/FunGame.Core.Api/Model/Enum/CommonEnums.cs +++ b/FunGame.Core.Api/Model/Enum/CommonEnums.cs @@ -70,7 +70,8 @@ namespace FunGame.Core.Api.Model.Enum OK, OKCancel, YesNo, - RetryCancel + RetryCancel, + Input } public enum InterfaceType @@ -93,9 +94,18 @@ namespace FunGame.Core.Api.Model.Enum Login, CheckLogin, Logout, + Disconnect, HeartBeat } + public enum ErrorType + { + None, + IsNotIP, + IsNotPort, + WrongFormat + } + #endregion #region Result @@ -119,7 +129,8 @@ namespace FunGame.Core.Api.Model.Enum CloseSocket, StartWebHelper, Login, - Logout + Logout, + Disconnect } public enum InterfaceMethod diff --git a/FunGame.Core.Api/Util/Utility.cs b/FunGame.Core.Api/Util/Utility.cs new file mode 100644 index 0000000..bb63d9d --- /dev/null +++ b/FunGame.Core.Api/Util/Utility.cs @@ -0,0 +1,79 @@ +using FunGame.Core.Api.Model.Enum; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Text.RegularExpressions; +using System.Threading.Tasks; + +namespace FunGame.Core.Api.Util +{ + /// + /// 工具类,客户端和服务器端都可以直接调用的工具方法都可以写在这里 + /// + public class Utility + { + /// + /// 判断字符串是否是IP地址 + /// + /// + /// + public static bool IsIP(string str) + { + //判断是否为IP + return Regex.IsMatch(str, @"^((2[0-4]\d|25[0-5]|[01]?\d\d?)\.){3}(2[0-4]\d|25[0-5]|[01]?\d\d?)$"); + } + + /// + /// 判断字符串是否为邮箱地址 + /// + /// + /// + public static bool IsEmail(string str) + { + //判断是否为Email + return Regex.IsMatch(str, @"^(\w)+(\.\w)*@(\w)+((\.\w+)+)$"); + } + + /// + /// 判断字符串是否是一个FunGame可接受的服务器地址 + /// + /// + /// + public static ErrorType IsServerAddress(string str) + { + string[] strs = str.Split(':'); + string ip; + int port; + if (strs.Length < 2) + { + ip = strs[0]; + port = 22222; + } + else if (strs.Length < 3) + { + ip = strs[0]; + port = Convert.ToInt32(strs[1]); + } + else return ErrorType.WrongFormat; + if (IsIP(ip) && port > 0 && port < 65536) return ErrorType.None; + else if (!IsIP(ip) && port > 0 && port < 65536) return ErrorType.IsNotIP; + else if (IsIP(ip) && (port <= 0 || port >= 65536)) return ErrorType.IsNotPort; + else return ErrorType.WrongFormat; + } + + /// + /// 判断参数是否是一个FunGame可接受的服务器地址 + /// + /// + /// + /// + public static ErrorType IsServerAddress(string ip, int port) + { + if (IsIP(ip) && port > 0 && port < 65536) return ErrorType.None; + else if (!IsIP(ip) && port > 0 && port < 65536) return ErrorType.IsNotIP; + else if (IsIP(ip) && (port <= 0 || port >= 65536)) return ErrorType.IsNotPort; + else return ErrorType.WrongFormat; + } + } +} diff --git a/FunGame.Desktop/Models/Component/ShowMessage.Designer.cs b/FunGame.Desktop/Models/Component/ShowMessage.Designer.cs index 2da5b97..5718bf6 100644 --- a/FunGame.Desktop/Models/Component/ShowMessage.Designer.cs +++ b/FunGame.Desktop/Models/Component/ShowMessage.Designer.cs @@ -37,6 +37,8 @@ this.MidButton = new System.Windows.Forms.Button(); this.Title = new System.Windows.Forms.Label(); this.TransparentRect = new FunGame.Desktop.Models.Component.TransparentRect(); + this.InputButton = new System.Windows.Forms.Button(); + this.InputText = new System.Windows.Forms.TextBox(); this.TransparentRect.SuspendLayout(); this.SuspendLayout(); // @@ -57,7 +59,7 @@ this.LeftButton.Location = new System.Drawing.Point(13, 127); this.LeftButton.Name = "LeftButton"; this.LeftButton.Size = new System.Drawing.Size(98, 37); - this.LeftButton.TabIndex = 98; + this.LeftButton.TabIndex = 1; this.LeftButton.Text = "Left"; this.LeftButton.UseVisualStyleBackColor = true; this.LeftButton.Click += new System.EventHandler(this.LeftButton_Click); @@ -77,7 +79,7 @@ this.Exit.Location = new System.Drawing.Point(187, 1); this.Exit.Name = "Exit"; this.Exit.Size = new System.Drawing.Size(47, 47); - this.Exit.TabIndex = 15; + this.Exit.TabIndex = 3; this.Exit.TextAlign = System.Drawing.ContentAlignment.TopLeft; this.Exit.UseVisualStyleBackColor = false; this.Exit.Click += new System.EventHandler(this.Exit_Click); @@ -88,7 +90,7 @@ this.RightButton.Location = new System.Drawing.Point(125, 127); this.RightButton.Name = "RightButton"; this.RightButton.Size = new System.Drawing.Size(98, 37); - this.RightButton.TabIndex = 101; + this.RightButton.TabIndex = 2; this.RightButton.Text = "Right"; this.RightButton.UseVisualStyleBackColor = true; this.RightButton.Click += new System.EventHandler(this.RightButton_Click); @@ -99,7 +101,7 @@ this.MidButton.Location = new System.Drawing.Point(65, 127); this.MidButton.Name = "MidButton"; this.MidButton.Size = new System.Drawing.Size(98, 37); - this.MidButton.TabIndex = 102; + this.MidButton.TabIndex = 1; this.MidButton.Text = "Middle"; this.MidButton.UseVisualStyleBackColor = true; this.MidButton.Click += new System.EventHandler(this.MidButton_Click); @@ -119,6 +121,8 @@ // this.TransparentRect.BackColor = System.Drawing.Color.WhiteSmoke; this.TransparentRect.BorderColor = System.Drawing.Color.WhiteSmoke; + this.TransparentRect.Controls.Add(this.InputButton); + this.TransparentRect.Controls.Add(this.InputText); this.TransparentRect.Controls.Add(this.Title); this.TransparentRect.Controls.Add(this.MidButton); this.TransparentRect.Controls.Add(this.RightButton); @@ -133,7 +137,30 @@ this.TransparentRect.Size = new System.Drawing.Size(235, 170); this.TransparentRect.TabIndex = 103; this.TransparentRect.TabStop = false; - this.TransparentRect.Text = "transparentRect1"; + // + // InputButton + // + this.InputButton.Font = new System.Drawing.Font("LanaPixel", 14.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point); + this.InputButton.Location = new System.Drawing.Point(168, 130); + this.InputButton.Name = "InputButton"; + this.InputButton.Size = new System.Drawing.Size(66, 34); + this.InputButton.TabIndex = 2; + this.InputButton.Text = "OK"; + this.InputButton.UseVisualStyleBackColor = true; + this.InputButton.Visible = false; + this.InputButton.Click += new System.EventHandler(this.InputButton_Click); + // + // InputText + // + this.InputText.Font = new System.Drawing.Font("LanaPixel", 18F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point); + this.InputText.Location = new System.Drawing.Point(2, 130); + this.InputText.MaxLength = 21; + this.InputText.Name = "InputText"; + this.InputText.Size = new System.Drawing.Size(163, 34); + this.InputText.TabIndex = 1; + this.InputText.Visible = false; + this.InputText.WordWrap = false; + this.InputText.KeyUp += new KeyEventHandler(this.InputText_KeyUp); // // ShowMessage // @@ -148,6 +175,7 @@ this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen; this.Text = "Show"; this.TransparentRect.ResumeLayout(false); + this.TransparentRect.PerformLayout(); this.ResumeLayout(false); } @@ -161,5 +189,7 @@ private Button MidButton; private Label Title; private TransparentRect TransparentRect; + private Button InputButton; + private TextBox InputText; } } \ No newline at end of file diff --git a/FunGame.Desktop/Models/Component/ShowMessage.cs b/FunGame.Desktop/Models/Component/ShowMessage.cs index df9b46e..1be9b9c 100644 --- a/FunGame.Desktop/Models/Component/ShowMessage.cs +++ b/FunGame.Desktop/Models/Component/ShowMessage.cs @@ -16,6 +16,7 @@ namespace FunGame.Desktop.Models.Component { private int Location_x, Location_y; private MessageResult MessageResult = MessageResult.Cancel; + private string InputResult = ""; private int AutoClose = 0; private const string TITLE_TIP = "提示"; @@ -70,6 +71,8 @@ namespace FunGame.Desktop.Models.Component { case MessageButtonType.OK: MidButton.Text = BUTTON_OK; + InputText.Visible = false; + InputButton.Visible = false; LeftButton.Visible = false; RightButton.Visible = false; MidButton.Visible = true; @@ -77,6 +80,8 @@ namespace FunGame.Desktop.Models.Component case MessageButtonType.OKCancel: LeftButton.Text = BUTTON_OK; RightButton.Text = BUTTON_CANCEL; + InputText.Visible = false; + InputButton.Visible = false; LeftButton.Visible = true; RightButton.Visible = true; MidButton.Visible = false; @@ -84,6 +89,8 @@ namespace FunGame.Desktop.Models.Component case MessageButtonType.YesNo: LeftButton.Text = BUTTON_YES; RightButton.Text = BUTTON_NO; + InputText.Visible = false; + InputButton.Visible = false; LeftButton.Visible = true; RightButton.Visible = true; MidButton.Visible = false; @@ -91,10 +98,20 @@ namespace FunGame.Desktop.Models.Component case MessageButtonType.RetryCancel: LeftButton.Text = BUTTON_RETRY; RightButton.Text = BUTTON_CANCEL; + InputText.Visible = false; + InputButton.Visible = false; LeftButton.Visible = true; RightButton.Visible = true; MidButton.Visible = false; break; + case MessageButtonType.Input: + InputButton.Text = BUTTON_OK; + LeftButton.Visible = false; + RightButton.Visible = false; + MidButton.Visible = false; + InputText.Visible = true; + InputButton.Visible = true; + break; } if (length > 4 && objs[4] != null) MidButton.Text = (string)objs[4]; if (length > 5 && objs[5] != null) LeftButton.Text = (string)objs[5]; @@ -231,6 +248,13 @@ namespace FunGame.Desktop.Models.Component return result; } + public static string InputMessage(string msg, string title) + { + object[] objs = { title, msg, 0, MessageButtonType.Input, BUTTON_CANCEL, BUTTON_RETRY, BUTTON_CANCEL }; + string result = new ShowMessage(objs).InputResult; + return result; + } + private void LeftButton_Click(object sender, EventArgs e) { SetButtonResult(LeftButton.Text); @@ -246,6 +270,34 @@ namespace FunGame.Desktop.Models.Component SetButtonResult(MidButton.Text); } + private void InputButton_Click() + { + if (InputText.Text != null && !InputText.Text.Equals("")) + { + InputResult = InputText.Text; + Dispose(); + } + else + { + InputText.Enabled = false; + WarningMessage("不能输入空值!"); + InputText.Enabled = true; + } + } + + private void InputButton_Click(object sender, EventArgs e) + { + InputButton_Click(); + } + + private void InputText_KeyUp(object sender, KeyEventArgs e) + { + if (e.KeyCode.Equals(Keys.Enter)) + { + InputButton_Click(); + } + } + private void Exit_Click(object sender, EventArgs e) { MessageResult = MessageResult.Cancel; diff --git a/FunGame.Desktop/Models/Component/ShowMessage.resx b/FunGame.Desktop/Models/Component/ShowMessage.resx index 03bd487..8ac3ecc 100644 --- a/FunGame.Desktop/Models/Component/ShowMessage.resx +++ b/FunGame.Desktop/Models/Component/ShowMessage.resx @@ -57,6 +57,15 @@ System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + True + + + True + + + True + @@ -68,6 +77,27 @@ eR/MUx+QvEfyPpinPiB5j+R9ME994BT5jv9Q+yX+S74/XvIdkpY7JUbXJnJZ8twAAAAASUVORK5CYII= + + True + + + True + + + True + + + True + + + True + + + True + + + True + AAABAAEAQEAAAAEAIAAoQgAAFgAAACgAAABAAAAAgAAAAAEAIAAAAAAAAEAAAAAAAAAAAAAAAAAAAAAA diff --git a/FunGame.Desktop/Models/Config/Config.cs b/FunGame.Desktop/Models/Config/Config.cs index 772d813..16a2e7f 100644 --- a/FunGame.Desktop/Models/Config/Config.cs +++ b/FunGame.Desktop/Models/Config/Config.cs @@ -28,6 +28,9 @@ namespace FunGame.Desktop.Models.Config public static bool FunGame_isConnected = false; // 是否连接上服务器 public static bool FunGame_isRetrying = false; // 是否正在重连 public static bool FunGame_isAutoRetry = true; // 是否自动重连 + public static bool Match_Mix = false; // 混战模式选项 + public static bool Match_Team = false; // 团队模式选项 + public static bool Match_HasPass = false; // 密码房间选项 public static string FunGame_Roomid = "-1"; // 房间号 public static string FunGame_Notice = ""; // 公告 @@ -40,6 +43,7 @@ namespace FunGame.Desktop.Models.Config public const string WebHelper_SetYellow = "-WebHelper .set yellow"; public const string WebHelper_WaitConnectAndSetYellow = "-WebHelper .waitconnect .set yellow"; public const string WebHelper_WaitLoginAndSetYellow = "-WebHelper .waitlogin .set yellow"; + public const string WebHelper_Disconnect = "-WebHelper .disconnect"; public const string WebHelper_Disconnected = "-WebHelper .disconnected"; public const string WebHelper_LogOut = "-WebHelper .logout"; public const string WebHelper_GetUser = "-WebHelper .get user"; @@ -50,8 +54,8 @@ namespace FunGame.Desktop.Models.Config /** * Socket Configs */ - public static string SERVER_IPADRESS = ""; // 默认IP地址 - public static int SERVER_PORT; // 默认端口 + public static string SERVER_IPADRESS = ""; // 服务器IP地址 + public static int SERVER_PORT = 0; // 服务器端口号 public static Encoding DEFAULT_ENCODING = Encoding.UTF8; /** @@ -71,7 +75,9 @@ namespace FunGame.Desktop.Models.Config public const string FunGame_CreateTeam = "创建游戏 团队"; public const string FunGame_StartGame = "开始游戏"; public const string FunGame_Connect = "连接服务器"; - public const string FunGame_Disconnect = "断开连接"; + public const string FunGame_ConnectTo = "连接指定服务器"; + public const string FunGame_Disconnect = "登出并断开连接"; + public const string FunGame_DisconnectWhenNotLogin = "断开连接"; public const string FunGame_Retry = "重新连接"; public const string FunGame_AutoRetryOn = "开启自动重连"; public const string FunGame_AutoRetryOff = "关闭自动重连"; @@ -92,7 +98,7 @@ namespace FunGame.Desktop.Models.Config public static readonly object[] PresetNoLoginItems = { FunGame_PresetMessage, - FunGame_Disconnect, + FunGame_DisconnectWhenNotLogin, FunGame_AutoRetryOn, FunGame_AutoRetryOff }; @@ -100,6 +106,7 @@ namespace FunGame.Desktop.Models.Config { FunGame_PresetMessage, FunGame_Connect, + FunGame_ConnectTo, FunGame_Retry, FunGame_AutoRetryOn, FunGame_AutoRetryOff diff --git a/FunGame.Desktop/Models/Config/Usercfg.cs b/FunGame.Desktop/Models/Config/Usercfg.cs index e105040..012443b 100644 --- a/FunGame.Desktop/Models/Config/Usercfg.cs +++ b/FunGame.Desktop/Models/Config/Usercfg.cs @@ -14,8 +14,5 @@ namespace FunGame.Desktop.Models.Config */ public static User? LoginUser = null; // 已登录的用户 public static string LoginUserName = ""; // 已登录用户名 - public static bool Match_Mix = false; // 混战模式选项 - public static bool Match_Team = false; // 团队模式选项 - public static bool Match_HasPass = false; // 密码房间选项 } } diff --git a/FunGame.Desktop/UI/Main/Main.cs b/FunGame.Desktop/UI/Main/Main.cs index fe9ff2e..67a0bef 100644 --- a/FunGame.Desktop/UI/Main/Main.cs +++ b/FunGame.Desktop/UI/Main/Main.cs @@ -9,6 +9,7 @@ using FunGame.Core.Api.Model.Entity; using FunGame.Desktop.Models.Config; using FunGame.Desktop.Utils; using FunGame.Core.Api.Model.Enum; +using FunGame.Core.Api.Util; namespace FunGame.Desktop.UI { @@ -192,13 +193,15 @@ namespace FunGame.Desktop.UI throw new Exception(GetNowShortTime() + "\nERROR:无法连接至服务器,请检查你的网络连接。"); else throw new Exception("ERROR:无法连接至服务器,请检查你的网络连接。"); - case Config.WebHelper_LogOut: + case Config.WebHelper_Disconnect: + Config.FunGame_isAutoRetry = false; Config.FunGame_isRetrying = false; - Config.FunGame_isConnected = false; + Config.FunGame_isAutoConnect = false; Config.FunGame_isAutoLogin = false; + Config.FunGame_isConnected = false; WebHelper_Action = (main) => { - SetServerStatusLight((int)LightType.Red); + SetServerStatusLight((int)LightType.Yellow); SetButtonEnableIfLogon(false, ClientState.WaitConnect); LogoutAccount(); }; @@ -206,7 +209,22 @@ namespace FunGame.Desktop.UI BeginInvoke(WebHelper_Action, this); else WebHelper_Action(this); - if (Config.FunGame_isAutoRetry) + break; + case Config.WebHelper_LogOut: + Config.FunGame_isRetrying = false; + Config.FunGame_isConnected = false; + Config.FunGame_isAutoLogin = false; + WebHelper_Action = (main) => + { + SetServerStatusLight((int)LightType.Yellow); + SetButtonEnableIfLogon(false, ClientState.WaitConnect); + LogoutAccount(); + }; + if (InvokeRequired) + BeginInvoke(WebHelper_Action, this); + else + WebHelper_Action(this); + if (Config.FunGame_isAutoConnect) { NOW_CONNECTEDRETRY = -1; Task.Run(() => @@ -299,6 +317,11 @@ namespace FunGame.Desktop.UI /// private void Connect() { + if (Config.SERVER_IPADRESS.Equals("") || Config.SERVER_PORT <= 0) + { + ShowMessage.ErrorMessage("查找可用的服务器失败!"); + return; + } Task.Run(() => { while (true) @@ -776,7 +799,7 @@ namespace FunGame.Desktop.UI // 向消息队列发送消息 if (!TalkText.Text.Trim().Equals("") && !TalkText.ForeColor.Equals(Color.DarkGray)) { - WritelnGameInfo(GetNowShortTime() + " [ " + (!Usercfg.LoginUserName.Equals("") ? Usercfg.LoginUserName : "尚未登录") + " ] 说: " + TalkText.Text); + WritelnGameInfo((!Usercfg.LoginUserName.Equals("") ? GetNowShortTime() + " [ " + Usercfg.LoginUserName + " ] 说: ": ":> ") + TalkText.Text); SwitchTalkMessage(TalkText.Text); TalkText.Text = ""; if (isLeave) TalkText_Leave(); // 回车不离开焦点 @@ -795,7 +818,7 @@ namespace FunGame.Desktop.UI /// private void SendTalkText_Click(string msg) { - WritelnGameInfo(GetNowShortTime() + " [ " + (!Usercfg.LoginUserName.Equals("") ? Usercfg.LoginUserName : "尚未登录") + " ] 说: " + msg); + WritelnGameInfo((!Usercfg.LoginUserName.Equals("") ? GetNowShortTime() + " [ " + Usercfg.LoginUserName + " ] 说: " : ":> ") + msg); } /// @@ -962,11 +985,11 @@ namespace FunGame.Desktop.UI WritelnGameInfo(GetNowShortTime() + " 开始匹配"); WritelnGameInfo("[ " + Usercfg.LoginUserName + " ] 开始匹配"); WriteGameInfo(">> 匹配参数:"); - if (!Usercfg.Match_Mix && !Usercfg.Match_Team && !Usercfg.Match_HasPass) + if (!Config.Match_Mix && !Config.Match_Team && !Config.Match_HasPass) WritelnGameInfo("无"); else { - WriteGameInfo((Usercfg.Match_Mix ? " 混战房间 " : "") + (Usercfg.Match_Team ? " 团队房间 " : "") + (Usercfg.Match_HasPass ? " 密码房间 " : "")); + WriteGameInfo((Config.Match_Mix ? " 混战房间 " : "") + (Config.Match_Team ? " 团队房间 " : "") + (Config.Match_HasPass ? " 密码房间 " : "")); WritelnGameInfo(); } // 显示停止匹配按钮 @@ -1002,24 +1025,24 @@ namespace FunGame.Desktop.UI private void CreateRoom_Click(object sender, EventArgs e) { string roomtype = ""; - if (Usercfg.Match_Mix && Usercfg.Match_Team) + if (Config.Match_Mix && Config.Match_Team) { ShowMessage.WarningMessage("创建房间不允许同时勾选混战和团队!"); return; } - else if (Usercfg.Match_Mix && !Usercfg.Match_Team && !Usercfg.Match_HasPass) + else if (Config.Match_Mix && !Config.Match_Team && !Config.Match_HasPass) { roomtype = Config.GameMode_Mix; } - else if (!Usercfg.Match_Mix && Usercfg.Match_Team && !Usercfg.Match_HasPass) + else if (!Config.Match_Mix && Config.Match_Team && !Config.Match_HasPass) { roomtype = Config.GameMode_Team; } - else if (Usercfg.Match_Mix && !Usercfg.Match_Team && Usercfg.Match_HasPass) + else if (Config.Match_Mix && !Config.Match_Team && Config.Match_HasPass) { roomtype = Config.GameMode_MixHasPass; } - else if (!Usercfg.Match_Mix && Usercfg.Match_Team && Usercfg.Match_HasPass) + else if (!Config.Match_Mix && Config.Match_Team && Config.Match_HasPass) { roomtype = Config.GameMode_TeamHasPass; } @@ -1138,8 +1161,8 @@ namespace FunGame.Desktop.UI /// private void CheckMix_CheckedChanged(object sender, EventArgs e) { - if (CheckMix.Checked) Usercfg.Match_Mix = true; - else Usercfg.Match_Mix = false; + if (CheckMix.Checked) Config.Match_Mix = true; + else Config.Match_Mix = false; } /// @@ -1149,8 +1172,8 @@ namespace FunGame.Desktop.UI /// private void CheckTeam_CheckedChanged(object sender, EventArgs e) { - if (CheckTeam.Checked) Usercfg.Match_Team = true; - else Usercfg.Match_Team = false; + if (CheckTeam.Checked) Config.Match_Team = true; + else Config.Match_Team = false; } /// @@ -1160,8 +1183,8 @@ namespace FunGame.Desktop.UI /// private void CheckHasPass_CheckedChanged(object sender, EventArgs e) { - if (CheckHasPass.Checked) Usercfg.Match_HasPass = true; - else Usercfg.Match_HasPass = false; + if (CheckHasPass.Checked) Config.Match_HasPass = true; + else Config.Match_HasPass = false; } /// @@ -1376,11 +1399,54 @@ namespace FunGame.Desktop.UI } break; case Config.FunGame_Disconnect: - if (Config.FunGame_isConnected) + if (Config.FunGame_isConnected && WebHelper != null) { - WritelnGameInfo(">> 实验性功能。"); + WebHelper.WebHelpMethod((int)WebHelperMethod.Disconnect); } break; + case Config.FunGame_DisconnectWhenNotLogin: + if (Config.FunGame_isConnected && WebHelper != null) + { + WebHelper.WebHelpMethod((int)WebHelperMethod.CloseSocket); + GetMessage(WebHelper, Config.WebHelper_Disconnect); + WritelnGameInfo(GetNowShortTime() + " >> 你已成功断开与服务器的连接。 "); + } + break; + case Config.FunGame_ConnectTo: + string msg = ShowMessage.InputMessage("请输入服务器IP地址和端口号,如: 127.0.0.1:22222。", "连接指定服务器"); + if (msg.Equals("")) return; + string[] addr = msg.Split(':'); + string ip; + int port; + if (addr.Length < 2) + { + ip = addr[0]; + port = 22222; + } + else if (addr.Length < 3) + { + ip = addr[0]; + port = Convert.ToInt32(addr[1]); + } + else + { + ShowMessage.ErrorMessage("格式错误!\n这不是一个服务器地址。"); + return; + } + ErrorType ErrorType = Utility.IsServerAddress(ip, port); + if (ErrorType == ErrorType.None) + { + Config.SERVER_IPADRESS = ip; + Config.SERVER_PORT = port; + NOW_CONNECTEDRETRY = -1; + Connect(); + } + else if (ErrorType == ErrorType.IsNotIP) ShowMessage.ErrorMessage("这不是一个IP地址!"); + else if (ErrorType == ErrorType.IsNotPort) ShowMessage.ErrorMessage("这不是一个端口号!\n正确范围:1~65535"); + else ShowMessage.ErrorMessage("格式错误!\n这不是一个服务器地址。"); + break; + default: + break; } } diff --git a/FunGame.Desktop/Utils/WebHelper.cs b/FunGame.Desktop/Utils/WebHelper.cs index 0af1d02..50bf2e5 100644 --- a/FunGame.Desktop/Utils/WebHelper.cs +++ b/FunGame.Desktop/Utils/WebHelper.cs @@ -61,6 +61,13 @@ namespace FunGame.Desktop.Utils return true; } return false; + case (int)WebHelperMethod.Disconnect: + if (client != null) + { + Send((int)SocketMessageType.Disconnect, new object[] { Main, client }); + return true; + } + return false; } return true; } @@ -178,6 +185,11 @@ namespace FunGame.Desktop.Utils Main.GetMessage(this, Config.WebHelper_LogOut); Close(); return true; + case (int)SocketMessageType.Disconnect: + Main.GetMessage(this, read, true); + Main.GetMessage(this, Config.WebHelper_Disconnect); + Close(); + return true; case (int)SocketMessageType.HeartBeat: if (WaitHeartBeat != null && !WaitHeartBeat.IsCompleted) WaitHeartBeat.Wait(1); Config.WebHelper_HeartBeatFaileds = 0; @@ -260,6 +272,11 @@ namespace FunGame.Desktop.Utils return true; } return false; + case SocketMessageType.Disconnect: + msg = MakeMessage(type, "断开连接"); + if (Send(msg, socket) > 0) + return true; + return false; case SocketMessageType.HeartBeat: msg = MakeMessage(type, "心跳检测"); if (Send(msg, socket) > 0) @@ -375,7 +392,7 @@ namespace FunGame.Desktop.Utils private void CreateSendHeartBeatStream() { - Thread.Sleep(1000); + Thread.Sleep(100); Main.GetMessage(this, "Creating: SendHeartBeatStream...OK"); while (IsConnected()) { @@ -386,7 +403,7 @@ namespace FunGame.Desktop.Utils private void CreateStreamReader() { - Thread.Sleep(1000); + Thread.Sleep(100); Main.GetMessage(this, "Creating: StreamReader...OK"); while (IsConnected()) {