using System; using System.Collections.Concurrent; using System.Collections.Generic; using System.ComponentModel; using System.Linq; using System.Text; using System.Threading.Tasks; using Fleck; namespace DB { /// <summary> /// 客户端网络状态 /// </summary> /// public enum NetStateEnum { [Description("已连接")] Connected = 1, [Description("已发送")] SendData = 2, [Description("已接收")] ReceiveData = 3, [Description("已解析")] ParseData = 4, [Description("已离线")] Disconnected = 5, [Description("Reported Timeout ")] ReportTimeout =. 6 } /// <Summary> /// recorded every Socket connection /// </ Summary> public class SocketConnectionInfo { Private SocketDataBufferSize const int = 1024; // if the single packet value is greater than the number of data bytes is necessary to increase this value, so sub /// <Summary> /// constructor and a socket to a customer number /// </ Summary> /// <param name = "socket"> socket </ param> /// <param name = "connectionld"> device unique number </ param> Internal SocketConnectionInfo (IWebSocketConnection socket, String connectionld) { ConnectionId = connectionld; CurrentSocket = socket; the DataBuffer = new byte [SocketDataBufferSize]; } /// <summary> Destructor /// /// </ Summary> ~ SocketConnectionInfo () { DataBuffer = null; CurrentSocket = null; } public void the Close () { IF (! = Null CurrentSocket / CurrentSocket.Connected && * * /) { CurrentSocket.Close (); } } /// <Summary> /// client socket /// </ Summary> public IWebSocketConnection CurrentSocket {GET; SET;} /// <Summary> /// buffer to store the data transmitted by the client /// </ Summary> public byte [] {DataBuffer GET; SET;} /// <Summary> /// current number of data bytes actually received, used in conjunction with the attribute DataBuffer data actually received to determine /// </ Summary> /// {int DataBufferLen GET public; SET;} /// <Summary> system time when the received data /// /// </ Summary> /// <Summary> GET ReceivedTimeFromServer the DateTime {public; SET;} /// <Summary> /// data received in the last collection time /// </ Summary> /// public ReceivedTimeFromClient the DateTime {GET; SET;} /// < Summary> /// is online /// </ Summary> public BOOL the IsAlive { GET { iF (CurrentSocket = null!) { return CurrentSocket.IsAvailable; } the else { return to false; } } } /// identifier for the socket connection /// </ Summary> public String {ConnectionId GET; SET;} public {NetStateEnum NetDataState GET; SET;} public byte [] {GET LastUnParsedBytes; SET;} // cache is not the last analytical data buffers per connection public Object ParsedEntity {GET; SET;} /// <Summary> /// is off, changed by the user manually. After disabling the connection process which does not receive data, receive data directly discarded. Prevent high frequency transmitting client data invalid data. /// </ Summary> public BOOL isDisabled {GET; SET;} public {BOOL IsLoggedIn GET; SET;} // if the login is successful, subsequent communication can only log in case of success interact /// <Summary> // / current terminal ID to be used, are likely to modify each communication /// </ Summary> public String CurrentPileCode {GET; SET;} public String the DeviceId {GET; SET;} public String TemporaryHint {GET; SET;} public String {GET MessageTypeName; Internal SET;} } public class SocketConnectionInfoFactory { /// <summary> /// 所有客户端Socket连接的集合,通过socket对象索引 /// </summary> private ConcurrentDictionary<IWebSocketConnection, SocketConnectionInfo> dictionary = new ConcurrentDictionary<IWebSocketConnection, SocketConnectionInfo>(); public ConcurrentDictionary<IWebSocketConnection, SocketConnectionInfo> GetItems() { return dictionary; } public SocketConnectionInfo BindSocketConnectionInfo(IWebSocketConnection socket, string connectionId) { SocketConnectionInfo socketConnectionInfo; if (dictionary.ContainsKey(socket)) { socketConnectionInfo = dictionary[socket]; } else { socketConnectionInfo = new SocketConnectionInfo(socket, connectionId); dictionary.TryAdd(socket, socketConnectionInfo); } return socketConnectionInfo; } public SocketConnectionInfo GetSocketConnectionInfo(string uniqueId) { SocketConnectionInfo socketConnectionInfo = null; foreach (var item in dictionary) { if (string.Compare(item.Value?.ConnectionId, uniqueId, true) == 0) { if (item.Value.ReceivedTimeFromServer >= socketConnectionInfo?.ReceivedTimeFromServer) { Remove(socketConnectionInfo.CurrentSocket); } else { socketConnectionInfo = item.Value; } } } return socketConnectionInfo; } public void Remove(IWebSocketConnection socket) { if (socket != null) { dictionary.TryRemove(socket, out SocketConnectionInfo value); try { //判断此连接是否可用 if (socket.IsAvailable) { socket.Close(); } } catch { throw; } socket.Close(); value = null; } } public void RemoveAll() { foreach (var item in dictionary) { item.Value?.Close(); } dictionary.Clear(); } public int Count { get { if (dictionary == null) { return 0; } return dictionary.Count; } } } }
Called
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace DB { public class DBHelper { private static object obj = new object(); private static SocketConnectionInfoFactory webSocket = null; public static SocketConnectionInfoFactory GetInstance() { if (webSocket == null) { lock (obj) { if (webSocket == null) { webSocket = new SocketConnectionInfoFactory(); } } } return webSocket; } } }