private async void Socket_OnDisconnected(dynamic sender, string e)
        {
            VoicenterRealtimeResponseArgs res = new VoicenterRealtimeResponseArgs();

            try
            {
                await Task.Run(async() =>
                {
                    this.ListenerStatus = ListenerStatus.Disconnected;
                    if (this.currentRetries < this.ReconnectionAttempts)
                    {
                        this.currentRetries++;
                    }
                    else
                    {
                        this.currentRetries = 0;
                        this.ChangeMonitorPriority();
                    }
                    res.Name = EventTypes.DISCONNECTED;

                    SocketServerUri = socket.ServerUri;
                    OnEvent(this, res);
                    this.Reconnect();
                });
            }
            catch (Exception ex)
            {
                VoicenterRealtimeLogger logger = new VoicenterRealtimeLogger();
                logger.ex      = ex;
                logger.message = $"in Socket_OnDisconnected() method, socket {socket?.ServerUri}";
                logger.level   = LogLevel.Error;
                Logger.log(this, logger);
            }
        }
 private async void Socket_OnConnected(object sender, EventArgs e)
 {
     try
     {
         await Task.Run(() =>
         {
             if (OnEvent != null)
             {
                 VoicenterRealtimeResponseArgs res = new VoicenterRealtimeResponseArgs();
                 res.Name                = EventTypes.CONNECTED;
                 res.Id                  = socket.Id;
                 SocketServerUri         = socket.ServerUri;
                 this.LastKeepAliveEvent = DateTime.UtcNow;
                 this._InitKeepAlive();
                 this.ListenerStatus = ListenerStatus.Connected;
                 OnEvent(this, res);
             }
         });
     }
     catch (Exception ex) {
         VoicenterRealtimeLogger logger = new VoicenterRealtimeLogger();
         logger.ex      = ex;
         logger.message = $"in Socket_OnConnected() method, socket {socket?.ServerUri}";
         logger.level   = LogLevel.Error;
         Logger.log(this, logger);
     }
 }
        public void RefreshToken(string token, List <Uri> uriList)
        {
            if (!String.IsNullOrEmpty(token))
            {
                this.Token = token;
            }
            this.UpdateMonitorUriList(uriList);
            VoicenterRealtimeResponseArgs res = new VoicenterRealtimeResponseArgs();

            res.Name = EventTypes.TokenRefreshed;
            res.Data = this.Token;
            OnEvent(this, res);
            Reconnect();
        }
        public async void Reconnect()
        {
            VoicenterRealtimeResponseArgs res = new VoicenterRealtimeResponseArgs();

            if (this.Reconnection)
            {
                res.Name            = EventTypes.RECONNECTING;
                this.ListenerStatus = ListenerStatus.Reconnecting;
                await this.StopListenning();

                SocketServerUri = this.currentMonitorURI;
                OnEvent(this, res);
                await Task.Delay(this.ReconnectionDelay);

                this.Listen();
            }
        }
        public void Listen()
        {
            try
            {
                var options = new SocketIOOptions();

                Dictionary <String, String> query = new Dictionary <String, String>();
                query.Add("token", this.Token);
                options.Query = query;


                options.Reconnection = false;

                if (monitorURIList.Count == 0)
                {
                    monitorURIList.Enqueue(VoicenterRealtimeListener.monitorURI);
                }
                socket = new SocketIO(this.currentMonitorURI, options);
                //await socket.ConnectAsync();
                socket.OnConnected    += Socket_OnConnected;
                socket.OnDisconnected += Socket_OnDisconnected;


                socket.On("loginStatus", async(data) =>
                {
                    await Task.Run(() =>
                    {
                        if (this.OnEvent != null)
                        {
                            VoicenterRealtimeResponseArgs res = new VoicenterRealtimeResponseArgs();
                            res.Name = EventTypes.loginStatus;
                            res.Data = data.GetValue <Object>();
                            OnEvent(this, res);
                        }
                    });
                });

                socket.On("AllExtensionsStatus", async(data) =>
                {
                    await Task.Run(() =>
                    {
                        if (this.OnEvent != null)
                        {
                            VoicenterRealtimeResponseArgs res = new VoicenterRealtimeResponseArgs();
                            res.Name = EventTypes.AllExtensionsStatus;
                            res.Data = data.GetValue <Object>();
                            OnEvent(this, res);
                        }
                    });
                });

                socket.On("ExtensionEvent", async(data) =>
                {
                    await Task.Run(() =>
                    {
                        if (this.OnEvent != null)
                        {
                            VoicenterRealtimeResponseArgs res = new VoicenterRealtimeResponseArgs();
                            res.Name = EventTypes.ExtensionEvent;
                            res.Data = data.GetValue <Object>();
                            OnEvent(this, res);
                        }
                    });
                });

                socket.On("loginSuccess", async(data) =>
                {
                    await Task.Run(() =>
                    {
                        if (this.OnEvent != null)
                        {
                            VoicenterRealtimeResponseArgs res = new VoicenterRealtimeResponseArgs();
                            res.Name = EventTypes.loginSuccess;
                            res.Data = data.GetValue <Object>();
                            OnEvent(this, res);
                        }
                    });
                });

                socket.On("QueueEvent", async(data) =>
                {
                    await Task.Run(() =>
                    {
                        if (this.OnEvent != null)
                        {
                            VoicenterRealtimeResponseArgs res = new VoicenterRealtimeResponseArgs();
                            res.Name = EventTypes.QueueEvent;
                            res.Data = data.GetValue <Object>();
                            OnEvent(this, res);
                        }
                    });
                });


                socket.On("ExtensionsUpdated", async(data) =>
                {
                    await Task.Run(() =>
                    {
                        if (this.OnEvent != null)
                        {
                            VoicenterRealtimeResponseArgs res = new VoicenterRealtimeResponseArgs();
                            res.Name = EventTypes.ExtensionsUpdated;
                            res.Data = data.GetValue <Object>();
                            this.Resync(false);
                            OnEvent(this, res);
                        }
                    });
                });

                socket.On("QueuesUpdated", async(data) =>
                {
                    await Task.Run(() =>
                    {
                        if (this.OnEvent != null)
                        {
                            VoicenterRealtimeResponseArgs res = new VoicenterRealtimeResponseArgs();
                            res.Name = EventTypes.QueuesUpdated;
                            res.Data = data.GetValue <Object>();
                            this.Resync(false);
                            OnEvent(this, res);
                        }
                    });
                });

                socket.On("keepaliveResponse", async(data) =>
                {
                    await Task.Run(() =>
                    {
                        if (this.OnEvent != null)
                        {
                            VoicenterRealtimeResponseArgs res = new VoicenterRealtimeResponseArgs();
                            res.Name = EventTypes.keepaliveResponse;
                            this.LastKeepAliveEvent = DateTime.UtcNow;
                            res.Data = data.GetValue <Object>();
                            OnEvent(this, res);
                        }
                    });
                });

                this.Connect();
            }
            catch (Exception ex)
            {
                VoicenterRealtimeLogger logger = new VoicenterRealtimeLogger();
                logger.ex      = ex;
                logger.message = $"in Listen() method, socket {socket?.ServerUri}";
                logger.level   = LogLevel.Error;
                Logger.log(this, logger);

                IsTokenNotValid = true;
            }
        }