-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathWebSocketClient.cs
More file actions
133 lines (124 loc) · 5.92 KB
/
WebSocketClient.cs
File metadata and controls
133 lines (124 loc) · 5.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
using System.Text;
using System.Diagnostics;
using UiPath.Ipc.WebSockets;
using Microsoft.Extensions.DependencyInjection;
namespace UiPath.Ipc.Tests;
class WebSocketClient
{
static async Task _Main(string[] args)
{
Console.WriteLine(typeof(int).Assembly);
Trace.Listeners.Add(new TextWriterTraceListener(Console.Out));
Thread.Sleep(1000);
var source = new CancellationTokenSource();
try
{
await await Task.WhenAny(RunTestsAsync(source.Token), Task.Run(() =>
{
Console.ReadLine();
Console.WriteLine("Cancelling...");
source.Cancel();
}));
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
Console.ReadLine();
}
}
private static async Task RunTestsAsync(CancellationToken cancellationToken)
{
Uri uri = new("ws://localhost:1212/wsDemo/");
var serviceProvider = ConfigureServices();
var callback = new ComputingCallback { Id = "custom made" };
var computingClientBuilder = new WebSocketClientBuilder<IComputingService, IComputingCallback>(uri, serviceProvider)
.CallbackInstance(callback)/*.EncryptAndSign("localhost")*/.RequestTimeout(TimeSpan.FromSeconds(2));
var stopwatch = Stopwatch.StartNew();
int count = 0;
try
{
var computingClient = computingClientBuilder.ValidateAndBuild();
var systemClient =
new WebSocketClientBuilder<ISystemService>(uri)
//.EncryptAndSign("localhost")
.RequestTimeout(TimeSpan.FromSeconds(2))
.Logger(serviceProvider)
.ValidateAndBuild();
var watch = Stopwatch.StartNew();
//using (var file = File.OpenRead(@"C:\Windows\DPINST.log"))
//{
// Console.WriteLine(await systemClient.Upload(file));
//}
for (int i =0; i<50;i++)
{
// test 1: call IPC service method with primitive types
float result1 = await computingClient.AddFloat(1.23f, 4.56f, cancellationToken);
count++;
Console.WriteLine($"[TEST 1] sum of 2 floating number is: {result1}");
// test 2: call IPC service method with complex types
ComplexNumber result2 = await computingClient.AddComplexNumber(
new ComplexNumber(0.1f, 0.3f),
new ComplexNumber(0.2f, 0.6f), cancellationToken);
Console.WriteLine($"[TEST 2] sum of 2 complexe number is: {result2.A}+{result2.B}i");
// test 3: call IPC service method with an array of complex types
ComplexNumber result3 = await computingClient.AddComplexNumbers(new[]
{
new ComplexNumber(0.5f, 0.4f),
new ComplexNumber(0.2f, 0.1f),
new ComplexNumber(0.3f, 0.5f),
}, cancellationToken);
Console.WriteLine($"[TEST 3] sum of 3 complexe number is: {result3.A}+{result3.B}i", cancellationToken);
// test 4: call IPC service method without parameter or return
//await systemClient.DoNothing(cancellationToken);
//Console.WriteLine($"[TEST 4] invoked DoNothing()");
//((IDisposable)systemClient).Dispose();
// test 5: call IPC service method with enum parameter
string text = await systemClient.ConvertText("hEllO woRd!", TextStyle.Upper, cancellationToken);
Console.WriteLine($"[TEST 5] {text}");
// test 6: call IPC service method returning GUID
Guid generatedId = await systemClient.GetGuid(Guid.NewGuid(), cancellationToken);
Console.WriteLine($"[TEST 6] generated ID is: {generatedId}");
// test 7: call IPC service method with byte array
byte[] input = Encoding.UTF8.GetBytes(string.Concat(Enumerable.Range(1, 1).Select(_ => "Test")));
byte[] reversed = await systemClient.ReverseBytes(input, cancellationToken);
Console.WriteLine($"[TEST 7] reverse bytes");
// test 8: call IPC service method with callback
var userName = await computingClient.SendMessage(new SystemMessage { Text = "client text" }, cancellationToken);
Console.WriteLine($"[TEST 8] client identity : {userName}");
//// test 9: call IPC service method with message parameter
////Console.WriteLine($"[TEST 9] callback error");
//try
//{
// //userName = await systemClient.SendMessage(new SystemMessage { Text = "client text" }, cancellationToken);
//}
//catch(Exception ex)
//{
// //Console.WriteLineex.Message);
//}
}
watch.Stop();
Console.WriteLine(watch.ElapsedMilliseconds);
var callbackProxy = (IDisposable)computingClient;
callbackProxy.Dispose();
callbackProxy.Dispose();
callbackProxy.Dispose();
//((IpcProxy)callbackProxy).CloseConnection();
((IpcProxy)computingClient).CloseConnection();
((IpcProxy)systemClient).CloseConnection();
}
finally
{
stopwatch.Stop();
Console.WriteLine();
Console.WriteLine("Calls per second: " + count / stopwatch.Elapsed.TotalSeconds);
Console.WriteLine();
}
// test 10: call slow IPC service method
//await systemClient.SlowOperation(cancellationToken);
//Console.WriteLine($"[TEST 10] Called slow operation");
}
private static IServiceProvider ConfigureServices() =>
new ServiceCollection()
.AddIpcWithLogging()
.BuildServiceProvider();
}