public async void Connect()
        {
            try
            {
                await Task.Run(async() =>
                {
                    var task = socket.ConnectAsync();
                    if (await Task.WhenAny(task, Task.Delay(this.ReconnectionDelay)) == task)
                    {
                        if (task.IsFaulted)
                        {
                            throw task.Exception;
                        }
                    }
                    else
                    {
                        ChangeMonitorPriority();
                        this.Reconnect();
                    }
                });
            }
            catch (Exception ex)
            {
                ChangeMonitorPriority();
                this.Reconnect();

                VoicenterRealtimeLogger logger = new VoicenterRealtimeLogger();
                logger.ex      = ex;
                logger.message = $" in Connect() method, socket {socket?.ServerUri}";
                logger.level   = LogLevel.Error;
                Logger.log(this, logger);
            }
        }
 public void Dispose()
 {
     try
     {
         if (keepAliveTimer != null)
         {
             keepAliveTimer.Elapsed -= OnTimedEvent;
             keepAliveTimer          = null;
         }
         //await socket.DisconnectAsync();
         if (socket != null)
         {
             monitorURIList.Clear();
             OnEvent                = null;
             socket.OnConnected    -= Socket_OnConnected;
             socket.OnDisconnected -= Socket_OnDisconnected;
             socket = null;
         }
     }
     catch (Exception ex)
     {
         VoicenterRealtimeLogger logger = new VoicenterRealtimeLogger();
         logger.ex      = ex;
         logger.message = $" in Dispose() method, socket {socket?.ServerUri}";
         logger.level   = LogLevel.Error;
         Logger.log(this, logger);
     }
 }
        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 static void log(object sender, VoicenterRealtimeLogger e)
 {
     if (logLevel <= e.level)
     {
         onLog(sender, e);
     }
 }
 public void ChangeMonitorPriority()
 {
     try
     {
         currentMonitorURI = this.monitorURIList.Dequeue();
         this.monitorURIList.Enqueue(currentMonitorURI);
     }
     catch (Exception ex)
     {
         VoicenterRealtimeLogger logger = new VoicenterRealtimeLogger();
         logger.ex      = ex;
         logger.message = $"in changeMonitorPriority() method, socket {socket?.ServerUri}";
         logger.level   = LogLevel.Error;
         Logger.log(this, logger);
     }
 }
 public async Task StopListenning()
 {
     try
     {
         if (socket != null)
         {
             this.ListenerStatus = ListenerStatus.Disconnected;
             await socket.DisconnectAsync();
         }
     }
     catch (Exception ex)
     {
         VoicenterRealtimeLogger logger = new VoicenterRealtimeLogger();
         logger.ex      = ex;
         logger.message = $" in StopListenning() method, socket {socket?.ServerUri}";
         logger.level   = LogLevel.Error;
         Logger.log(this, logger);
     }
 }
        private void OnTimedEvent(Object source, ElapsedEventArgs e)
        {
            Task.Run(async() =>
            {
                try
                {
                    var UtcNow = DateTime.UtcNow;
                    var LastKeepAliveEventMilliseconds = (int)UtcNow.Subtract(this.LastKeepAliveEvent).TotalMilliseconds;
                    var LastfirstPriorityMilliseconds  = (int)UtcNow.Subtract(this.firstPrioritymonitorConnected).TotalMilliseconds;

                    if (socket.Connected && socket.ServerUri != _firstPrioritymonitor && LastfirstPriorityMilliseconds > this.KeepAliveInterval * 6)
                    {
                        monitorURIList                     = this.monitorURIListByDefault;
                        this.currentMonitorURI             = _firstPrioritymonitor;
                        this.firstPrioritymonitorConnected = DateTime.UtcNow;
                        this.Reconnect();
                    }
                    else if (socket.Connected && LastKeepAliveEventMilliseconds > this.KeepAliveInterval * 2)
                    {
                        this.ChangeMonitorPriority();
                        this.Reconnect();
                    }
                    else
                    {
                        await socket.EmitAsync("keepalive");
                    }
                }
                catch (Exception ex)
                {
                    VoicenterRealtimeLogger logger = new VoicenterRealtimeLogger();
                    logger.ex      = ex;
                    logger.message = $"in OnTimedEvent() method, socket {socket?.ServerUri}";
                    logger.level   = LogLevel.Error;
                    Logger.log(this, logger);
                }
            });
        }
        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;
            }
        }