
using System.Threading.Tasks;
using MrWu.Debug;
using WebSocketSharp;
using WebSocketSharp.Server;

namespace Server.Net
{
    public abstract class BaseWChannel : WebSocketBehavior
    {
        
        public bool Active
        {
            get { return ReadyState == WebSocketState.Open; }
        }
        
        public string RemoteEndPointIP {
            get
            {
                string ipAddress = null;
                if (this.Headers["X-Forwarded-For"] != null)
                {
                    ipAddress = this.Headers["X-Forwarded-For"];
                }else if (this.Headers["X-Real-IP"] != null)
                {
                    ipAddress = this.Headers["X-Real-IP"];
                }

                if (ipAddress == null)
                {
                    return this.UserEndPoint.Address.ToString();
                }

                if (ipAddress.IndexOf(',') >= 0)
                {
                    string[] ss = ipAddress.Split(',');
                    ipAddress = ss[0];
                }

                return ipAddress.Trim();
            }
        }
        
        public void BeginDisconnect()
        {
            Task.Factory.StartNew(Close);
        }
    }
}