private async void OnConnect(object arg) { try { dipSocketClient = new DipSocketClient(@"ws://localhost:6000/chat", arg.ToString()); dipSocketClient.Closed += DipSocketClientClosed;; dipSocketClient.Error += DipSocketClientError; dipSocketClient.On("OnConnected", (result) => { User = JsonConvert.DeserializeObject <ConnectionInfo>(result.Data); }); dipSocketClient.On("OnMessageReceived", (result) => { OnMessageReceived(result); }); dipSocketClient.On("OnMessageError", (result) => { OnMessageError(result); }); dipSocketClient.On("OnServerInfo", (result) => { OnServerInfo(result); }); await dipSocketClient.StartAsync(); } catch (Exception ex) { Errors.Add(new Error { Message = ex.Message, Verbose = ex.ToString() }); } }
public async Task ConnectAsync(Dispatcher dispatcher) { try { if (socketClient != null) { return; } var serverIsRunning = await IsServerRunningAsync(); if (!serverIsRunning) { return; } IsConnecting = true; socketClient = new DipSocketClient($"{Url}/serverhub", Environment.UserName); socketClient.On("OnConnected", message => { dispatcher.Invoke(() => { IsConnecting = false; IsConnected = true; OnNotification(); }); }); socketClient.On("OnNotification", async(message) => { await dispatcher.Invoke(async() => { await OnServerMonitorNotificationAsync(message); }); }); socketClient.Closed += async(sender, args) => { await dispatcher.Invoke(async() => { await DisposeSocketAsync(); }); }; socketClient.Error += async(sender, args) => { var ex = args as Exception; if (ex.InnerException is TaskCanceledException) { await DisposeSocketAsync(); } else { await dispatcher.Invoke(async() => { OnException(args.Message, new Exception(args.Message)); await DisposeSocketAsync(); }); } }; await socketClient.StartAsync(Name); } catch (WebSocketException) { await DisposeSocketAsync(); } catch (Exception ex) { await DisposeSocketAsync(); OnException(ex.Message, ex); } }
private async Task <bool> MonitorAsync(bool isForRun = false) { if (IsConnected) { NotificationsAdd(new Message { MessageType = MessageType.Info, Text = $"Already connected to strategy", Timestamp = DateTime.Now }); return(IsConnected); } if (!IsValidSelectServer()) { return(false); } await SetCommandVisibility(false, true, false); Notifications.Clear(); if (string.IsNullOrWhiteSpace(strategyAssemblyManager.Id)) { throw new Exception("StrategyAssemblyManager has not loaded the strategy assemblies."); } socketClient = new DipSocketClient($"{SelectedServer.Url}/notificationhub", strategyAssemblyManager.Id); socketClient.On("Connected", message => { ViewModelContext.UiDispatcher.Invoke(() => { NotificationsAdd(new Message { MessageType = MessageType.Info, Text = $"Connected - {message.ToString()}", Timestamp = DateTime.Now }); }); }); socketClient.On("Notification", async(message) => { await ViewModelContext.UiDispatcher.Invoke(async() => { await OnStrategyNotificationAsync(message); }); }); socketClient.On("Trade", (message) => { ViewModelContext.UiDispatcher.Invoke(async() => { await OnTradeNotificationAsync(message); }); }); socketClient.On("OrderBook", (message) => { ViewModelContext.UiDispatcher.Invoke(async() => { await OnOrderBookNotificationAsync(message); }); }); socketClient.On("AccountInfo", (message) => { ViewModelContext.UiDispatcher.Invoke(() => { OnAccountNotification(message); }); }); socketClient.On("Candlesticks", (message) => { ViewModelContext.UiDispatcher.Invoke(async() => { await OnCandlesticksNotificationAsync(message); }); }); socketClient.Closed += async(sender, args) => { await ViewModelContext.UiDispatcher.Invoke(async() => { NotificationsAdd(new Message { MessageType = MessageType.Error, Text = $"socketClient.Closed", TextVerbose = args.ToString(), Timestamp = DateTime.Now }); await DisposeSocketAsync(false); }); }; socketClient.Error += async(sender, args) => { var ex = args as Exception; if (ex.InnerException is TaskCanceledException) { return; } await ViewModelContext.UiDispatcher.Invoke(async() => { NotificationsAdd(new Message { MessageType = MessageType.Error, Text = $"{args.Message}", TextVerbose = args.ToString(), Timestamp = DateTime.Now }); await DisposeSocketAsync(false); }); }; try { await socketClient.StartAsync(strategy.Name); StrategyDisplayViewModel.IsActive = true; if (!isForRun) { await SetCommandVisibility(false, false, true); } return(true); } catch (Exception ex) { Logger.Log($"MonitorAsync {ex.ToString()}", Prism.Logging.Category.Exception, Prism.Logging.Priority.High); NotificationsAdd(new Message { MessageType = MessageType.Error, Text = $"Monitor - {ex.Message}", TextVerbose = ex.ToString(), Timestamp = DateTime.Now }); await SetCommandVisibility(false, false, false); await DisconnectSocketAsync(); return(false); } }