+
+
\ No newline at end of file
diff --git a/Api/Program.cs b/Api/Program.cs
index 443aa8b..f2cf074 100644
--- a/Api/Program.cs
+++ b/Api/Program.cs
@@ -1,4 +1,5 @@
using Api.SignalR;
+using Microsoft.AspNetCore.SignalR;
var builder = WebApplication.CreateBuilder(args);
@@ -8,6 +9,7 @@ builder.Services.AddEndpointsApiExplorer();
builder.Services.AddOpenApi();
builder.Services.AddSignalR();
builder.Services.AddHostedService();
+builder.Services.AddSingleton();
builder.Services.AddOpenApiDocument(config =>
{
config.Title = "NSwag Demo API";
diff --git a/Api/SignalR/SignalRSendService.cs b/Api/SignalR/SignalRSendService.cs
index ef63451..351ab0c 100644
--- a/Api/SignalR/SignalRSendService.cs
+++ b/Api/SignalR/SignalRSendService.cs
@@ -12,16 +12,16 @@ namespace Api.SignalR
while (!stoppingToken.IsCancellationRequested)
{
// For example, wait 10 seconds between messages.
- await Task.Delay(TimeSpan.FromSeconds(10), stoppingToken);
+ await Task.Delay(TimeSpan.FromSeconds(2), stoppingToken);
// Log or do any work here.
logger.LogInformation("Background service sending message at: {Time}", DateTime.Now);
// Send a message to all connected clients.
- await hubContext.Clients.All.SendAsync(
+ await hubContext.Clients.User("user1234").SendAsync(
"ReceiveMessage", // This is the client method name.
"Background Service", // Example sender.
- "Hello from the background task!", // The message.
+ $"Hello from the background task at {DateTime.Now:F}", // The message.
stoppingToken);
}
}
diff --git a/Api/SignalR/UserProvider.cs b/Api/SignalR/UserProvider.cs
new file mode 100644
index 0000000..f8a19b9
--- /dev/null
+++ b/Api/SignalR/UserProvider.cs
@@ -0,0 +1,14 @@
+using Microsoft.AspNetCore.SignalR;
+
+namespace Api.SignalR
+{
+ public class UserProvider : IUserIdProvider
+ {
+ public string GetUserId(HubConnectionContext connection)
+ {
+ // Extract the "userId" query parameter from the HTTP context.
+ var httpContext = connection.GetHttpContext();
+ return httpContext?.Request.Query["userId"].FirstOrDefault();
+ }
+ }
+}
\ No newline at end of file
diff --git a/react-client/.idea/.gitignore b/react-client/.idea/.gitignore
new file mode 100644
index 0000000..7e5b7d7
--- /dev/null
+++ b/react-client/.idea/.gitignore
@@ -0,0 +1,8 @@
+# Default ignored files
+/shelf/
+/workspace.xml
+# Editor-based HTTP Client requests
+/httpRequests/
+# Datasource local storage ignored files
+/dataSources/
+/dataSources.local.xml
diff --git a/react-client/.idea/encodings.xml b/react-client/.idea/encodings.xml
new file mode 100644
index 0000000..df87cf9
--- /dev/null
+++ b/react-client/.idea/encodings.xml
@@ -0,0 +1,4 @@
+
+
+
+
\ No newline at end of file
diff --git a/react-client/.idea/git_toolbox_blame.xml b/react-client/.idea/git_toolbox_blame.xml
new file mode 100644
index 0000000..7dc1249
--- /dev/null
+++ b/react-client/.idea/git_toolbox_blame.xml
@@ -0,0 +1,6 @@
+
+
+
+
+
+
\ No newline at end of file
diff --git a/react-client/.idea/inspectionProfiles/Project_Default.xml b/react-client/.idea/inspectionProfiles/Project_Default.xml
new file mode 100644
index 0000000..03d9549
--- /dev/null
+++ b/react-client/.idea/inspectionProfiles/Project_Default.xml
@@ -0,0 +1,6 @@
+
+
+
+
+
+
\ No newline at end of file
diff --git a/react-client/.idea/modules.xml b/react-client/.idea/modules.xml
new file mode 100644
index 0000000..a2b5918
--- /dev/null
+++ b/react-client/.idea/modules.xml
@@ -0,0 +1,8 @@
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/react-client/.idea/react-client.iml b/react-client/.idea/react-client.iml
new file mode 100644
index 0000000..24643cc
--- /dev/null
+++ b/react-client/.idea/react-client.iml
@@ -0,0 +1,12 @@
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/react-client/.idea/runConfigurations/start.xml b/react-client/.idea/runConfigurations/start.xml
new file mode 100644
index 0000000..6e6cb50
--- /dev/null
+++ b/react-client/.idea/runConfigurations/start.xml
@@ -0,0 +1,15 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/react-client/.idea/vcs.xml b/react-client/.idea/vcs.xml
new file mode 100644
index 0000000..6c0b863
--- /dev/null
+++ b/react-client/.idea/vcs.xml
@@ -0,0 +1,6 @@
+
+
+
+
+
+
\ No newline at end of file
diff --git a/react-client/src/App.tsx b/react-client/src/App.tsx
index c8467a5..65b8e5f 100644
--- a/react-client/src/App.tsx
+++ b/react-client/src/App.tsx
@@ -8,10 +8,11 @@ function App() {
const [forecast, setForecast] = useState([]);
const [connection, setConnection] = useState(null);
+ const [message, setMessage] = useState('');
useEffect(() => {
const newConnection = new HubConnectionBuilder()
- .withUrl('http://localhost:5175/WeatherUpdateHub') // adjust the URL/port as needed
+ .withUrl('http://localhost:5175/WeatherUpdateHub?userId=user1234')
.withAutomaticReconnect()
.build();
@@ -25,6 +26,7 @@ function App() {
.then(() => {
console.log('Connected to SignalR hub!');
connection.on('ReceiveMessage', (user, message) => {
+ setMessage(message);
console.log('Received message:', user, message);
});
})
@@ -54,7 +56,7 @@ function App() {