I am facing a weird issue here and I was hoping I could get some help. I followed the example on integrating signalR to my C# ASP.net code and this is what I have so far
Filename : main.cshtml
<input type="button" id="sendmessage" value="Send" />
@section scripts {
<script type="text/javascript" src="~/Scripts/jquery-3.3.1.js"></script>
<script src="~/Scripts/jquery.signalR-2.2.2.min.js"></script>
<script src="~/signalr/hubs"></script>
<script src="~/Scripts/external.js"></script>
}
Filename : external.js
$(function () {
var d = $.connection
var chat = $.connection.responseHub;
// Create a function that the hub can call back to display messages.
chat.client.addNewMessageToPage = function (name, message) {
console.log(name);
};
// Start the connection.
$.connection.hub.start().done(function () {
$('#sendmessage').click(function () {
// Call the Send method on the hub.
chat.server.send("Hello", "World");
});
});
});
This is my backend code
Filename : ResponseHub.cs
public class ResponseHub : Hub
{
public void Send(string response, string something)
{
// Call the addNewMessageToPage method to update clients.
this.Clients.All.addNewMessageToPage(response, "random");
}
.......
}
Now here is what is happening my front end has a button with id "sendmessage" when I press that button it hits my backend function called Send in the ResponseHub class which sends it back to my front page since the statement this.Clients.All.addNewMessageToPage
sends it to front end to the function bound to addNewMessageToPage
. This works great and only works if I press the send button.
Now if the back end method ResponseHub.Send is called through some other source in the backend then it fails to reach the front end function addNewMessageToPage
. My question is why does ResponseHub.Send only reach the front end when its called through the (Front End) Send button ? How can i fix this so the backend function can reach the front end function directly ? There is a thread in my backend that is contantly polling and that thread sometimes needs to send messages to the front end thus to do that its calling ResponseHub.Send to send messages to the front end. Any suggestions on how I can fix this ?