Hi!
I have problem with client side implementation of signalrHub.
This is my ChatHub hub
public class ChatHub : Hub
{
public string GetConnectionId()
{
return Context.ConnectionId;
}
public override Task OnConnectedAsync()
{
return base.OnConnectedAsync();
}
public override async Task OnDisconnectedAsync(Exception exception)
{
await Clients.Client(GetConnectionId()).SendAsync("diss", Context.ConnectionId);
await base.OnDisconnectedAsync(exception);
}
}
and here is my js code
<pre> var connection = new signalR.HubConnectionBuilder()
.withUrl("/chatt", {
accessTokenFactory: () => "testing"
})
.build();
connection.start().then(function () {
connection.invoke('GetConnectionId')
.then(function (connectionId) {
removeConnectionID(connectionId);
})
}).catch(err => console.error(err));
connection.on("diss", function(connectionId) {
removeConnectionID(connectionId);
});
async function removeConnectionID(id) {
await $.get("/Chat/Test") ;
}
when i call removeConnectionID from connection.start()...
ajax call executes and takes me to Test action
but when i call removeConnectionID from connection.on("diss", ...
(OnDisconnectedAsync)
nothing happens and i dont get any errors
What I have tried:
tried sending console.log to all clients from connection.on("diss", ...
(OnDisconnectedAsync) and it worked fine
and tried to call same function from connection.start() and function works.
i think problem is that hub closes connection before action is done but dont know how to fix it!
Any help is appreciated!!!