|
| 1 | +// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. |
| 2 | +// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. |
| 3 | + |
| 4 | +using System; |
| 5 | +using System.Net.WebSockets; |
| 6 | +using System.Text; |
| 7 | +using System.Threading; |
| 8 | +using System.Threading.Tasks; |
| 9 | +using Microsoft.AspNet.WebSockets.Client; |
| 10 | + |
| 11 | +namespace AutobahnTestClient |
| 12 | +{ |
| 13 | + public class Program |
| 14 | + { |
| 15 | + public async Task Main(string[] args) |
| 16 | + { |
| 17 | + try |
| 18 | + { |
| 19 | + string serverAddress = "ws://localhost:9001"; |
| 20 | + string agent = |
| 21 | + "ManagedWebSockets"; |
| 22 | + // "NativeWebSockets"; |
| 23 | + |
| 24 | + Console.WriteLine("Getting case count."); |
| 25 | + var caseCount = await GetCaseCountAsync(serverAddress, agent); |
| 26 | + Console.WriteLine(caseCount + " case(s)."); |
| 27 | + |
| 28 | + for (int i = 1; i <= caseCount; i++) |
| 29 | + { |
| 30 | + await RunCaseAsync(serverAddress, i, agent); |
| 31 | + } |
| 32 | + |
| 33 | + await UpdateReportsAsync(serverAddress, agent); |
| 34 | + } |
| 35 | + catch (Exception ex) |
| 36 | + { |
| 37 | + Console.WriteLine(ex); |
| 38 | + } |
| 39 | + |
| 40 | + Console.WriteLine("Done"); |
| 41 | + Console.ReadLine(); |
| 42 | + } |
| 43 | + |
| 44 | + private async Task<WebSocket> ConnectAsync(string address, string agent) |
| 45 | + { |
| 46 | + if (string.Equals(agent, "NativeWebSockets")) |
| 47 | + { |
| 48 | + var client = new ClientWebSocket(); |
| 49 | + await client.ConnectAsync(new Uri(address), CancellationToken.None); |
| 50 | + return client; |
| 51 | + } |
| 52 | + else |
| 53 | + { |
| 54 | + // TODO: BUG: Require ws or wss schemes |
| 55 | + var client = new WebSocketClient(); |
| 56 | + return await client.ConnectAsync(new Uri(address), CancellationToken.None); |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + private async Task<int> GetCaseCountAsync(string serverAddress, string agent) |
| 61 | + { |
| 62 | + var webSocket = await ConnectAsync(serverAddress + "/getCaseCount", agent); |
| 63 | + byte[] buffer = new byte[1024 * 4]; |
| 64 | + var result = await webSocket.ReceiveAsync(new ArraySegment<byte>(buffer), CancellationToken.None); |
| 65 | + await webSocket.CloseAsync(WebSocketCloseStatus.NormalClosure, string.Empty, CancellationToken.None); |
| 66 | + var caseCountText = Encoding.UTF8.GetString(buffer, 0, result.Count); |
| 67 | + return int.Parse(caseCountText); |
| 68 | + } |
| 69 | + |
| 70 | + private async Task RunCaseAsync(string serverAddress, int caseId, string agent) |
| 71 | + { |
| 72 | + try |
| 73 | + { |
| 74 | + Console.WriteLine("Running case " + caseId); |
| 75 | + var webSocket = await ConnectAsync(serverAddress + "/runCase?case=" + caseId + "&agent=" + agent, agent); |
| 76 | + await Echo(webSocket); |
| 77 | + } |
| 78 | + catch (Exception ex) |
| 79 | + { |
| 80 | + Console.WriteLine(ex); |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + private async Task Echo(WebSocket webSocket) |
| 85 | + { |
| 86 | + byte[] buffer = new byte[1024 * 4]; |
| 87 | + var result = await webSocket.ReceiveAsync(new ArraySegment<byte>(buffer), CancellationToken.None); |
| 88 | + while (!result.CloseStatus.HasValue) |
| 89 | + { |
| 90 | + await webSocket.SendAsync(new ArraySegment<byte>(buffer, 0, result.Count), result.MessageType, result.EndOfMessage, CancellationToken.None); |
| 91 | + result = await webSocket.ReceiveAsync(new ArraySegment<byte>(buffer), CancellationToken.None); |
| 92 | + } |
| 93 | + await webSocket.CloseAsync(result.CloseStatus.Value, result.CloseStatusDescription, CancellationToken.None); |
| 94 | + } |
| 95 | + |
| 96 | + private async Task UpdateReportsAsync(string serverAddress, string agent) |
| 97 | + { |
| 98 | + var webSocket = await ConnectAsync(serverAddress + "/updateReports?agent=" + agent, agent); |
| 99 | + await webSocket.CloseAsync(WebSocketCloseStatus.NormalClosure, string.Empty, CancellationToken.None); |
| 100 | + } |
| 101 | + } |
| 102 | +} |
0 commit comments