mirror of
https://github.com/project-redbud/FunGame-Server.git
synced 2025-04-23 12:39:36 +08:00
重复登录时挤掉账号
This commit is contained in:
parent
a4f612f648
commit
eaa1b7d228
@ -24,6 +24,7 @@ namespace Milimoe.FunGame.Server.Model
|
|||||||
private Guid CheckLoginKey = Guid.Empty;
|
private Guid CheckLoginKey = Guid.Empty;
|
||||||
private string UserName = "";
|
private string UserName = "";
|
||||||
private string Password = "";
|
private string Password = "";
|
||||||
|
private int FailedTimes = 0; // 超过一定次数断开连接
|
||||||
|
|
||||||
public ServerModel(ClientSocket socket, bool running)
|
public ServerModel(ClientSocket socket, bool running)
|
||||||
{
|
{
|
||||||
@ -31,9 +32,7 @@ namespace Milimoe.FunGame.Server.Model
|
|||||||
Running = running;
|
Running = running;
|
||||||
}
|
}
|
||||||
|
|
||||||
private int FailedTimes = 0; // 超过一定次数断开连接
|
public bool Read(ClientSocket socket)
|
||||||
|
|
||||||
private bool Read(ClientSocket socket)
|
|
||||||
{
|
{
|
||||||
// 接收客户端消息
|
// 接收客户端消息
|
||||||
try
|
try
|
||||||
@ -91,8 +90,11 @@ namespace Milimoe.FunGame.Server.Model
|
|||||||
Guid checkloginkey = NetworkUtility.ConvertJsonObject<Guid>(args[0]);
|
Guid checkloginkey = NetworkUtility.ConvertJsonObject<Guid>(args[0]);
|
||||||
if (CheckLoginKey.Equals(checkloginkey))
|
if (CheckLoginKey.Equals(checkloginkey))
|
||||||
{
|
{
|
||||||
// 添加至玩家列表
|
// 创建User对象
|
||||||
User = Factory.New<User>(UserName, Password);
|
User = Factory.New<User>(UserName, Password);
|
||||||
|
// 检查有没有重复登录的情况
|
||||||
|
KickUser();
|
||||||
|
// 添加至玩家列表
|
||||||
AddUser();
|
AddUser();
|
||||||
GetUserCount();
|
GetUserCount();
|
||||||
return Send(socket, type, UserName, Password);
|
return Send(socket, type, UserName, Password);
|
||||||
@ -138,7 +140,7 @@ namespace Milimoe.FunGame.Server.Model
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private bool Send(ClientSocket socket, SocketMessageType type, params object[] objs)
|
public bool Send(ClientSocket socket, SocketMessageType type, params object[] objs)
|
||||||
{
|
{
|
||||||
// 发送消息给客户端
|
// 发送消息给客户端
|
||||||
try
|
try
|
||||||
@ -173,9 +175,11 @@ namespace Milimoe.FunGame.Server.Model
|
|||||||
|
|
||||||
private void KickUser()
|
private void KickUser()
|
||||||
{
|
{
|
||||||
if (User != null)
|
if (User != null && Config.OnlinePlayers.ContainsKey(User.Username))
|
||||||
{
|
{
|
||||||
ServerHelper.WriteLine("OnlinePlayers: 玩家 " + User.Username + " 重复登录!");
|
ServerHelper.WriteLine("OnlinePlayers: 玩家 " + User.Username + " 重复登录!");
|
||||||
|
Config.OnlinePlayers.TryGetValue(User.Username, out ServerModel? serverTask);
|
||||||
|
serverTask?.Send(serverTask.Socket!, SocketMessageType.Logout, serverTask.CheckLoginKey, "您的账号在别处登录,已强制下线。");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -183,28 +187,21 @@ namespace Milimoe.FunGame.Server.Model
|
|||||||
{
|
{
|
||||||
if (User != null)
|
if (User != null)
|
||||||
{
|
{
|
||||||
if (!Config.OnlinePlayers.ContainsKey(User.Username))
|
if (this != null)
|
||||||
{
|
{
|
||||||
if (Task != null)
|
Config.OnlinePlayers.AddOrUpdate(User.Username, this, (key, value) => value);
|
||||||
{
|
|
||||||
Config.OnlinePlayers.AddOrUpdate(User.Username, Task, (key, value) => value);
|
|
||||||
ServerHelper.WriteLine("OnlinePlayers: 玩家 " + User.Username + " 已添加");
|
ServerHelper.WriteLine("OnlinePlayers: 玩家 " + User.Username + " 已添加");
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else
|
|
||||||
{
|
|
||||||
KickUser();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
private bool RemoveUser()
|
private bool RemoveUser()
|
||||||
{
|
{
|
||||||
if (Task != null && User != null)
|
if (this != null && User != null)
|
||||||
{
|
{
|
||||||
if (Config.OnlinePlayers.TryRemove(User.Username, out Task))
|
if (Config.OnlinePlayers.TryRemove(User.Username, out _))
|
||||||
{
|
{
|
||||||
ServerHelper.WriteLine("OnlinePlayers: 玩家 " + User.Username + " 已移除");
|
ServerHelper.WriteLine("OnlinePlayers: 玩家 " + User.Username + " 已移除");
|
||||||
User = null;
|
User = null;
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
using Milimoe.FunGame.Core.Library.Constant;
|
using Milimoe.FunGame.Core.Library.Constant;
|
||||||
|
using Milimoe.FunGame.Server.Model;
|
||||||
using System.Collections;
|
using System.Collections;
|
||||||
using System.Collections.Concurrent;
|
using System.Collections.Concurrent;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
@ -29,13 +30,13 @@ namespace Milimoe.FunGame.Server.Others
|
|||||||
/// string: 玩家标识ID
|
/// string: 玩家标识ID
|
||||||
/// Task:玩家线程
|
/// Task:玩家线程
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public static ConcurrentDictionary<string, Task> OnlinePlayers { get; } = new ConcurrentDictionary<string, Task>();
|
public static ConcurrentDictionary<string, ServerModel> OnlinePlayers { get; } = new ConcurrentDictionary<string, ServerModel>();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* string:房间号
|
* string:房间号
|
||||||
* Task:玩家线程
|
* Task:玩家线程
|
||||||
*/
|
*/
|
||||||
public static ConcurrentDictionary<string, Task> PlayingPlayers { get; } = new ConcurrentDictionary<string, Task>();
|
public static ConcurrentDictionary<string, ServerModel> PlayingPlayers { get; } = new ConcurrentDictionary<string, ServerModel>();
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user