0

I've a disconnect method in TestHub(which is inherited by Hub) class for signal R. I'm not able to call javascript method fnDeleteCustomer from OnDisconnected method, however same same js method gets called on Connect method. What I'm doing wrong?

 public override Task OnDisconnected() { try { var customer = ConnectedUsers.Find(x => x.ConnectionID == Context.ConnectionId); if (customer!=null) { Clients.Client(customer.ConnectionID).fnDeleteCustomer(customer.UserId); return base.OnDisconnected(); } } catch { }; return null; } 

3 Answers 3

3

According to MSDN:

Occurs when a connection disconnects from this hub instance.

So you have no any alive connection and you cannot access the client hub and its methods.

I suppose you should use client-side disconnected event:

$.connection.hub.disconnected(function() { $.connection.hub.fnDeleteCustomer(userId); }); 

More information about signalr lifetime events can be found here.

Sign up to request clarification or add additional context in comments.

Comments

0

You are not able to execute fnDeleteCustomer because at the moment of executing OnDisconnected, the client had already disconnected from the hub, so in that moment, the client wil not have a ConnectionId.

Sure, you can use the client disconnected method, but the SignalR disconnection mostly happens when the client leaves the respective page.

From my point of view, it is not the client who just left that you want to execute the fnDeleteCustomer method, but the remaining ones so they can be notified that somebody left.

Hope this helps! Best of luck!

EDIT:

If you want to notify all the other clients that someone left, you simply do so:

 public override OnDisconnected() { var customer = ConnectedUsers.Find(x => x.ConnectionID == Context.ConnectionId); Clients.All.notifySomeoneLeft(customer.Name); } 

Then, you create your client method notifySomeoneLeft:

$.connection.client.notifySomeoneLeft = function(customerName){ alert(customerName + "just left!"); }; 

And that's it. Everytime someone leaves, all the connected clients will get an alert.

Best of luck!

5 Comments

yes i want to notify other customers that a client left, is there any work around to do so?
Is it what you were looking for?
notifySomeoneLeft() will be executed on all other clients that are still on that page. The client that left will NOT be able to be notified because his connection ended when OnDisconnected was called!
Does this answer the question or do you still have some questions?
Ok let me give you some more idea about the problem, there is no other person this application and i wan't to fire an ajax call from client side to other application. so this can't be done with Clients.All.notifySomeoneLeft(customer.Name); . Hope you getting my point, as what solution you suggested, I already tried that and that's what i asked in my question.
0
public override OnDisconnected() { var customer = ConnectedUsers.Find(x => x.ConnectionID == Context.ConnectionId); Clients.All.notifySomeoneLeft(customer.Name); } 

The context.connectionId is getting new connectionId instead of old one.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.