public static async new Task <ContainerExecResult> Create(API API, ContainerExec exec, JToken response, string operationUrl) { var result = new ContainerExecResultWithWebSockets(exec); var webSocketStrings = exec.Interactive ? new[] { "0", "control" } : new[] { "0", "1", "2", "control" }; var sockets = new List <ClientWebSocket>(); foreach (var i in webSocketStrings) { string fdsSecret = response.SelectToken($"metadata.metadata.fds.{i}").Value <string>(); string wsUrl = $"{API.BaseUrlWebSocket}{operationUrl}/websocket?secret={fdsSecret}"; sockets.Add(await ClientWebSocketExtensions.CreateAndConnectAsync(wsUrl, API)); } result.WebSockets = sockets.ToArray(); return(result); }
public IEnumerable <ClientWebSocket> Exec(string[] command, Dictionary <string, string> environment = null, bool waitForWebSocket = true, bool interactive = true, int width = 80, int height = 25) { ContainerExec exec = new ContainerExec() { Command = command, Environment = environment, WaitForWebSocket = waitForWebSocket, Interactive = interactive, Width = width, Height = height, }; JToken response = API.Post($"/{Client.Version}/containers/{Name}/exec", exec); string operationUrl = response.Value <string>("operation"); if (waitForWebSocket == false) { API.WaitForOperationComplete(response); // wait-for-websocket is false. Nothing to return. return(null); } response = API.Get(operationUrl); int fdsN = interactive ? 1 : 3; List <Task <ClientWebSocket> > tasks = new List <Task <ClientWebSocket> >(); for (int i = 0; i < fdsN; i++) { string fdsSecret = response.SelectToken($"metadata.metadata.fds.{i}").Value <string>(); string wsUrl = $"{API.BaseUrlWebSocket}{operationUrl}/websocket?secret={fdsSecret}"; Task <ClientWebSocket> task = ClientWebSocketExtensions.CreateAndConnectAsync(wsUrl); tasks.Add(task); } Task.WaitAll(tasks.ToArray()); return(tasks.Select(t => t.Result)); }