blob: d8cfbe5dbb33db26901052ba4e920dca1ee24dd4 (
plain)
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
|
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
namespace Microsoft.AspNetCore.SignalR.Crankier
{
public class AgentSender : IAgent
{
private readonly SemaphoreSlim _lock = new SemaphoreSlim(1, 1);
private readonly StreamWriter _outputStreamWriter;
public AgentSender(StreamWriter outputStreamWriter)
{
_outputStreamWriter = outputStreamWriter;
}
public async Task PongAsync(int id, int value)
{
var parameters = new
{
Id = id,
Value = value
};
await SendAsync("pong", JToken.FromObject(parameters));
}
public async Task LogAsync(int id, string text)
{
var parameters = new
{
Id = id,
Text = text
};
await SendAsync("log", JToken.FromObject(parameters));
}
public async Task StatusAsync(
int id,
StatusInformation statusInformation)
{
var parameters = new
{
Id = id,
StatusInformation = statusInformation
};
await SendAsync("status", JToken.FromObject(parameters));
}
private async Task SendAsync(string method, JToken parameters)
{
await _lock.WaitAsync();
try
{
await _outputStreamWriter.WriteLineAsync(
JsonConvert.SerializeObject(new Message
{
Command = method,
Value = parameters
}));
await _outputStreamWriter.FlushAsync();
}
finally
{
_lock.Release();
}
}
}
}
|