public async Task LaunchAndServe(ProcessStartInfo psi, HttpContext context, Func <string, Task <string> > extract_conn_url) { if (!context.WebSockets.IsWebSocketRequest) { context.Response.StatusCode = 400; return; } var tcs = new TaskCompletionSource <string> (); var proc = Process.Start(psi); try { proc.ErrorDataReceived += (sender, e) => { var str = e.Data; Console.WriteLine($"stderr: {str}"); if (str.Contains("listening on", StringComparison.Ordinal)) { var res = str.Substring(str.IndexOf("ws://", StringComparison.Ordinal)); if (res != null) { tcs.TrySetResult(res); } } }; proc.OutputDataReceived += (sender, e) => { Console.WriteLine($"stdout: {e.Data}"); }; proc.BeginErrorReadLine(); proc.BeginOutputReadLine(); if (await Task.WhenAny(tcs.Task, Task.Delay(2000)) != tcs.Task) { Console.WriteLine("Didnt get the con string after 2s."); throw new Exception("node.js timedout"); } var line = await tcs.Task; var con_str = extract_conn_url != null ? await extract_conn_url(line) : line; Console.WriteLine($"lauching proxy for {con_str}"); var proxy = new MonoProxy(); var browserUri = new Uri(con_str); var ideSocket = await context.WebSockets.AcceptWebSocketAsync(); await proxy.Run(browserUri, ideSocket); Console.WriteLine("Proxy done"); } catch (Exception e) { Console.WriteLine("got exception {0}", e); } finally { proc.CancelErrorRead(); proc.CancelOutputRead(); proc.Kill(); proc.WaitForExit(); proc.Close(); } }
public Task Run(Uri browserUri, WebSocket ideSocket) { return(proxy.Run(browserUri, ideSocket)); }