2

here i got bit code for signalr and included.

client code

$(function () {
    // Proxy created on the fly
    var chat = $.connection.chat;

    // Declare a function on the chat hub so the server can invoke it
    chat.addMessage = function (message) {
        $('#messages').append('<li>' + message + '');
    };

    $("#broadcast").click(function () {
        // Call the chat method on the server
        chat.send($('#msg').val());
    });

    // Start the connection
    $.connection.hub.start();
});

server code

public class Chat : Hub
{
    public void Send(string message)
    {
        // Call the addMessage method on all clients
        Clients.addMessage(message);
    }
}

just see this line Clients.addMessage(message); addMessage() will invoke addMessage() function in client side but think suppose when i am sending data or message to specific client and if that is client is not connected any more then what will happen?

is there any provision in signalr like can we determine that client is connected before delivering data to specific client.

i want to detect a specific client is connected or not before sending data to client. if client is not connected then i will store data in db for that specific user. please help me with sample code that how to detect a specific client is connected or not? thanks

Thomas
  • 33,544
  • 126
  • 357
  • 626
  • HTTP is stateless so and I think SgnalR hold the list of client connected to an application. – शेखर Oct 06 '14 at 07:05
  • suppose a client first connect and after few minute close his browser windows. so i like to know the way to detect a specific user is connected or not before send some message to him. share your idea....is it possible. thanks – Thomas Oct 06 '14 at 07:07
  • Here is a link http://stackoverflow.com/questions/9334838/signalr-detect-connection-state-on-client – शेखर Oct 06 '14 at 07:15
  • the link is not the answer for me. i like to know how could i detect a specific user is connected or not from server side hub code. – Thomas Oct 06 '14 at 07:41

1 Answers1

1

I have a suggestion for this. Declare a static List , in Chat class. Use following code for OnConnect event.

public override Task OnConnected() {
    string connectionId = Context.ConnectionId;
    // Store this connectionId in list -- This will be helpful for tracking list of connected clients.
    return base.OnConnected();
}

And have OnDisconnect method.

public override Task OnDisconnected() {
        string connectionId = Context.ConnectionId;
        // Remove this connectionId from list
        // and save the message for disconnected clients.
        // Maintain list of disconnected clients in a list, say ABC
        return base.OnDisconnected();
}

On send method execute for connected clients only.

public void Send(string message){
        // Call the addMessage method on all clients
        Clients.AllExcept(ABC.ToArray()).addMessage(message);
}

You can refer to this link

Arindam Nayak
  • 7,346
  • 4
  • 32
  • 48
  • what is ABC.ToArray() ? – Thomas Oct 06 '14 at 08:45
  • after connecting if user close his browser or navigate to other site then OnDisconnected() function will be fired or not? if not then there could be problem. suppose user connect and close his browser or navigate to other site then when a message will be sent to a specific user who is not connect at that time then message will not be deliver to that user. how to handle this situation....advise. thanks – Thomas Oct 06 '14 at 08:47
  • When a user attempts to navigate away from a page with an active SignalR connection, the SignalR client will then make a best-effort attempt to notify the server that the client connection will be stopped. If the SignalR client's best-effort attempt fails to reach the server, the server will dispose of the connection after a configurable DisconnectTimeout later, at which time the OnDisconnected event will fire. Check this link - www.asp.net/signalr/overview/testing-and-debugging/troubleshooting – Arindam Nayak Oct 06 '14 at 09:10
  • @Thomas , ABC.ToArray() , is just a reference to previous code block comment section. – Arindam Nayak Oct 06 '14 at 09:11
  • can we show alert from signalr disconnect function when user close browser or navigate to any page. please discuss. thanks – Thomas Oct 06 '14 at 10:26
  • You can checkout this link - http://stackoverflow.com/questions/23560929/can-i-get-signalr-to-not-disconnect-itself-when-settings-window-location, it says that signal internally binds `unload` and then it send disconnect event, if your requirement is like this, you can override that and show alert . – Arindam Nayak Oct 06 '14 at 10:31
  • what is the meaning of this line Clients.AllExcept(ABC.ToArray()).addMessage(message); ? does it send msg to all client except the sender ABC ? – Thomas Oct 06 '14 at 11:50
  • suppose when connection break between client & server then OnDisconnected() function is called by signalr you said but from server side function OnDisconnected() how signalr detect the connection break between which client & server ? have u any idea? – Thomas Oct 06 '14 at 11:54
  • no , ABC is a list of disconnected clients, and that code block says send to all except those disconnected clients. – Arindam Nayak Oct 06 '14 at 11:59
  • As i had said, it attaches `$(window).bind("beforeunload", .. ` to detect close and redirect and then fires `OnDisconnected` . – Arindam Nayak Oct 06 '14 at 12:01
  • no probably u do not understand my qiestion. suppose client & server is connected and suddenly client pc lost the internet connection for long time. then how server side code detect which client has no connection with server. we could detect lost or disconnected client id then we can remove that user from online user list. tell me if u do not understand. thanks – Thomas Oct 06 '14 at 12:06
  • An unclean disconnect happens when the client can't send that "abort" request, or simply goes away due to network conditions (like you're explaining here). In that case, the server can only know the client is gone when the HTTP request last associated with that client is no longer there. This can take anywhere from 0 seconds (immediate) to many minutes, depending on the network layers between client and server. Once this happens, the server raises OnDisconnected once the DisconnectTimeout threshold has been reached (~30 seconds by default). -- https://github.com/SignalR/SignalR/issues/2081 – Arindam Nayak Oct 06 '14 at 12:42
  • thanks for answer but i like to know if the situation happen then how could i remove that user data from the list where i store all user id when user join. can u show me this with sample code. thanks – Thomas Oct 06 '14 at 12:52
  • You need to write code in `OnDisconnected` event, to remove user from list of connected users, you will get `Context.ConnectionId;` there , remove that based on `ConnectionId`. – Arindam Nayak Oct 06 '14 at 12:55
  • yes i know but if i do not know the userid which lost the internet connection then how could i remove that user from my list from server side code? – Thomas Oct 06 '14 at 12:57
  • Only if `OnDisconnected` event gets fired, either for connection loss, browser close, redirect or explicit stopping connection, then in that function you should get the `connectionid`. If i understand correctly, you need `userid`, for that you need to maintain one to one mapping for `userid` and `connectionid`. – Arindam Nayak Oct 06 '14 at 13:07
  • Can u give code for one to one mapping or redirect to right article. Thanks – Thomas Oct 06 '14 at 14:43
  • I can give you idea only, you can have code , that will create user on `OnConnected` event, and map userid to `ConnectionId` , from there you can have mapping and manage . Links - http://www.asp.net/signalr/overview/guide-to-the-api/mapping-users-to-connections http://www.tugberkugurlu.com/archive/mapping-asp-net-signalr-connections-to-real-application-users – Arindam Nayak Oct 06 '14 at 14:49
  • one last question that we know that we can call client side function from signalr server code but i like to know can we call client side function from signalr server code which return value to server side function ? if yes then discuss with sample code how to make it possible. thanks – Thomas Oct 06 '14 at 17:02
  • Yes, you can do that , search for `"How to define methods on the client that the server can call"` in this link - http://www.asp.net/signalr/overview/guide-to-the-api/hubs-api-guide-net-client – Arindam Nayak Oct 06 '14 at 18:07
  • i know how to call client side method from signalr server side code but i like to know how client side method can return data to server side code when we will call client side method from signalr server side code. is it possible? – Thomas Oct 06 '14 at 18:58