$(function(){// Proxy created on the flyvar 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
publicclassChat:Hub{publicvoidSend(string message){// Call the addMessage method on all clientsClients.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
For each client connect to hub will pass a unique connection id,you can get the this value of the hub context.You can also map a user to the connection id.
You can use the OnConnected,OnDisconnected ,and OnReconnected methods of the hub class to track the user connection status.
Please refer to link for more information about how to map signalR user to connections:
one different question. when we call client side code from signalr hub server side code then can we call client side in such a way that client side function will return value to server side function from where the client side function will be called?
Participant
1347 Points
2399 Posts
Want to send message to only connected client SignalR
Oct 06, 2014 02:53 AM|mou_inn|LINK
here i got bit code for signalr and included.
client code
server code
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
</div>All-Star
16806 Points
2777 Posts
Microsoft
Re: Want to send message to only connected client SignalR
Oct 06, 2014 10:08 PM|Kevin Shen - MSFT|LINK
Hi mou_inn,
For each client connect to hub will pass a unique connection id,you can get the this value of the hub context.You can also map a user to the connection id.
You can use the OnConnected,OnDisconnected ,and OnReconnected methods of the hub class to track the user connection status.
Please refer to link for more information about how to map signalR user to connections:
http://www.asp.net/signalr/overview/guide-to-the-api/mapping-users-to-connections
You can use a dictionary to store the user and the connectionId.
You can store the use and connectionid in the OnConnected Method like below:
For more details ,please refer to the link:
http://www.asp.net/signalr/overview/guide-to-the-api/mapping-users-to-connections#inmemory
Best Regards,
Kevin Shen.
Participant
1347 Points
2399 Posts
Re: Want to send message to only connected client SignalR
Oct 07, 2014 09:30 AM|mou_inn|LINK
Participant
1347 Points
2399 Posts
Re: Want to send message to only connected client SignalR
Oct 07, 2014 02:12 PM|mou_inn|LINK
one different question. when we call client side code from signalr hub server side code then can we call client side in such a way that client side function will return value to server side function from where the client side function will be called?
please discuss. thanks