I am trying to create a server client environment with SignalR. I read a lot of information on the internet and also a lot on Stack Overflow but I am totaly stuck :(
What I have tried:
Here is what I did:
I created a ASP.NET web application .NET framework (publised as ISS website port 50999, acting as my server). I added Microsoft.AspNet.SignalR, Microsoft.AspNet.SignalR.Core, Microsoft.AspNet.SignalR.JS and Microsoft.AspNet.SignalR.SystemWeb to my solution with the packet manager console.
I created a Owin startup class:
using Microsoft.Owin;
using Owin;
[assembly: OwinStartup(typeof(TestSignalR.Startup))]
namespace TestSignalR
{
public class Startup
{
public void Configuration(IAppBuilder app)
{
app.MapSignalR();
}
}
}
I created a hub, with a return message to all clients:
[HubName("mart")]
public class MartHub : Hub
{
public Task SendMessage(string user, string message)
{
using (StreamWriter file = new StreamWriter(@"C:\temp\logServer.txt", true))
{
file.WriteLine("Test");
}
return Clients.All.SendAsync("ReceiveMessage", user, message);
}
}
For my client I created a simple console app with my receive message handler
static void Main(string[] args)
{
var connection = new HubConnection("http://127.0.0.1:50999/");
var myHub = connection.CreateHubProxy("mart");
myHub.On<string, string>("ReceiveMessage", (user, message) =>
{
Console.WriteLine(user, message);
Console.Read();
});
connection.Start().ContinueWith(task => {
if (task.IsFaulted)
{
Console.WriteLine("There was an error opening the connection:{0}",
task.Exception.GetBaseException());
}
else
{
Console.WriteLine("Connected");
}
}).Wait();
myHub.Invoke<string>("SendMessage","mart","test").ContinueWith(task => {
if (task.IsFaulted)
{
Console.WriteLine("There was an error calling send: {0}",
task.Exception.GetBaseException());
}
else
{
Console.WriteLine(task.Result);
}
});
When I run my client, the Sendmessage method on my server runs great. The test line is placed into my txt file. But.. I do not receive my message back from the server.
I also tried it from a button on my default.aspx page on the server but that did not work either
protected void Button1_Click(object sender, EventArgs e)
{
var context = GlobalHost.ConnectionManager.GetHubContext<MartHub>();
context.Clients.All.Send("ReceiveMessage", "Mart", "Test");
}
Can somebody tell me what is going wrong here?