mirror of
https://github.com/project-redbud/FunGame-Core.git
synced 2025-12-05 00:06:02 +00:00
添加加入房间框架
This commit is contained in:
parent
81d5903751
commit
ebfd51b761
@ -6,7 +6,7 @@ namespace Milimoe.FunGame.Core.Interface.Base
|
||||
{
|
||||
public System.Net.Sockets.Socket Instance { get; }
|
||||
public int Runtime { get; }
|
||||
public string Token { get; }
|
||||
public Guid Token { get; }
|
||||
public string ServerIP { get; }
|
||||
public int ServerPort { get; }
|
||||
public string ServerName { get; }
|
||||
|
||||
@ -8,33 +8,15 @@ namespace Milimoe.FunGame.Core.Library.Common.Network
|
||||
{
|
||||
public System.Net.Sockets.Socket Instance { get; }
|
||||
public int Runtime { get; } = (int)SocketRuntimeType.Server;
|
||||
public string Token { get; } = "";
|
||||
public Guid Token { get; } = Guid.Empty;
|
||||
public string ServerIP { get; } = "";
|
||||
public int ServerPort { get; } = 0;
|
||||
public string ServerName { get; } = "";
|
||||
public string ServerNotice { get; } = "";
|
||||
public string ClientIP { get; } = "";
|
||||
public string ClientName
|
||||
{
|
||||
get
|
||||
{
|
||||
return _ClientName;
|
||||
}
|
||||
}
|
||||
public bool Connected
|
||||
{
|
||||
get
|
||||
{
|
||||
return Instance != null && Instance.Connected;
|
||||
}
|
||||
}
|
||||
public bool Receiving
|
||||
{
|
||||
get
|
||||
{
|
||||
return _Receiving;
|
||||
}
|
||||
}
|
||||
public string ClientName => _ClientName;
|
||||
public bool Connected => Instance != null && Instance.Connected;
|
||||
public bool Receiving => _Receiving;
|
||||
|
||||
private Task? ReceivingTask;
|
||||
|
||||
@ -58,7 +40,7 @@ namespace Milimoe.FunGame.Core.Library.Common.Network
|
||||
public object[] Receive()
|
||||
{
|
||||
object[] result = SocketManager.Receive(Instance);
|
||||
if (result.Length != 2) throw new SocketWrongInfoException();
|
||||
if (result.Length != 3) throw new SocketWrongInfoException();
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
@ -8,12 +8,12 @@ namespace Milimoe.FunGame.Core.Library.Common.Network
|
||||
public class JsonObject
|
||||
{
|
||||
public SocketMessageType MessageType { get; } = SocketMessageType.Unknown;
|
||||
public string Token { get; }
|
||||
public Guid Token { get; }
|
||||
public object[] Parameters { get; }
|
||||
public string JsonString { get; }
|
||||
|
||||
[JsonConstructor]
|
||||
public JsonObject(SocketMessageType MessageType, string Token, object[] Parameters)
|
||||
public JsonObject(SocketMessageType MessageType, Guid Token, object[] Parameters)
|
||||
{
|
||||
this.MessageType = MessageType;
|
||||
this.Token = Token;
|
||||
@ -21,7 +21,7 @@ namespace Milimoe.FunGame.Core.Library.Common.Network
|
||||
this.JsonString = JsonSerializer.Serialize(this);
|
||||
}
|
||||
|
||||
public static string GetString(SocketMessageType MessageType, string Token, object[] Parameters)
|
||||
public static string GetString(SocketMessageType MessageType, Guid Token, object[] Parameters)
|
||||
{
|
||||
return new JsonObject(MessageType, Token, Parameters).JsonString;
|
||||
}
|
||||
|
||||
@ -9,25 +9,13 @@ namespace Milimoe.FunGame.Core.Library.Common.Network
|
||||
{
|
||||
public System.Net.Sockets.Socket Instance { get; }
|
||||
public int Runtime { get; } = (int)SocketRuntimeType.Server;
|
||||
public string Token { get; } = "";
|
||||
public Guid Token { get; } = Guid.Empty;
|
||||
public string ServerIP { get; } = "";
|
||||
public int ServerPort { get; } = 0;
|
||||
public string ServerName { get; } = "";
|
||||
public string ServerNotice { get; } = "";
|
||||
public bool Connected
|
||||
{
|
||||
get
|
||||
{
|
||||
return Instance != null && Instance.Connected;
|
||||
}
|
||||
}
|
||||
public bool Receiving
|
||||
{
|
||||
get
|
||||
{
|
||||
return _Receiving;
|
||||
}
|
||||
}
|
||||
public bool Connected => Instance != null && Instance.Connected;
|
||||
public bool Receiving => _Receiving;
|
||||
|
||||
private readonly ThreadManager PlayerThreads;
|
||||
private bool _Receiving = false;
|
||||
|
||||
@ -8,39 +8,15 @@ namespace Milimoe.FunGame.Core.Library.Common.Network
|
||||
{
|
||||
public System.Net.Sockets.Socket Instance { get; }
|
||||
public int Runtime { get; } = (int)SocketRuntimeType.Client;
|
||||
public string Token { get; set; } = "";
|
||||
public Guid Token { get; set; } = Guid.Empty;
|
||||
public string ServerIP { get; } = "";
|
||||
public int ServerPort { get; } = 0;
|
||||
public string ServerName { get; } = "";
|
||||
public string ServerNotice { get; } = "";
|
||||
public int HeartBeatFaileds
|
||||
{
|
||||
get
|
||||
{
|
||||
return _HeartBeatFaileds;
|
||||
}
|
||||
}
|
||||
public bool Connected
|
||||
{
|
||||
get
|
||||
{
|
||||
return Instance != null && Instance.Connected;
|
||||
}
|
||||
}
|
||||
public bool Receiving
|
||||
{
|
||||
get
|
||||
{
|
||||
return _Receiving;
|
||||
}
|
||||
}
|
||||
public bool SendingHeartBeat
|
||||
{
|
||||
get
|
||||
{
|
||||
return _SendingHeartBeat;
|
||||
}
|
||||
}
|
||||
public int HeartBeatFaileds => _HeartBeatFaileds;
|
||||
public bool Connected => Instance != null && Instance.Connected;
|
||||
public bool Receiving => _Receiving;
|
||||
public bool SendingHeartBeat => _SendingHeartBeat;
|
||||
|
||||
private Task? SendingHeartBeatTask;
|
||||
private Task? ReceivingTask;
|
||||
|
||||
@ -32,6 +32,8 @@
|
||||
public const string Logout = "Logout";
|
||||
public const string Disconnect = "Disconnect";
|
||||
public const string HeartBeat = "HeartBeat";
|
||||
public const string IntoRoom = "IntoRoom";
|
||||
public const string Chat = "Chat";
|
||||
}
|
||||
|
||||
public class ReflectionSet
|
||||
|
||||
@ -57,7 +57,9 @@
|
||||
CheckLogin = 1 << 4,
|
||||
Logout = 1 << 5,
|
||||
Disconnect = 1 << 6,
|
||||
HeartBeat = 1 << 7
|
||||
HeartBeat = 1 << 7,
|
||||
IntoRoom = 1 << 8,
|
||||
Chat = 1 << 9
|
||||
}
|
||||
|
||||
public enum SocketRuntimeType
|
||||
|
||||
@ -109,4 +109,9 @@
|
||||
{
|
||||
public override string Message => "执行SQL查询时遇到错误 (#10022)";
|
||||
}
|
||||
|
||||
public class CanNotIntoRoomException : Exception
|
||||
{
|
||||
public override string Message => "无法加入指定房间 (#10023)";
|
||||
}
|
||||
}
|
||||
|
||||
@ -110,7 +110,7 @@ namespace Milimoe.FunGame.Core.Service
|
||||
/// <param name="type">通信类型</param>
|
||||
/// <param name="objs">参数</param>
|
||||
/// <returns>通信结果</returns>
|
||||
internal static SocketResult Send(Socket ClientSocket, SocketMessageType type, string token, object[] objs)
|
||||
internal static SocketResult Send(Socket ClientSocket, SocketMessageType type, Guid token, object[] objs)
|
||||
{
|
||||
if (ClientSocket != null && objs != null && objs.Length > 0)
|
||||
{
|
||||
@ -129,7 +129,7 @@ namespace Milimoe.FunGame.Core.Service
|
||||
/// <param name="type">通信类型</param>
|
||||
/// <param name="objs">参数</param>
|
||||
/// <returns>通信结果</returns>
|
||||
internal static SocketResult Send(SocketMessageType type, string token, object[] objs)
|
||||
internal static SocketResult Send(SocketMessageType type, Guid token, object[] objs)
|
||||
{
|
||||
if (objs is null || objs.Length <= 0)
|
||||
{
|
||||
@ -176,7 +176,7 @@ namespace Milimoe.FunGame.Core.Service
|
||||
/// 用于服务器接收客户端信息
|
||||
/// </summary>
|
||||
/// <param name="ClientSocket">客户端Socket</param>
|
||||
/// <returns>通信类型[0]和参数[1]</returns>
|
||||
/// <returns>通信类型[0]、Token[1]和参数[2]</returns>
|
||||
internal static object[] Receive(Socket ClientSocket)
|
||||
{
|
||||
object[] result = Array.Empty<object>();
|
||||
@ -191,7 +191,7 @@ namespace Milimoe.FunGame.Core.Service
|
||||
Library.Common.Network.JsonObject? json = Library.Common.Network.JsonObject.GetObject(msg);
|
||||
if (json != null)
|
||||
{
|
||||
result = new object[] { json.MessageType, json.Parameters };
|
||||
result = new object[] { json.MessageType, json.Token, json.Parameters };
|
||||
}
|
||||
return result;
|
||||
}
|
||||
@ -215,6 +215,8 @@ namespace Milimoe.FunGame.Core.Service
|
||||
SocketMessageType.Logout => SocketSet.Logout,
|
||||
SocketMessageType.Disconnect => SocketSet.Disconnect,
|
||||
SocketMessageType.HeartBeat => SocketSet.HeartBeat,
|
||||
SocketMessageType.IntoRoom => SocketSet.IntoRoom,
|
||||
SocketMessageType.Chat => SocketSet.Chat,
|
||||
_ => SocketSet.Unknown,
|
||||
};
|
||||
}
|
||||
|
||||
@ -71,9 +71,6 @@ namespace Milimoe.FunGame.Desktop.Controller
|
||||
case MainSet.SetRed:
|
||||
break;
|
||||
|
||||
case MainSet.SetUser:
|
||||
break;
|
||||
|
||||
case MainSet.LogOut:
|
||||
Main.OnBeforeLogoutEvent(new GeneralEventArgs());
|
||||
result = MainModel.LogOut();
|
||||
@ -83,6 +80,10 @@ namespace Milimoe.FunGame.Desktop.Controller
|
||||
result = MainModel.Close();
|
||||
break;
|
||||
|
||||
case MainSet.JoinRoom:
|
||||
result = MainModel.IntoRoom();
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
@ -153,5 +154,10 @@ namespace Milimoe.FunGame.Desktop.Controller
|
||||
{
|
||||
return Do<bool>(MainSet.Close);
|
||||
}
|
||||
|
||||
public bool IntoRoom()
|
||||
{
|
||||
return Do<bool>(MainSet.JoinRoom);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -5,7 +5,7 @@ using Milimoe.FunGame.Desktop.Library.Component;
|
||||
|
||||
namespace Milimoe.FunGame.Desktop.Library.Base
|
||||
{
|
||||
public class BaseMain : GeneralForm, IConnectEventHandler, IDisconnectEventHandler, ILogoutEventHandler, IIntoRoomEventHandler, ISendTalkEventHandler,
|
||||
public class BaseMain : GeneralForm, IConnectEventHandler, IDisconnectEventHandler, ILoginEventHandler, ILogoutEventHandler, IIntoRoomEventHandler, ISendTalkEventHandler,
|
||||
ICreateRoomEventHandler, IQuitRoomEventHandler, IStartMatchEventHandler, IStartGameEventHandler, IOpenInventoryEventHandler, IOpenStoreEventHandler
|
||||
{
|
||||
public event IEventHandler.BeforeEventHandler? BeforeConnect;
|
||||
@ -90,6 +90,47 @@ namespace Milimoe.FunGame.Desktop.Library.Base
|
||||
else return EventResult.NoEventImplement;
|
||||
}
|
||||
|
||||
public event IEventHandler.BeforeEventHandler? BeforeLogin;
|
||||
public event IEventHandler.AfterEventHandler? AfterLogin;
|
||||
public event IEventHandler.SucceedEventHandler? SucceedLogin;
|
||||
public event IEventHandler.FailedEventHandler? FailedLogin;
|
||||
|
||||
public EventResult OnBeforeLoginEvent(GeneralEventArgs e)
|
||||
{
|
||||
if (BeforeLogin != null)
|
||||
{
|
||||
return BeforeLogin(this, e);
|
||||
}
|
||||
else return EventResult.NoEventImplement;
|
||||
}
|
||||
|
||||
public EventResult OnAfterLoginEvent(GeneralEventArgs e)
|
||||
{
|
||||
if (AfterLogin != null)
|
||||
{
|
||||
return AfterLogin(this, e);
|
||||
}
|
||||
else return EventResult.NoEventImplement;
|
||||
}
|
||||
|
||||
public EventResult OnSucceedLoginEvent(GeneralEventArgs e)
|
||||
{
|
||||
if (SucceedLogin != null)
|
||||
{
|
||||
return SucceedLogin(this, e);
|
||||
}
|
||||
else return EventResult.NoEventImplement;
|
||||
}
|
||||
|
||||
public EventResult OnFailedLoginEvent(GeneralEventArgs e)
|
||||
{
|
||||
if (FailedLogin != null)
|
||||
{
|
||||
return FailedLogin(this, e);
|
||||
}
|
||||
else return EventResult.NoEventImplement;
|
||||
}
|
||||
|
||||
public event IEventHandler.BeforeEventHandler? BeforeLogout;
|
||||
public event IEventHandler.AfterEventHandler? AfterLogout;
|
||||
public event IEventHandler.SucceedEventHandler? SucceedLogout;
|
||||
|
||||
@ -20,6 +20,7 @@ namespace Milimoe.FunGame.Desktop.Library
|
||||
public const string Connect = ".connect";
|
||||
public const string GetServerConnection = ".getserverconnection";
|
||||
public const string Close = ".close";
|
||||
public const string JoinRoom = ".joinroom";
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
@ -16,8 +16,8 @@ namespace Milimoe.FunGame.Desktop.Library.Interface
|
||||
public void SetGreen();
|
||||
public void SetYellow();
|
||||
public void SetRed();
|
||||
public void SetUser();
|
||||
public bool LogOut();
|
||||
public bool Close();
|
||||
public bool IntoRoom();
|
||||
}
|
||||
}
|
||||
|
||||
@ -242,9 +242,21 @@ namespace Milimoe.FunGame.Desktop.Model
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public void SetUser()
|
||||
public bool IntoRoom()
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
try
|
||||
{
|
||||
if (Socket?.Send(SocketMessageType.IntoRoom, Config.FunGame_Roomid) == SocketResult.Success)
|
||||
return true;
|
||||
else throw new CanNotIntoRoomException();
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Main.GetMessage(e.GetErrorInfo());
|
||||
Main.OnFailedIntoRoomEvent(new GeneralEventArgs());
|
||||
Main.OnAfterIntoRoomEvent(new GeneralEventArgs());
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
@ -316,6 +328,10 @@ namespace Milimoe.FunGame.Desktop.Model
|
||||
Main.UpdateUI(MainSet.SetGreenAndPing);
|
||||
break;
|
||||
|
||||
case SocketMessageType.IntoRoom:
|
||||
SocketHandler_IntoRoom(objs);
|
||||
break;
|
||||
|
||||
case SocketMessageType.Unknown:
|
||||
default:
|
||||
break;
|
||||
@ -339,14 +355,16 @@ namespace Milimoe.FunGame.Desktop.Model
|
||||
private void SocketHandler_Connect(object[] objs)
|
||||
{
|
||||
string msg = "";
|
||||
Guid token = Guid.Empty;
|
||||
if (objs.Length > 0) msg = NetworkUtility.ConvertJsonObject<string>(objs[0])!;
|
||||
string[] strings = msg.Split(';');
|
||||
string ServerName = strings[0];
|
||||
string ServerNotice = strings[1];
|
||||
Config.FunGame_ServerName = ServerName;
|
||||
Config.FunGame_Notice = ServerNotice;
|
||||
if (objs.Length > 1) msg = NetworkUtility.ConvertJsonObject<string>(objs[1])!;
|
||||
Socket!.Token = msg;
|
||||
if (objs.Length > 1) token = NetworkUtility.ConvertJsonObject<Guid>(objs[1]);
|
||||
Socket!.Token = token;
|
||||
Config.Guid_Socket = token;
|
||||
Main.GetMessage($"已连接服务器:{ServerName}。\n\n********** 服务器公告 **********\n\n{ServerNotice}\n\n");
|
||||
// 设置等待登录的黄灯
|
||||
Main.UpdateUI(MainSet.WaitLoginAndSetYellow);
|
||||
@ -433,6 +451,22 @@ namespace Milimoe.FunGame.Desktop.Model
|
||||
Main.OnAfterDisconnectEvent(new GeneralEventArgs());
|
||||
}
|
||||
|
||||
private void SocketHandler_IntoRoom(object[] objs)
|
||||
{
|
||||
string roomid = "";
|
||||
if (objs.Length > 0) roomid = NetworkUtility.ConvertJsonObject<string>(objs[0])!;
|
||||
if (roomid == "-1")
|
||||
{
|
||||
Main.GetMessage($"已连接到公共聊天服务器。");
|
||||
}
|
||||
else
|
||||
{
|
||||
Config.FunGame_Roomid = roomid;
|
||||
}
|
||||
Main.OnSucceedIntoRoomEvent(new GeneralEventArgs());
|
||||
Main.OnAfterIntoRoomEvent(new GeneralEventArgs());
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
|
||||
@ -19,6 +19,7 @@ namespace Milimoe.FunGame.Desktop.UI
|
||||
protected override void BindEvent()
|
||||
{
|
||||
base.BindEvent();
|
||||
SucceedLogin += SucceedLoginEvent;
|
||||
FailedLogin += FailedLoginEvent;
|
||||
}
|
||||
|
||||
@ -77,6 +78,25 @@ namespace Milimoe.FunGame.Desktop.UI
|
||||
public EventResult FailedLoginEvent(object sender, GeneralEventArgs e)
|
||||
{
|
||||
GoToLogin.Enabled = true;
|
||||
RunTime.Main?.OnFailedLoginEvent(e);
|
||||
return EventResult.Success;
|
||||
}
|
||||
|
||||
private EventResult SucceedLoginEvent(object sender, GeneralEventArgs e)
|
||||
{
|
||||
RunTime.Main?.OnSucceedLoginEvent(e);
|
||||
return EventResult.Success;
|
||||
}
|
||||
|
||||
private EventResult BeforeLoginEvent(object sender, GeneralEventArgs e)
|
||||
{
|
||||
RunTime.Main?.OnBeforeLoginEvent(e);
|
||||
return EventResult.Success;
|
||||
}
|
||||
|
||||
private EventResult AfterLoginEvent(object sender, GeneralEventArgs e)
|
||||
{
|
||||
RunTime.Main?.OnAfterLoginEvent(e);
|
||||
return EventResult.Success;
|
||||
}
|
||||
}
|
||||
|
||||
@ -81,6 +81,7 @@ namespace Milimoe.FunGame.Desktop.UI
|
||||
base.BindEvent();
|
||||
FailedConnect += FailedConnectEvent;
|
||||
SucceedConnect += SucceedConnectEvent;
|
||||
SucceedLogin += SucceedLoginEvent;
|
||||
}
|
||||
|
||||
#endregion
|
||||
@ -1174,6 +1175,7 @@ namespace Milimoe.FunGame.Desktop.UI
|
||||
/// <returns></returns>
|
||||
public EventResult FailedConnectEvent(object sender, GeneralEventArgs e)
|
||||
{
|
||||
// 自动重连
|
||||
if (Config.FunGame_isConnected && Config.FunGame_isAutoRetry && CurrentRetryTimes <= MaxRetryTimes)
|
||||
{
|
||||
Task.Run(() =>
|
||||
@ -1203,6 +1205,19 @@ namespace Milimoe.FunGame.Desktop.UI
|
||||
return EventResult.Success;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 登录成功后触发事件
|
||||
/// </summary>
|
||||
/// <param name="sender"></param>
|
||||
/// <param name="e"></param>
|
||||
/// <returns></returns>
|
||||
private EventResult SucceedLoginEvent(object sender, GeneralEventArgs e)
|
||||
{
|
||||
// 接入-1号房间聊天室
|
||||
if (MainController?.IntoRoom() ?? false) return EventResult.Success;
|
||||
else return EventResult.Fail;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region 工具方法
|
||||
@ -1300,8 +1315,7 @@ namespace Milimoe.FunGame.Desktop.UI
|
||||
if (Config.FunGame_isConnected && MainController != null)
|
||||
{
|
||||
// 先退出登录再断开连接
|
||||
bool? @bool = MainController?.LogOut();
|
||||
if (@bool ?? false) MainController?.Disconnect();
|
||||
if (MainController?.LogOut() ?? false) MainController?.Disconnect();
|
||||
}
|
||||
break;
|
||||
case Constant.FunGame_DisconnectWhenNotLogin:
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user