I'm new to SignalR (the Core version, currently 1.0.0-alpha2-final). I have a view rendered from a controller action, and the page interacts with a few APIs without reloading. I would like to update parts of the page from other (API style) controller methods that do not return anything themselves. So the index view renders a page with controls, and also connects to SignalR.
What I have is
<div id="DisplayMessages"></div>
as a placeholder for messages.
My controller has a dependency injection of IHubContext.
let connection = new signalR.HubConnection("/signalr", { transport: signalR.TransportType.LongPolling });
connection.on('send',
data => {
var DisplayMessagesDiv = document.getElementById("DisplayMessages");
DisplayMessagesDiv.innerHTML += "<br/>" + data;
});
connection.onClosed = e => {
console.log('connection closed');
};
connection.start().then(() => connection.invoke('send', 'Hello'));
function SendMessage() {
var msg = document.getElementById("txtMessage").value;
connection.invoke('send', msg);
}
I can send messages to all clients from inside my controller method like
public JsonResult DoSomething([FromBody] DoSomethingParameters parameters)
doSomething();
_signalRHub.Clients.All.InvokeAsync("Send", "Did something with " + parameters.SomeParameter);
What I cannot figure out how to only send it to the client that initially rendered the page that now sends to the API. There is an example in the sample that maps to the User's identity, but that is useless since the page can be rendered by an anonymous user. How could I receive the connection ID of the client that is requesting the API whilst at the same time his browser is connected to SignalR?
Thanks for any info!