chronoxor / NetCoreServer

Ultra fast and low latency asynchronous socket server & client C# .NET Core library with support TCP, SSL, UDP, HTTP, HTTPS, WebSocket protocols and 10K connections problem solution
https://chronoxor.github.io/NetCoreServer
MIT License
2.63k stars 550 forks source link

how to get a list of sessions #262

Closed romanhamidulin closed 1 year ago

romanhamidulin commented 1 year ago

TcpServer have Sessions but it protected readonly ConcurrentDictionary<Guid, TcpSession> Sessions = new ConcurrentDictionary<Guid, TcpSession>(); and i have own session witn additional properties protected override TcpSession CreateSession() { return new TeleofisSession(this); } How can I get a list of sessions and then refer to a specific session to send a package to it?

TailyFair commented 1 year ago

You can utilize inheritance to achieve your wanted behavior. Maybe something like this could work?

public class CustomSession : TcpSession
{
    public CustomSession(TcpServer server) : base(server)
    {
        CustomProperty = "Something";
    }

    public string CustomProperty { get; set; }

    public void DoCustomAction()
    {
        Console.WriteLine(CustomProperty);
    }
}

public class CustomServer : TcpServer
{
    public CustomServer(IPAddress address, int port) : base(address, port)
    {
    }

    protected override TcpSession CreateSession() => new CustomSession(this);

    public void CustomAction()
    {
        var session = Sessions
            .FirstOrDefault(x => ((CustomSession)x.Value).CustomProperty == "Something");
        ((CustomSession)session.Value)?.DoCustomAction();
    }
}
romanhamidulin commented 1 year ago

thanks for the idea, but I needed to get a list of sessions on the web and I did the following

 public class TeleofisSession : TcpSession
{ 
        public string Imei { get; set; }
        public string status { get; set; }
        public bool Cancel { get; set; }
        public DateTime LastActiveTime { get; set; } = DateTime.Now;

        public Guid execId { get; private set; }
        private Bus bus;
        public TeleofisSession(TcpServer server) : base(server)
        {

        }
        protected override void OnConnected()
        {}
        protected override void OnDisconnected()
        {}
        protected override void OnReceived(byte[] buffer, long offset, long size)
        {}

}
public class TeleofisSocketServer : TcpServer
    {
        private static readonly Logger logger = LogManager.GetCurrentClassLogger();
        public ConcurrentDictionary<Guid, TcpSession> sessions = new ConcurrentDictionary<Guid, TcpSession>();
        public TeleofisSocketServer(IPAddress address, int port) : base(address, port) 
      {

            sessions = Sessions; 
        }
        protected override TcpSession CreateSession() { return new TeleofisSession(this); }

        protected override void OnError(SocketError error)
        {
            logger.Debug("Ошибка: {0}", error);
            //Console.WriteLine($"Chat TCP server caught an error with code {error}");
        }
    }

And in the following way we output to the web using Nancy

 public ApiModule(TeleofisSocketServer sos)
{
           Get("sessions", (_) =>
            {
                return Response.AsJson(sos.sessions.Select(s =>
                {
                    dynamic mock = new ExpandoObject();
                    mock.sid = ((TeleofisSession)s.Value).execId;
                    mock.imei = ((TeleofisSession)s.Value).Imei;
                    mock.lastActive = ((TeleofisSession)s.Value).LastActiveTime;
                    mock.ip = ((IPEndPoint)(((TeleofisSession)s.Value).Socket.RemoteEndPoint)).Address.ToString();
                    mock.id = ((TeleofisSession)s.Value).Id;
                    return mock;
                }));
            });
}