Code: Select all
//I set additional parameters to Logging configuration in application.json
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning",
"Microsoft.AspNetCore.SignalR": "Debug",//this
"Microsoft.AspNetCore.Http.Connections": "Debug"//and this one
}
}
< /code>
Protokolliert, wenn Client Endpoint Verbraucher aufruft:
dbug: Microsoft.AspNetCore.SignalR.Internal.DefaultHubDispatcher[1] Received hub invocation: InvocationMessage { InvocationId: "2", Target: "ConsumeCandidate", Arguments: [ lolkek, 581dc267-d2ff-403f-924c-cd6dba40535b ], StreamIds: [ ] }.
Server hat keine Probleme mit der Empfangsnachricht, aber Hub ruft keine erforderliche Methode auf.
Hub: < Br />
Code: Select all
public class LobbyHub(ILobbyService lobbyService, ILogger logger, IGameService gameService,
IElectionManager electionManager) : Hub
{
// this method should be invoked
public void ConsumeCandidate(string lobbyName, string userClientId)
{
logger.LogInformation($"Chancellor candidate received from: {userClientId}");
}
//For instance hub is able to invoke this one
public async Task StartGame(string lobbyName, string userClientId)
{
await gameService.StartRound(lobbyName, userClientId);
}
}
app.MapHub("/lobbyhub");
< /code>
Wie Sie sehen, kann es keine Namensprobleme sein. Beachten Sie auch, dass alle anderen Methoden dieser Hub gut funktionieren Verbraucherung
Code: Select all
builder.Services.AddSignalR().AddHubOptions(options =>
{
options.EnableDetailedErrors = true;
});
< /code>
Clientcode: < /p>
const invokeMethod = async (methodName, ...args) => {
console.log("Invoking method:", methodName, "\nwith args:", args);
console.log("Connection state:", connection.state);
return connection.invoke(methodName, ...args);
};
function setChoosingCandidatesButton(candidates) {
candidates.allCandidates.forEach((candidate) => {
const candidateButton = document.getElementById('s' + candidate.index);
candidateButton.textContent = "choose";
candidateButton.dataset.id = candidate.userId + " " + candidates.lobbyName;
candidateButton.onclick = (e) => {
let data = e.target.dataset.id.split(" ");
invokeMethod("ConsumeCandidate", data[1], data[0]);
};
});
}
Ich habe die zweite Hub hinzugefügt, um die Methode zu testen: < /p>
Code: Select all
public class GameHub(ILogger logger,
IElectionManager electionManager) : Hub
{
public async Task ConsumeCandidate(string lobbyName, string userClientId)
{
logger.LogInformation($"Chancellor candidate received from: {userClientId}");
}
}
< /code>
Protokolle: < /p>
dbug: Microsoft.AspNetCore.SignalR.Internal.DefaultHubDispatcher[1]
Received hub invocation: InvocationMessage { InvocationId: "0", Target: "ConsumeCandidate", Arguments: [ lolkek, 581dc267-d2ff-403f-924c-cd6dba40535b ], StreamIds: [ ] }.
info: SecretHitler.WebApp.Hubs.GameHub[0]
Chancellor candidate received from: 581dc267-d2ff-403f-924c-cd6dba40535b
Code: Select all
//This method from LobbyHub
public override Task OnDisconnectedAsync(Exception? exception)
{
logger.LogInformation($"Client disconnected {Context.ConnectionId}");
return base.OnDisconnectedAsync(exception);
}