I am trying to send a message from console app to a MVC application using SignalR, following is the code:
static void Main(string[] args)
{
string url = "http://localhost:8080";
string line=null;
MyHub obj = new MyHub();
using (WebApp.Start(url))
{
Console.WriteLine("Server running on {0}", url);
Console.ReadLine();
Console.WriteLine("Enter your message:");
line = Console.ReadLine();
obj.Send(line);
}
}
}
class Startup
{
public void Configuration(IAppBuilder app)
{
app.UseCors(CorsOptions.AllowAll);
app.MapSignalR();
}
}
public class MyHub : Hub
{
public void Send(string message)
{
Clients.All.addMessage(message);
}
}
and here is how iam trying to get the message in the website :
<script type="text/javascript">
$(function () {
//Set the hubs URL for the connection
$.connection.hub.url = "http://localhost:8080/signalr";
// Declare a proxy to reference the hub.
var chat = $.connection.myHub;
// Create a function that the hub can call to broadcast messages.
chat.client.addMessage = function (message) {
// Html encode display name and message.
//var encodedName = $('<div />').text(name).html();
var encodedMsg = $('<div />').text(message).html();
// Add the message to the page.
$('#discussion').append('<li><strong>'
+ '</strong>: ' + encodedMsg + '</li>');
};
// Get the user name and store it to prepend to messages.
//$('#displayname').val(prompt('Enter your name:', ''));
// Set initial focus to message input box.
//$('#message').focus();
// Start the connection.
});
</script>
The thing is that iam getting the following exception:
It feels like i cant directly instantiate an object of the class Myhub, any idea on how to fix this, remember that i need to send the message from the console to the webpage.. Any recommendations???