I have an asp.net classic website. ive got SignalR basic functionality to work (where one client send messages to rest of the clients). but now i want to send Messages only to specific connectionsIDs.
my Hub :
** [HubName("chatHub")]
public class ChatHub : Hub
{
public static List<string> messages = new List<string>();
public void GetServiceState()
{
Clients.updateMessages(messages);
}
public void UpdateServiceState()
{
messages.Add(DateTime.Now.ToString("dd/MM/yyyy HH:mm:ss"));
Clients.updateMessages(messages);
}
}**
Asp code:
<script type="text/javascript">
$(function () {
// creates a proxy to the health check hub
var healthCheckHub = $.connection.chatHub;
console.log($.connection.hub)
// handles the callback sent from the server
healthCheckHub.updateMessages = function (data) {
$("li").remove();
$.each(data, function () {
$('#messages').append('<li>' + this + '</li>');
console.log($.connection);
});
};
$("#trigger").click(function () {
healthCheckHub.updateServiceState();
});
// Start the connection and request current state
$.connection.hub.start(function () {
healthCheckHub.getServiceState();
});
});
Problem is i dont really know how to send to one specific ConnectionID with hub, since Clients.updateMessages(messages); send messages to all of them. how do i solve this?
P.S: ive already looked at: Send server message to connected clients with Signalr/PersistentConnection
and http://riba-escapades.blogspot.dk/2012/05/signalr-send-messages-to-single-client.html
that didnt worked.
SignalR allows messages to be sent to a particular client connection, all connections associated with a specific user, as well as to named groups of connections. => await Clients. User(userId).
Each client connecting to a hub passes a unique connection id. You can retrieve this value in the Context. ConnectionId property of the hub context.
When user click on send button, the message to be posted to server side using signalR connection hub. Thus whenever you post any message after clicking the join group button, the message will appear to all the clients who has joined the group.
In the default mode, the app server creates five server connections with Azure SignalR Service. The app server uses the Azure SignalR Service SDK by default. In the following performance test results, server connections are increased to 15 (or more for broadcasting and sending a message to a big group).
Well, you can send a message to a single client from a Hub like so:
Clients.Client(someConnectionIdIWantToSendToSpecifically).doSomething();
The trick is you need to know the connection ID you want to send the message to. Even more specifically you probably want to know the logical identity of the thing you want to send the message too since that logical identity could have multiple connections or have dropped and reconnected under a completely different connection id. Mapping of connections to logical identities is something SignalR leaves up to the application itself.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With