实现连接指定IP和主动断开连接

This commit is contained in:
Mili 2022-09-17 20:46:29 +08:00
parent 5a4e87595a
commit f2e55ad20d
9 changed files with 326 additions and 37 deletions

View File

@ -70,7 +70,8 @@ namespace FunGame.Core.Api.Model.Enum
OK, OK,
OKCancel, OKCancel,
YesNo, YesNo,
RetryCancel RetryCancel,
Input
} }
public enum InterfaceType public enum InterfaceType
@ -93,9 +94,18 @@ namespace FunGame.Core.Api.Model.Enum
Login, Login,
CheckLogin, CheckLogin,
Logout, Logout,
Disconnect,
HeartBeat HeartBeat
} }
public enum ErrorType
{
None,
IsNotIP,
IsNotPort,
WrongFormat
}
#endregion #endregion
#region Result #region Result
@ -119,7 +129,8 @@ namespace FunGame.Core.Api.Model.Enum
CloseSocket, CloseSocket,
StartWebHelper, StartWebHelper,
Login, Login,
Logout Logout,
Disconnect
} }
public enum InterfaceMethod public enum InterfaceMethod

View File

@ -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
{
/// <summary>
/// 工具类,客户端和服务器端都可以直接调用的工具方法都可以写在这里
/// </summary>
public class Utility
{
/// <summary>
/// 判断字符串是否是IP地址
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
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?)$");
}
/// <summary>
/// 判断字符串是否为邮箱地址
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
public static bool IsEmail(string str)
{
//判断是否为Email
return Regex.IsMatch(str, @"^(\w)+(\.\w)*@(\w)+((\.\w+)+)$");
}
/// <summary>
/// 判断字符串是否是一个FunGame可接受的服务器地址
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
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;
}
/// <summary>
/// 判断参数是否是一个FunGame可接受的服务器地址
/// </summary>
/// <param name="ip"></param>
/// <param name="port"></param>
/// <returns></returns>
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;
}
}
}

View File

@ -37,6 +37,8 @@
this.MidButton = new System.Windows.Forms.Button(); this.MidButton = new System.Windows.Forms.Button();
this.Title = new System.Windows.Forms.Label(); this.Title = new System.Windows.Forms.Label();
this.TransparentRect = new FunGame.Desktop.Models.Component.TransparentRect(); 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.TransparentRect.SuspendLayout();
this.SuspendLayout(); this.SuspendLayout();
// //
@ -57,7 +59,7 @@
this.LeftButton.Location = new System.Drawing.Point(13, 127); this.LeftButton.Location = new System.Drawing.Point(13, 127);
this.LeftButton.Name = "LeftButton"; this.LeftButton.Name = "LeftButton";
this.LeftButton.Size = new System.Drawing.Size(98, 37); this.LeftButton.Size = new System.Drawing.Size(98, 37);
this.LeftButton.TabIndex = 98; this.LeftButton.TabIndex = 1;
this.LeftButton.Text = "Left"; this.LeftButton.Text = "Left";
this.LeftButton.UseVisualStyleBackColor = true; this.LeftButton.UseVisualStyleBackColor = true;
this.LeftButton.Click += new System.EventHandler(this.LeftButton_Click); this.LeftButton.Click += new System.EventHandler(this.LeftButton_Click);
@ -77,7 +79,7 @@
this.Exit.Location = new System.Drawing.Point(187, 1); this.Exit.Location = new System.Drawing.Point(187, 1);
this.Exit.Name = "Exit"; this.Exit.Name = "Exit";
this.Exit.Size = new System.Drawing.Size(47, 47); 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.TextAlign = System.Drawing.ContentAlignment.TopLeft;
this.Exit.UseVisualStyleBackColor = false; this.Exit.UseVisualStyleBackColor = false;
this.Exit.Click += new System.EventHandler(this.Exit_Click); this.Exit.Click += new System.EventHandler(this.Exit_Click);
@ -88,7 +90,7 @@
this.RightButton.Location = new System.Drawing.Point(125, 127); this.RightButton.Location = new System.Drawing.Point(125, 127);
this.RightButton.Name = "RightButton"; this.RightButton.Name = "RightButton";
this.RightButton.Size = new System.Drawing.Size(98, 37); this.RightButton.Size = new System.Drawing.Size(98, 37);
this.RightButton.TabIndex = 101; this.RightButton.TabIndex = 2;
this.RightButton.Text = "Right"; this.RightButton.Text = "Right";
this.RightButton.UseVisualStyleBackColor = true; this.RightButton.UseVisualStyleBackColor = true;
this.RightButton.Click += new System.EventHandler(this.RightButton_Click); this.RightButton.Click += new System.EventHandler(this.RightButton_Click);
@ -99,7 +101,7 @@
this.MidButton.Location = new System.Drawing.Point(65, 127); this.MidButton.Location = new System.Drawing.Point(65, 127);
this.MidButton.Name = "MidButton"; this.MidButton.Name = "MidButton";
this.MidButton.Size = new System.Drawing.Size(98, 37); this.MidButton.Size = new System.Drawing.Size(98, 37);
this.MidButton.TabIndex = 102; this.MidButton.TabIndex = 1;
this.MidButton.Text = "Middle"; this.MidButton.Text = "Middle";
this.MidButton.UseVisualStyleBackColor = true; this.MidButton.UseVisualStyleBackColor = true;
this.MidButton.Click += new System.EventHandler(this.MidButton_Click); this.MidButton.Click += new System.EventHandler(this.MidButton_Click);
@ -119,6 +121,8 @@
// //
this.TransparentRect.BackColor = System.Drawing.Color.WhiteSmoke; this.TransparentRect.BackColor = System.Drawing.Color.WhiteSmoke;
this.TransparentRect.BorderColor = 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.Title);
this.TransparentRect.Controls.Add(this.MidButton); this.TransparentRect.Controls.Add(this.MidButton);
this.TransparentRect.Controls.Add(this.RightButton); this.TransparentRect.Controls.Add(this.RightButton);
@ -133,7 +137,30 @@
this.TransparentRect.Size = new System.Drawing.Size(235, 170); this.TransparentRect.Size = new System.Drawing.Size(235, 170);
this.TransparentRect.TabIndex = 103; this.TransparentRect.TabIndex = 103;
this.TransparentRect.TabStop = false; 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 // ShowMessage
// //
@ -148,6 +175,7 @@
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen; this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "Show"; this.Text = "Show";
this.TransparentRect.ResumeLayout(false); this.TransparentRect.ResumeLayout(false);
this.TransparentRect.PerformLayout();
this.ResumeLayout(false); this.ResumeLayout(false);
} }
@ -161,5 +189,7 @@
private Button MidButton; private Button MidButton;
private Label Title; private Label Title;
private TransparentRect TransparentRect; private TransparentRect TransparentRect;
private Button InputButton;
private TextBox InputText;
} }
} }

View File

@ -16,6 +16,7 @@ namespace FunGame.Desktop.Models.Component
{ {
private int Location_x, Location_y; private int Location_x, Location_y;
private MessageResult MessageResult = MessageResult.Cancel; private MessageResult MessageResult = MessageResult.Cancel;
private string InputResult = "";
private int AutoClose = 0; private int AutoClose = 0;
private const string TITLE_TIP = "提示"; private const string TITLE_TIP = "提示";
@ -70,6 +71,8 @@ namespace FunGame.Desktop.Models.Component
{ {
case MessageButtonType.OK: case MessageButtonType.OK:
MidButton.Text = BUTTON_OK; MidButton.Text = BUTTON_OK;
InputText.Visible = false;
InputButton.Visible = false;
LeftButton.Visible = false; LeftButton.Visible = false;
RightButton.Visible = false; RightButton.Visible = false;
MidButton.Visible = true; MidButton.Visible = true;
@ -77,6 +80,8 @@ namespace FunGame.Desktop.Models.Component
case MessageButtonType.OKCancel: case MessageButtonType.OKCancel:
LeftButton.Text = BUTTON_OK; LeftButton.Text = BUTTON_OK;
RightButton.Text = BUTTON_CANCEL; RightButton.Text = BUTTON_CANCEL;
InputText.Visible = false;
InputButton.Visible = false;
LeftButton.Visible = true; LeftButton.Visible = true;
RightButton.Visible = true; RightButton.Visible = true;
MidButton.Visible = false; MidButton.Visible = false;
@ -84,6 +89,8 @@ namespace FunGame.Desktop.Models.Component
case MessageButtonType.YesNo: case MessageButtonType.YesNo:
LeftButton.Text = BUTTON_YES; LeftButton.Text = BUTTON_YES;
RightButton.Text = BUTTON_NO; RightButton.Text = BUTTON_NO;
InputText.Visible = false;
InputButton.Visible = false;
LeftButton.Visible = true; LeftButton.Visible = true;
RightButton.Visible = true; RightButton.Visible = true;
MidButton.Visible = false; MidButton.Visible = false;
@ -91,10 +98,20 @@ namespace FunGame.Desktop.Models.Component
case MessageButtonType.RetryCancel: case MessageButtonType.RetryCancel:
LeftButton.Text = BUTTON_RETRY; LeftButton.Text = BUTTON_RETRY;
RightButton.Text = BUTTON_CANCEL; RightButton.Text = BUTTON_CANCEL;
InputText.Visible = false;
InputButton.Visible = false;
LeftButton.Visible = true; LeftButton.Visible = true;
RightButton.Visible = true; RightButton.Visible = true;
MidButton.Visible = false; MidButton.Visible = false;
break; 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 > 4 && objs[4] != null) MidButton.Text = (string)objs[4];
if (length > 5 && objs[5] != null) LeftButton.Text = (string)objs[5]; if (length > 5 && objs[5] != null) LeftButton.Text = (string)objs[5];
@ -231,6 +248,13 @@ namespace FunGame.Desktop.Models.Component
return result; 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) private void LeftButton_Click(object sender, EventArgs e)
{ {
SetButtonResult(LeftButton.Text); SetButtonResult(LeftButton.Text);
@ -246,6 +270,34 @@ namespace FunGame.Desktop.Models.Component
SetButtonResult(MidButton.Text); 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) private void Exit_Click(object sender, EventArgs e)
{ {
MessageResult = MessageResult.Cancel; MessageResult = MessageResult.Cancel;

View File

@ -57,6 +57,15 @@
<resheader name="writer"> <resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value> <value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader> </resheader>
<metadata name="MsgText.Locked" type="System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<value>True</value>
</metadata>
<metadata name="LeftButton.Locked" type="System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<value>True</value>
</metadata>
<metadata name="Exit.Locked" type="System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<value>True</value>
</metadata>
<assembly alias="System.Drawing" name="System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" /> <assembly alias="System.Drawing" name="System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
<data name="Exit.BackgroundImage" type="System.Drawing.Bitmap, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64"> <data name="Exit.BackgroundImage" type="System.Drawing.Bitmap, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">
<value> <value>
@ -68,6 +77,27 @@
eR/MUx+QvEfyPpinPiB5j+R9ME994BT5jv9Q+yX+S74/XvIdkpY7JUbXJnJZ8twAAAAASUVORK5CYII= eR/MUx+QvEfyPpinPiB5j+R9ME994BT5jv9Q+yX+S74/XvIdkpY7JUbXJnJZ8twAAAAASUVORK5CYII=
</value> </value>
</data> </data>
<metadata name="RightButton.Locked" type="System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<value>True</value>
</metadata>
<metadata name="MidButton.Locked" type="System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<value>True</value>
</metadata>
<metadata name="Title.Locked" type="System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<value>True</value>
</metadata>
<metadata name="TransparentRect.Locked" type="System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<value>True</value>
</metadata>
<metadata name="InputButton.Locked" type="System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<value>True</value>
</metadata>
<metadata name="InputText.Locked" type="System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<value>True</value>
</metadata>
<metadata name="$this.Locked" type="System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<value>True</value>
</metadata>
<data name="$this.Icon" type="System.Drawing.Icon, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64"> <data name="$this.Icon" type="System.Drawing.Icon, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">
<value> <value>
AAABAAEAQEAAAAEAIAAoQgAAFgAAACgAAABAAAAAgAAAAAEAIAAAAAAAAEAAAAAAAAAAAAAAAAAAAAAA AAABAAEAQEAAAAEAIAAoQgAAFgAAACgAAABAAAAAgAAAAAEAIAAAAAAAAEAAAAAAAAAAAAAAAAAAAAAA

View File

@ -28,6 +28,9 @@ namespace FunGame.Desktop.Models.Config
public static bool FunGame_isConnected = false; // 是否连接上服务器 public static bool FunGame_isConnected = false; // 是否连接上服务器
public static bool FunGame_isRetrying = false; // 是否正在重连 public static bool FunGame_isRetrying = false; // 是否正在重连
public static bool FunGame_isAutoRetry = true; // 是否自动重连 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_Roomid = "-1"; // 房间号
public static string FunGame_Notice = ""; // 公告 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_SetYellow = "-WebHelper .set yellow";
public const string WebHelper_WaitConnectAndSetYellow = "-WebHelper .waitconnect .set yellow"; public const string WebHelper_WaitConnectAndSetYellow = "-WebHelper .waitconnect .set yellow";
public const string WebHelper_WaitLoginAndSetYellow = "-WebHelper .waitlogin .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_Disconnected = "-WebHelper .disconnected";
public const string WebHelper_LogOut = "-WebHelper .logout"; public const string WebHelper_LogOut = "-WebHelper .logout";
public const string WebHelper_GetUser = "-WebHelper .get user"; public const string WebHelper_GetUser = "-WebHelper .get user";
@ -50,8 +54,8 @@ namespace FunGame.Desktop.Models.Config
/** /**
* Socket Configs * Socket Configs
*/ */
public static string SERVER_IPADRESS = ""; // 默认IP地址 public static string SERVER_IPADRESS = ""; // 服务器IP地址
public static int SERVER_PORT; // 默认端口 public static int SERVER_PORT = 0; // 服务器端口号
public static Encoding DEFAULT_ENCODING = Encoding.UTF8; 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_CreateTeam = "创建游戏 团队";
public const string FunGame_StartGame = "开始游戏"; public const string FunGame_StartGame = "开始游戏";
public const string FunGame_Connect = "连接服务器"; 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_Retry = "重新连接";
public const string FunGame_AutoRetryOn = "开启自动重连"; public const string FunGame_AutoRetryOn = "开启自动重连";
public const string FunGame_AutoRetryOff = "关闭自动重连"; public const string FunGame_AutoRetryOff = "关闭自动重连";
@ -92,7 +98,7 @@ namespace FunGame.Desktop.Models.Config
public static readonly object[] PresetNoLoginItems = public static readonly object[] PresetNoLoginItems =
{ {
FunGame_PresetMessage, FunGame_PresetMessage,
FunGame_Disconnect, FunGame_DisconnectWhenNotLogin,
FunGame_AutoRetryOn, FunGame_AutoRetryOn,
FunGame_AutoRetryOff FunGame_AutoRetryOff
}; };
@ -100,6 +106,7 @@ namespace FunGame.Desktop.Models.Config
{ {
FunGame_PresetMessage, FunGame_PresetMessage,
FunGame_Connect, FunGame_Connect,
FunGame_ConnectTo,
FunGame_Retry, FunGame_Retry,
FunGame_AutoRetryOn, FunGame_AutoRetryOn,
FunGame_AutoRetryOff FunGame_AutoRetryOff

View File

@ -14,8 +14,5 @@ namespace FunGame.Desktop.Models.Config
*/ */
public static User? LoginUser = null; // 已登录的用户 public static User? LoginUser = null; // 已登录的用户
public static string LoginUserName = ""; // 已登录用户名 public static string LoginUserName = ""; // 已登录用户名
public static bool Match_Mix = false; // 混战模式选项
public static bool Match_Team = false; // 团队模式选项
public static bool Match_HasPass = false; // 密码房间选项
} }
} }

View File

@ -9,6 +9,7 @@ using FunGame.Core.Api.Model.Entity;
using FunGame.Desktop.Models.Config; using FunGame.Desktop.Models.Config;
using FunGame.Desktop.Utils; using FunGame.Desktop.Utils;
using FunGame.Core.Api.Model.Enum; using FunGame.Core.Api.Model.Enum;
using FunGame.Core.Api.Util;
namespace FunGame.Desktop.UI namespace FunGame.Desktop.UI
{ {
@ -192,13 +193,15 @@ namespace FunGame.Desktop.UI
throw new Exception(GetNowShortTime() + "\nERROR无法连接至服务器请检查你的网络连接。"); throw new Exception(GetNowShortTime() + "\nERROR无法连接至服务器请检查你的网络连接。");
else else
throw new Exception("ERROR无法连接至服务器请检查你的网络连接。"); throw new Exception("ERROR无法连接至服务器请检查你的网络连接。");
case Config.WebHelper_LogOut: case Config.WebHelper_Disconnect:
Config.FunGame_isAutoRetry = false;
Config.FunGame_isRetrying = false; Config.FunGame_isRetrying = false;
Config.FunGame_isConnected = false; Config.FunGame_isAutoConnect = false;
Config.FunGame_isAutoLogin = false; Config.FunGame_isAutoLogin = false;
Config.FunGame_isConnected = false;
WebHelper_Action = (main) => WebHelper_Action = (main) =>
{ {
SetServerStatusLight((int)LightType.Red); SetServerStatusLight((int)LightType.Yellow);
SetButtonEnableIfLogon(false, ClientState.WaitConnect); SetButtonEnableIfLogon(false, ClientState.WaitConnect);
LogoutAccount(); LogoutAccount();
}; };
@ -206,7 +209,22 @@ namespace FunGame.Desktop.UI
BeginInvoke(WebHelper_Action, this); BeginInvoke(WebHelper_Action, this);
else else
WebHelper_Action(this); 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; NOW_CONNECTEDRETRY = -1;
Task.Run(() => Task.Run(() =>
@ -299,6 +317,11 @@ namespace FunGame.Desktop.UI
/// </summary> /// </summary>
private void Connect() private void Connect()
{ {
if (Config.SERVER_IPADRESS.Equals("") || Config.SERVER_PORT <= 0)
{
ShowMessage.ErrorMessage("查找可用的服务器失败!");
return;
}
Task.Run(() => Task.Run(() =>
{ {
while (true) while (true)
@ -776,7 +799,7 @@ namespace FunGame.Desktop.UI
// 向消息队列发送消息 // 向消息队列发送消息
if (!TalkText.Text.Trim().Equals("") && !TalkText.ForeColor.Equals(Color.DarkGray)) 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); SwitchTalkMessage(TalkText.Text);
TalkText.Text = ""; TalkText.Text = "";
if (isLeave) TalkText_Leave(); // 回车不离开焦点 if (isLeave) TalkText_Leave(); // 回车不离开焦点
@ -795,7 +818,7 @@ namespace FunGame.Desktop.UI
/// <param name="msg"></param> /// <param name="msg"></param>
private void SendTalkText_Click(string msg) private void SendTalkText_Click(string msg)
{ {
WritelnGameInfo(GetNowShortTime() + " [ " + (!Usercfg.LoginUserName.Equals("") ? Usercfg.LoginUserName : "尚未登录") + " ] 说: " + msg); WritelnGameInfo((!Usercfg.LoginUserName.Equals("") ? GetNowShortTime() + " [ " + Usercfg.LoginUserName + " ] 说: " : ":> ") + msg);
} }
/// <summary> /// <summary>
@ -962,11 +985,11 @@ namespace FunGame.Desktop.UI
WritelnGameInfo(GetNowShortTime() + " 开始匹配"); WritelnGameInfo(GetNowShortTime() + " 开始匹配");
WritelnGameInfo("[ " + Usercfg.LoginUserName + " ] 开始匹配"); WritelnGameInfo("[ " + Usercfg.LoginUserName + " ] 开始匹配");
WriteGameInfo(">> 匹配参数:"); WriteGameInfo(">> 匹配参数:");
if (!Usercfg.Match_Mix && !Usercfg.Match_Team && !Usercfg.Match_HasPass) if (!Config.Match_Mix && !Config.Match_Team && !Config.Match_HasPass)
WritelnGameInfo("无"); WritelnGameInfo("无");
else else
{ {
WriteGameInfo((Usercfg.Match_Mix ? " 混战房间 " : "") + (Usercfg.Match_Team ? " 团队房间 " : "") + (Usercfg.Match_HasPass ? " 密码房间 " : "")); WriteGameInfo((Config.Match_Mix ? " 混战房间 " : "") + (Config.Match_Team ? " 团队房间 " : "") + (Config.Match_HasPass ? " 密码房间 " : ""));
WritelnGameInfo(); WritelnGameInfo();
} }
// 显示停止匹配按钮 // 显示停止匹配按钮
@ -1002,24 +1025,24 @@ namespace FunGame.Desktop.UI
private void CreateRoom_Click(object sender, EventArgs e) private void CreateRoom_Click(object sender, EventArgs e)
{ {
string roomtype = ""; string roomtype = "";
if (Usercfg.Match_Mix && Usercfg.Match_Team) if (Config.Match_Mix && Config.Match_Team)
{ {
ShowMessage.WarningMessage("创建房间不允许同时勾选混战和团队!"); ShowMessage.WarningMessage("创建房间不允许同时勾选混战和团队!");
return; 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; 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; 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; 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; roomtype = Config.GameMode_TeamHasPass;
} }
@ -1138,8 +1161,8 @@ namespace FunGame.Desktop.UI
/// <param name="e"></param> /// <param name="e"></param>
private void CheckMix_CheckedChanged(object sender, EventArgs e) private void CheckMix_CheckedChanged(object sender, EventArgs e)
{ {
if (CheckMix.Checked) Usercfg.Match_Mix = true; if (CheckMix.Checked) Config.Match_Mix = true;
else Usercfg.Match_Mix = false; else Config.Match_Mix = false;
} }
/// <summary> /// <summary>
@ -1149,8 +1172,8 @@ namespace FunGame.Desktop.UI
/// <param name="e"></param> /// <param name="e"></param>
private void CheckTeam_CheckedChanged(object sender, EventArgs e) private void CheckTeam_CheckedChanged(object sender, EventArgs e)
{ {
if (CheckTeam.Checked) Usercfg.Match_Team = true; if (CheckTeam.Checked) Config.Match_Team = true;
else Usercfg.Match_Team = false; else Config.Match_Team = false;
} }
/// <summary> /// <summary>
@ -1160,8 +1183,8 @@ namespace FunGame.Desktop.UI
/// <param name="e"></param> /// <param name="e"></param>
private void CheckHasPass_CheckedChanged(object sender, EventArgs e) private void CheckHasPass_CheckedChanged(object sender, EventArgs e)
{ {
if (CheckHasPass.Checked) Usercfg.Match_HasPass = true; if (CheckHasPass.Checked) Config.Match_HasPass = true;
else Usercfg.Match_HasPass = false; else Config.Match_HasPass = false;
} }
/// <summary> /// <summary>
@ -1376,11 +1399,54 @@ namespace FunGame.Desktop.UI
} }
break; break;
case Config.FunGame_Disconnect: case Config.FunGame_Disconnect:
if (Config.FunGame_isConnected) if (Config.FunGame_isConnected && WebHelper != null)
{ {
WritelnGameInfo(">> 实验性功能。"); WebHelper.WebHelpMethod((int)WebHelperMethod.Disconnect);
} }
break; 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;
} }
} }

View File

@ -61,6 +61,13 @@ namespace FunGame.Desktop.Utils
return true; return true;
} }
return false; return false;
case (int)WebHelperMethod.Disconnect:
if (client != null)
{
Send((int)SocketMessageType.Disconnect, new object[] { Main, client });
return true;
}
return false;
} }
return true; return true;
} }
@ -178,6 +185,11 @@ namespace FunGame.Desktop.Utils
Main.GetMessage(this, Config.WebHelper_LogOut); Main.GetMessage(this, Config.WebHelper_LogOut);
Close(); Close();
return true; return true;
case (int)SocketMessageType.Disconnect:
Main.GetMessage(this, read, true);
Main.GetMessage(this, Config.WebHelper_Disconnect);
Close();
return true;
case (int)SocketMessageType.HeartBeat: case (int)SocketMessageType.HeartBeat:
if (WaitHeartBeat != null && !WaitHeartBeat.IsCompleted) WaitHeartBeat.Wait(1); if (WaitHeartBeat != null && !WaitHeartBeat.IsCompleted) WaitHeartBeat.Wait(1);
Config.WebHelper_HeartBeatFaileds = 0; Config.WebHelper_HeartBeatFaileds = 0;
@ -260,6 +272,11 @@ namespace FunGame.Desktop.Utils
return true; return true;
} }
return false; return false;
case SocketMessageType.Disconnect:
msg = MakeMessage(type, "断开连接");
if (Send(msg, socket) > 0)
return true;
return false;
case SocketMessageType.HeartBeat: case SocketMessageType.HeartBeat:
msg = MakeMessage(type, "心跳检测"); msg = MakeMessage(type, "心跳检测");
if (Send(msg, socket) > 0) if (Send(msg, socket) > 0)
@ -375,7 +392,7 @@ namespace FunGame.Desktop.Utils
private void CreateSendHeartBeatStream() private void CreateSendHeartBeatStream()
{ {
Thread.Sleep(1000); Thread.Sleep(100);
Main.GetMessage(this, "Creating: SendHeartBeatStream...OK"); Main.GetMessage(this, "Creating: SendHeartBeatStream...OK");
while (IsConnected()) while (IsConnected())
{ {
@ -386,7 +403,7 @@ namespace FunGame.Desktop.Utils
private void CreateStreamReader() private void CreateStreamReader()
{ {
Thread.Sleep(1000); Thread.Sleep(100);
Main.GetMessage(this, "Creating: StreamReader...OK"); Main.GetMessage(this, "Creating: StreamReader...OK");
while (IsConnected()) while (IsConnected())
{ {