Click here to Skip to main content
15,884,720 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi!
I have problem with client side implementation of signalrHub.
This is my ChatHub hub
C#
public class ChatHub : Hub
    {

        public string GetConnectionId()
        {
            return Context.ConnectionId;
        }

        public override Task OnConnectedAsync()
        {


            // Groups.AddToGroupAsync(Context.ConnectionId, name);
           
            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
JavaScript
<pre> var connection = new signalR.HubConnectionBuilder()
    .withUrl("/chatt", {
        accessTokenFactory: () => "testing"
    })
    .build();


    connection.start().then(function () {
    connection.invoke('GetConnectionId')
    .then(function (connectionId) {
       //alert(connectionId);
       removeConnectionID(connectionId);
    })

    }).catch(err => console.error(err));

     connection.on("diss", function(connectionId) {
   
         removeConnectionID(connectionId);
        //console.log(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!!!
Posted
Updated 24-Jan-21 21:42pm

1 solution

Think about what you're asking.

When a client disconnects from your hub, and will no longer receive any messages from your server...

... you want to send a message from the server to that client telling them they've disconnected.

That obviously won't work. Instead, you need to use client-side code to tell the user when they've disconnected:
How to notify the user about disconnections | Understanding and Handling Connection Lifetime Events in SignalR | Microsoft Docs[^]
 
Share this answer
 
Comments
sasko1 25-Jan-21 9:15am    
Thanks for responding!!
When i use

$.connection.hub.disconnected(function () {
removeConnectionID();
})

I get error
Uncaught TypeError: Cannot read property 'hub' of undefined.
When i type $.connection. i don't get hub as property to select at all.

Saw somewhere that i should have "Signalr/Hubs" referenced but when installing signalr files to project from client side library i cant find that file to download.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900