-
Notifications
You must be signed in to change notification settings - Fork 0
/
BlazorChatSampleHub.cs
163 lines (146 loc) · 6.26 KB
/
BlazorChatSampleHub.cs
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
using System;
using System.Text;
using System.Threading.Tasks;
using Microsoft.AspNetCore.SignalR;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
namespace BlazorChat
{
public class BlazorChatSampleHub : Hub
{
public const string HubUrl = "/chat";
static List<User> ConnectedUsers = new List<User>();
static int TotalTranslations;
Dictionary<string, string> Translations = new Dictionary<string, string>();
public async Task Broadcast(string message)
{
if (message.StartsWith("[Notice]"))
{
await Translate("en", message);
}
else
{
await Translate(CurrentUser.Language, message);
}
foreach (KeyValuePair<string, string> translation in Translations)
{
await Clients.Group(translation.Key).SendAsync("Broadcast", $"{CurrentUser.Name} ({CurrentUser.Language})", translation.Value);
}
}
public async Task UserConnected(List<User> user)
{
var id = Context.ConnectionId;
ConnectedUsers.Add(new User() {Name = user[0].Name, ConnectionId = id, Language = user[0].Language});
await Groups.AddToGroupAsync(id, user[0].Language);
await Clients.All.SendAsync("UserConnected", ConnectedUsers);
Console.WriteLine($"Adding... id: {id}, user: {user[0].Name}, language: {user[0].Language}");
}
public override Task OnConnectedAsync()
{
Console.WriteLine($"{Context.ConnectionId} connected");
return base.OnConnectedAsync();
}
public override async Task OnDisconnectedAsync(Exception e)
{
Console.WriteLine($"Disconnected {e?.Message} {Context.ConnectionId}");
// Remove user on disconnect
var user = ConnectedUsers.FirstOrDefault(x => x.ConnectionId == Context.ConnectionId);
if (user != null)
{
ConnectedUsers.Remove(user);
await Groups.RemoveFromGroupAsync(user.ConnectionId, user.Language);
Console.WriteLine($"Removing... id: {user.ConnectionId}, user: {user.Name}, language: {user.Language}");
await Clients.All.SendAsync("UserConnected", ConnectedUsers);
await base.OnDisconnectedAsync(e);
}
}
private User CurrentUser
{
get
{
return ConnectedUsers.FirstOrDefault(i => i.ConnectionId == Context.ConnectionId);
}
}
private async Task Translate(string sourceLanguage, string message)
{
TotalTranslations += 1;
Console.WriteLine("Translation Counter: " + TotalTranslations);
// Setup and retrieve basic job settings
IConfiguration config = new ConfigurationBuilder()
.AddJsonFile("config/appsettings.json")
.AddEnvironmentVariables()
.Build();
AzureSpeechSettings azureSpeechSettings = config.GetRequiredSection("AzureSpeech").Get<AzureSpeechSettings>();
List<string> languages = new List<string> {"zh-Hant", "en", "de", "ru", "es"};
int i = languages.IndexOf(sourceLanguage);
if (TotalTranslations < 2000)
{
languages.RemoveAt(i);
}
StringBuilder route = new StringBuilder("/translate?api-version=3.0&from=" + sourceLanguage, 100);
route.Append("&to=" + sourceLanguage);
foreach (var lan in languages)
{
route.Append("&to=" + lan);
}
Console.WriteLine(route.ToString());
if (TotalTranslations < 2000)
{
object[] body = new object[] { new { Text = message } };
var requestBody = JsonConvert.SerializeObject(body);
using (var client = new HttpClient())
using (var request = new HttpRequestMessage())
{
// Build the request.
request.Method = HttpMethod.Post;
request.RequestUri = new Uri(azureSpeechSettings.Endpoint + route);
request.Content = new StringContent(requestBody, Encoding.UTF8, "application/json");
request.Headers.Add("Ocp-Apim-Subscription-Key", azureSpeechSettings.SubscriptionKey);
request.Headers.Add("Ocp-Apim-Subscription-Region", azureSpeechSettings.Location);
// Send the request and get response.
HttpResponseMessage response = await client.SendAsync(request).ConfigureAwait(false);
// Read response as a string.
string jsonResults = await response.Content.ReadAsStringAsync();
jsonResults = jsonResults.TrimStart(new char[] { '[' }).TrimEnd(new char[] { ']' });
var transList = JObject.Parse(jsonResults).SelectToken("translations").ToObject<List<Translation>>();
Translations.Clear();
foreach (var tr in transList)
{
Translations.Add(tr.To, tr.Text);
}
Console.WriteLine(jsonResults);
}
}
else
{
Translations.Clear();
foreach (var tr in languages)
{
Translations.Add(tr, message);
}
Console.WriteLine("Translation limit exceeded");
}
}
}
public class User
{
public required string Name {get; set;}
public required string Language {get; set;}
public required string ConnectionId {get; set;}
}
[JsonObject]
public class Translation
{
[JsonProperty("text")]
public required string Text {get; set;}
[JsonProperty("to")]
public required string To {get; set;}
}
// Collections of job-specific settings
public class AzureSpeechSettings
{
public required string SubscriptionKey { get; set; }
public required string Endpoint { get; set; }
public required string Location { get; set; }
}
}