You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Hi. My project structure is like this:
My app is: admin can create a game and this game will be scheduled to X date. Game has questions, and each question has his own answers. Now clients sends me gameid, and im sending them questions with answers of this game. I want to test sending questions +answers realtime to clients but i cant.
My ui's are .net 8 apps (admin panel is web api, gameserver is empty web project which only contain hubs).
When event happens, from event handler with help of signalr im sending datas sequantially with time interval to clients. Sources are below:
Event handler (infrastructure layer in screenshot):
public class TestEventHandler : IEventHandler<TestEvent>
{
private readonly IGameRepository _gameRepository;
private readonly IHubContext<GameHub> _hubContext;
public TestEventHandler(IHubContext<GameHub> hubContext, IGameRepository gameRepository)
{
this._hubContext = hubContext;
this._gameRepository = gameRepository;
}
public async Task HandleAsync(GameCreatedEvent )
{
// successfully printed:
Console.WriteLine($"TestEventHandler triggered for Game with Id: {@event.gameId}");
// i can get datas here, datas are available:
var questionsWithAnswers = await _gameRepository.GetQuestionsWithAnswersByGameId(@event.gameId);
if (questionsWithAnswers is null || questionsWithAnswers.Count == 0) return;
var group = _hubContext.Clients
.Group(@event.gameId.ToString());
await group.SendAsync("GameStarted", new { GameId = .gameId });
await _hubContext.Clients.All.SendAsync("ReceiveMessage", "This is a test message!");
foreach (var question in questionsWithAnswers)
{
// successfully printed:
Console.WriteLine("Datas are sent.");
await group.SendAsync
(
method: "ReceiveQuestion",
arg1: new
{
question.QuestionId,
question.QuestionText,
question.Answers,
question.AnswerTimeInSeconds
}
);
await Task.Delay(TimeSpan.FromSeconds(question.AnswerTimeInSeconds));
}
await group.SendAsync("GameEnded");
// successfully printed:
Console.WriteLine("TestEventHandler finished. Datas end.");
}
}
my hub is (GameServer layer in screenshot):
public class GameHub : Hub
{
public async Task JoinGameGroup(string gameId)
{
await Groups.AddToGroupAsync
(
connectionId: Context.ConnectionId,
groupName: gameId
);
Console.WriteLine($"Client {Context.ConnectionId} joined game {gameId}");
}
public async Task LeaveGameGroup(string gameId)
{
await Groups.RemoveFromGroupAsync(Context.ConnectionId, gameId);
}
}
{
builder.Services.AddHangfire((_, opts) =>
{
opts.UsePostgreSqlStorage(x => x.UseNpgsqlConnection(builder.Configuration.GetConnectionString("ConnectionString_Standart")));
});
builder.Services.AddHangfireServer();
builder.Services.AddSignalR(); // To can use this type: HubContext<T>
// game infrastructure
builder.Services.AddScoped<IGameEventScheduler, GameEventScheduler>();
builder.Services.AddScoped<IGameEventDispatcher, GameEventDispatcher>();
builder.Services.AddSingleton<IEventPublisher, InMemoryMessagePublisher>();
builder.Services.AddTransient<IEventHandler<GameCreatedEvent>, GameEventHandler>();
}
services registerations of signalr layer:
var builder = WebApplication.CreateBuilder(args);
{
builder.Services
.AddSignalR()
.AddHubOptions<GameHub>(options => { });
builder.Services
.AddCors(options => options
.AddPolicy("SignalrCorsSettings", builder => builder
.AllowAnyMethod()
.AllowAnyHeader()
.AllowCredentials()
.WithOrigins("http://localhost:8080")));}
/* front client url is : "http://localhost:8080/test_client.html", its simple/just one html file which contains html+js codes which i gived before */
var app = builder.Build();
{
app.UseCors("SignalrCorsSettings");
app.MapHub<GameHub>("/game-hub");
}
app.Run();
now my problem is i cant send datas from signalr to clients properly. In console i cant get nothing except "Connected!" message. But im sending "ReceiveQuestion" and other signals to front code.
Logs from console:
[2025-04-27T11:51:41.904Z] Debug: Selecting transport 'WebSockets'.
[2025-04-27T11:51:41.914Z] Information: WebSocket connected to ws://localhost:5001/game-hub?id=IsWVARqNM1GL-yIkRDagYg.
[2025-04-27T11:51:41.914Z] Debug: The HttpConnection connected successfully.
[2025-04-27T11:51:41.914Z] Debug: Sending handshake request.
[2025-04-27T11:51:41.914Z] Information: Using HubProtocol 'json'.
[2025-04-27T11:51:41.932Z] Debug: Server handshake complete.
[2025-04-27T11:51:41.932Z] Debug: HubConnection connected successfully.
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
Hi. My project structure is like this:

My app is: admin can create a game and this game will be scheduled to X date. Game has questions, and each question has his own answers. Now clients sends me gameid, and im sending them questions with answers of this game. I want to test sending questions +answers realtime to clients but i cant.
My ui's are .net 8 apps (admin panel is web api, gameserver is empty web project which only contain hubs).
When event happens, from event handler with help of signalr im sending datas sequantially with time interval to clients. Sources are below:
Event handler (infrastructure layer in screenshot):
my hub is (GameServer layer in screenshot):
frontend client is:
services registerations of infrastructure layer:
services registerations of signalr layer:
now my problem is i cant send datas from signalr to clients properly. In console i cant get nothing except "Connected!" message. But im sending "ReceiveQuestion" and other signals to front code.
Logs from console:
What im missing, can anyone help?
Beta Was this translation helpful? Give feedback.
All reactions