private void HandleCdpResponse(CdpResponse response) { if (response.IsMsgTargetPageCreated()) { _cdpTargetId = response.GetStrValue("params.targetInfo.targetId"); _logger.LogInformation($"Target Id : {_cdpTargetId}"); Send(CdpApi.GetOpenSessionMsg(_cdpTargetId)).Wait(); return; } if (response.IsMsgSessionCreated()) { _cdpSessionId = response.GetStrValue("result.sessionId"); _logger.LogInformation($"Session Id : {_cdpSessionId}"); foreach (var setting in CdpApi.Configuration) { Publish(setting, new { }); } Publish(CdpApi.Methods.TargetAutoAttach, CdpApi.TargetAutoAttachConfig); Publish(CdpApi.Methods.BrowserGetWinTarget, new { targetId = _cdpTargetId }, (rq, rs) => { rs.WithVal <int>("result.windowId", x => { _cdpWindowId = x; }); _logger.LogInformation($"Window Id : {_cdpWindowId}"); HandleActionResult(_routeService.Execute(NBlinkContext.DefaultRoute())); }); return; } if (string.IsNullOrEmpty(_cdpSessionId)) { return; } if (response.IsMsgFromTargetSession(_cdpSessionId)) { HandleTargetSessionMsg(response); return; } if (response.IsMsgTargetDestroyed(_cdpTargetId)) { _logger.LogError($"{_cdpTargetId} destroyed s******g down application..."); _dispatcher.Publish(new ExitCmd()); } }
public async Task StartAsync(CancellationToken cancellationToken) { await Task.Delay(1000); _dispatcher.Publish(new StartCmd()); }
protected void Publish(string payload) { _dispatcher.Publish(CdpApi.EvalMsg(payload)); }
protected void StartBrowserProcess() { var stdout = new StringBuilder(); _process = new Process { StartInfo = GetProcessInfo() }; _process.StartInfo.UseShellExecute = false; _process.StartInfo.RedirectStandardOutput = true; _process.StartInfo.RedirectStandardError = true; _process.EnableRaisingEvents = true; _process.OutputDataReceived += (sender, ed) => { Log(ed.Data); }; _process.Exited += (sender, ed) => { Log("Cdp closed"); _isExitHandled = true; }; _process.ErrorDataReceived += (sender, ed) => { if (!string.IsNullOrEmpty(ed.Data)) { MatchCollection results = dtregex.Matches(ed.Data); if (results.Count > 0) { var wsurl = results.First().Groups[1].Value; if (string.IsNullOrEmpty(wsurl)) { throw new Exception("Failed to get ws url"); } Dispatcher.Publish(new ConnectCmd(wsurl)); _cdpStart.Set(); } } stdout.Append(ed.Data); }; try { if (!_process.Start()) { throw new Exception("Failed to start the process"); } _process.BeginOutputReadLine(); _process.BeginErrorReadLine(); if (_cdpStart.WaitOne(new TimeSpan(0, 0, 10))) { Log("Found WS Url"); } else { Log("timed out"); throw new Exception("Timed out waiting for CDP"); } while (!_isExitHandled) { Task.Delay(100); } Dispatcher.Publish(new ExitCmd()); } finally { _process.CancelOutputRead(); _process.CancelErrorRead(); Terminate(); } }