private async Task OnServerMonitorNotificationAsync(Socket.Messages.Message message)
        {
            try
            {
                var serverMonitorNotifications = JsonConvert.DeserializeObject <List <Core.Server.ServerNotification> >(message.Data);

                if (serverMonitorNotifications.Any(smn => smn.Equals(Core.Server.ServerNotificationLevel.DisconnectClient)))
                {
                    await DisposeSocketAsync().ConfigureAwait(false);
                }
                else
                {
                    var serverMonitorNotification = serverMonitorNotifications.OrderByDescending(smn => smn.Timestamp).First();

                    var serverMonitor = JsonConvert.DeserializeObject <Core.Server.ServerMonitor>(serverMonitorNotification.Message);

                    ServerMonitorHelper.UpdateServerMonitor(this, serverMonitor);

                    OnNotification();
                }
            }
            catch (Exception ex)
            {
                OnException(ex.Message, ex);
            }
        }
Exemple #2
0
        public async Task ConnectAsync(Dispatcher dispatcher)
        {
            await serverMonitorSemaphoreSlim.WaitAsync().ConfigureAwait(false);

            try
            {
                if (socketClient != null)
                {
                    return;
                }

                if (IsConnected ||
                    IsConnecting ||
                    string.IsNullOrWhiteSpace(Uri.ToString()) ||
                    !Enabled)
                {
                    return;
                }

                var serverIsRunning = await IsServerRunningAsync().ConfigureAwait(false);

                if (!serverIsRunning)
                {
                    return;
                }

                IsConnecting = true;

                socketClient = new SocketClient(new Uri(Uri, "serverhub"), Environment.UserName);

                socketClient.On("OnConnected", message =>
                {
                    dispatcher.Invoke(() =>
                    {
                        IsConnecting = false;
                        IsConnected  = true;
                        OnNotification($"{Name} connected.");
                    });
                });

                socketClient.On("OnNotification", async(message) =>
                {
                    await dispatcher.Invoke(async() =>
                    {
                        await OnServerMonitorNotificationAsync(message).ConfigureAwait(false);
                    }).ConfigureAwait(false);
                });

                socketClient.Closed += async(sender, args) =>
                {
                    await dispatcher.Invoke(async() =>
                    {
                        ServerMonitorHelper.UpdateServerMonitor(this, new Core.Server.ServerMonitor());
                        OnNotification($"{Name} disconnected.");
                        await DisposeSocketAsync().ConfigureAwait(false);
                    }).ConfigureAwait(false);
                };

                socketClient.Error += async(sender, args) =>
                {
                    var ex = args as Exception;
                    if (ex.InnerException is TaskCanceledException)
                    {
                        await DisposeSocketAsync().ConfigureAwait(false);
                    }
                    else
                    {
                        await dispatcher.Invoke(async() =>
                        {
                            OnException(args.Message, new Exception(args.Message));
                            await DisposeSocketAsync().ConfigureAwait(false);
                        }).ConfigureAwait(false);
                    }
                };

                await socketClient.StartAsync(Name).ConfigureAwait(false);
            }
            catch (WebSocketException)
            {
                await DisposeSocketAsync().ConfigureAwait(false);
            }
            catch (Exception ex)
            {
                await DisposeSocketAsync().ConfigureAwait(false);

                OnException(ex.Message, ex);
            }
            finally
            {
                serverMonitorSemaphoreSlim.Release();
            }
        }