I have a working implementation of a Windows Service acting as a SignalR client sending a message to a method on a hub in a ASP.NET MVC (server). I just need to know how to use MVC to display my received string in a View or something.
I have the following client code in a Windows Service setting up the hubConnection and calling the "Hello" method:
protected override async void OnStart(string[] args)
{
eventLog1.WriteEntry("In OnStart");
try
{
var hubConnection = new HubConnection("http://localhost/AlphaFrontEndService/signalr", useDefaultUrl: false);
IHubProxy alphaProxy = hubConnection.CreateHubProxy("AlphaHub");
await hubConnection.Start();
// Invoke method on hub
await alphaProxy.Invoke("Hello", "Message from Service");
}
catch (Exception ex)
{
eventLog1.WriteEntry(ex.Message);
}
}
When I start the Service, it calls my MVC app with this hub and method:
public class AlphaHub : Hub
{
public void Hello(string message)
{
// We got the string from the Windows Service
// using SignalR. Now need to send to the clients
Clients.All.addNewMesssageToPage(message);
}
}
I set up a method on the HomeController:
public ActionResult Messaging()
{
return View();
}
Then the Messaging view:
@{
ViewBag.Title = "Messaging";
}
<h2>Messaging</h2>
<ul id="messages"></ul>
@section scripts
{
<script src="~/Scripts/jquery.signalR-2.0.2.min.js"></script>
<script src="~/signalr/hubs"></script>
<script>
$(function() {
var alpha = $.connection.alphaHub;
// Create a function that the hub can call back to display messages
alpha.client.addNewMessageToPage = function (message) {
// Add the message to the page.
$('<li />').text(message).appendTo('#messages');
};
$.connection.hub.start();
});
</script>
}
It's not updating the browser at /Home/Messaging