服务器端提炼一个在线客户端管理类,新增一个发送消息给指定在线的角色的客户端,v1.6.17
This commit is contained in:
@@ -43,7 +43,7 @@ namespace CommonLibrary
|
||||
**************************************************************************/
|
||||
|
||||
|
||||
SoftBasic.FrameworkVersion = new SystemVersion("1.6.16");
|
||||
SoftBasic.FrameworkVersion = new SystemVersion("1.6.17");
|
||||
|
||||
}
|
||||
|
||||
|
||||
80
软件系统服务端模版/BasicSupport/NetAccountManager.cs
Normal file
80
软件系统服务端模版/BasicSupport/NetAccountManager.cs
Normal file
@@ -0,0 +1,80 @@
|
||||
using CommonLibrary;
|
||||
using HslCommunication;
|
||||
using Newtonsoft.Json.Linq;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
|
||||
namespace 软件系统服务端模版.BasicSupport
|
||||
{
|
||||
|
||||
/******************************************************************************************
|
||||
*
|
||||
* 时间:2017年10月16日 21:17:00
|
||||
* 功能:一个服务器端的在线客户端管理器
|
||||
*
|
||||
* ****************************************************************************************/
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 所有在线客户端的管理器,该在线模型是基于NetAccount类扩展的
|
||||
/// </summary>
|
||||
public class NetAccountManager
|
||||
{
|
||||
/// <summary>
|
||||
/// 新增一个在线的客户端
|
||||
/// </summary>
|
||||
/// <param name="account"></param>
|
||||
public void AddOnlineClient(NetAccount account)
|
||||
{
|
||||
hybirdLock.Enter();
|
||||
OnlineClients.Add(account);
|
||||
m_ClientsOnlineCache = JArray.FromObject(OnlineClients).ToString();
|
||||
hybirdLock.Leave();
|
||||
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 根据在线客户端的唯一ID进行移除指定的客户端
|
||||
/// </summary>
|
||||
/// <param name="uniqueId"></param>
|
||||
public void RemoveOnlineClient(string uniqueId)
|
||||
{
|
||||
hybirdLock.Enter();
|
||||
|
||||
int index = -1;
|
||||
for (int i = 0; i < OnlineClients.Count; i++)
|
||||
{
|
||||
if (OnlineClients[i].UniqueId == uniqueId)
|
||||
{
|
||||
index = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (index >= 0)
|
||||
{
|
||||
OnlineClients.RemoveAt(index);
|
||||
}
|
||||
|
||||
m_ClientsOnlineCache = JArray.FromObject(OnlineClients).ToString();
|
||||
|
||||
hybirdLock.Leave();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 缓存的在线客户端信息
|
||||
/// </summary>
|
||||
public string ClientsOnlineCache { get => m_ClientsOnlineCache; }
|
||||
|
||||
|
||||
#region Private Member
|
||||
|
||||
private List<NetAccount> OnlineClients = new List<NetAccount>(); // 所有在线客户端的列表
|
||||
private SimpleHybirdLock hybirdLock = new SimpleHybirdLock(); // 操作列表的混合锁
|
||||
private string m_ClientsOnlineCache = "[]"; // 在线客户端的缓存
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
@@ -14,6 +14,7 @@ using HslCommunication.BasicFramework;
|
||||
using System.Diagnostics;
|
||||
using HslCommunication.LogNet;
|
||||
using HslCommunication;
|
||||
using 软件系统服务端模版.BasicSupport;
|
||||
|
||||
|
||||
/********************************************************************************************
|
||||
@@ -992,7 +993,7 @@ namespace 软件系统服务端模版
|
||||
|
||||
private void Net_socket_server_ClientOffline(AsyncStateOne object1, string object2)
|
||||
{
|
||||
RemoveOnlineClient(object1.ClientUniqueID);
|
||||
netAccountManager.RemoveOnlineClient(object1.ClientUniqueID);
|
||||
net_socket_server.SendAllClients(CommonHeadCode.MultiNetHeadCode.用户下线, object1.ClientUniqueID);
|
||||
|
||||
UserInterfaceMessageRender(DateTime.Now.ToString("MM-dd HH:mm:ss ") + object1.IpAddress + ":" + object1.LoginAlias + " " + object2);
|
||||
@@ -1020,56 +1021,17 @@ namespace 软件系统服务端模版
|
||||
{ "Time", new JValue(DateTime.Now) },
|
||||
{ "FileCount", new JValue(ShareFileContainer.FileCount) },
|
||||
{ "chats", new JValue(Chats_Managment.ToSaveString())},
|
||||
{ "ClientsOnline", new JValue(ClientsOnlineCache) }
|
||||
{ "ClientsOnline", new JValue(netAccountManager.ClientsOnlineCache) }
|
||||
};
|
||||
|
||||
// 发送客户端的初始化数据
|
||||
net_socket_server.Send(object1, CommonHeadCode.MultiNetHeadCode.初始化数据, json.ToString());
|
||||
// 新增到在线客户端的队列中
|
||||
AddOnlineClient(account);
|
||||
netAccountManager.AddOnlineClient(account);
|
||||
// 触发上下线功能
|
||||
UserInterfaceMessageRender(DateTime.Now.ToString("MM-dd HH:mm:ss ") + object1.IpAddress + ":" + object1.LoginAlias + " 上线");
|
||||
}
|
||||
|
||||
private List<NetAccount> OnlineClients = new List<NetAccount>();
|
||||
private SimpleHybirdLock hybirdLock = new SimpleHybirdLock();
|
||||
|
||||
private void AddOnlineClient(NetAccount account)
|
||||
{
|
||||
hybirdLock.Enter();
|
||||
OnlineClients.Add(account);
|
||||
ClientsOnlineCache = JArray.FromObject(OnlineClients).ToString();
|
||||
hybirdLock.Leave();
|
||||
|
||||
}
|
||||
|
||||
private void RemoveOnlineClient(string uniqueId)
|
||||
{
|
||||
hybirdLock.Enter();
|
||||
|
||||
int index = -1;
|
||||
for (int i = 0; i < OnlineClients.Count; i++)
|
||||
{
|
||||
if (OnlineClients[i].UniqueId == uniqueId)
|
||||
{
|
||||
index = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (index >= 0)
|
||||
{
|
||||
OnlineClients.RemoveAt(index);
|
||||
}
|
||||
|
||||
ClientsOnlineCache = JArray.FromObject(OnlineClients).ToString();
|
||||
|
||||
hybirdLock.Leave();
|
||||
}
|
||||
|
||||
private string ClientsOnlineCache = "[]";
|
||||
|
||||
|
||||
|
||||
private void Net_socket_server_AllClientsStatusChange(string data)
|
||||
{
|
||||
@@ -1112,6 +1074,29 @@ namespace 软件系统服务端模版
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 发送数据给在线的指定角色客户端
|
||||
/// </summary>
|
||||
/// <param name="handle">信息句柄</param>
|
||||
/// <param name="message">消息</param>
|
||||
/// <param name="roleCode">角色代码</param>
|
||||
private void SendToClientsByRoleCode(NetHandle handle, string message, string roleCode)
|
||||
{
|
||||
foreach (var m in UserServer.ServerRoles.GetUsernamesByRolename(roleCode))
|
||||
{
|
||||
net_socket_server.SendClientByAlias(m, handle, roleCode);
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region 在线客户端管理器
|
||||
|
||||
/// <summary>
|
||||
/// 所有在线客户端的管理器
|
||||
/// </summary>
|
||||
private NetAccountManager netAccountManager { get; set; } = new NetAccountManager();
|
||||
|
||||
#endregion
|
||||
|
||||
#region 后台计数线程
|
||||
|
||||
@@ -53,6 +53,7 @@
|
||||
<Reference Include="System.Xml" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Compile Include="BasicSupport\NetAccountManager.cs" />
|
||||
<Compile Include="FormServerWindow.cs">
|
||||
<SubType>Form</SubType>
|
||||
</Compile>
|
||||
|
||||
Reference in New Issue
Block a user