I have my server console app:
static void Main(string[] args)
{
string url = "http://localhost:8080";
using (WebApp.Start(url))
{
MyHub hub = new MyHub();
Console.WriteLine("Server running on {0}", url);
var key = Console.ReadLine();
while (key != "quit")
{
hub.Send("Server", key);
}
}
}
public class MyHub : Hub
{
public void Send(string name, string message)
{
var context = GlobalHost.ConnectionManager.GetHubContext<MyHub>();
context.Clients.All.addMessage(name, message);
}
}
public class Startup
{
public void Configuration(IAppBuilder app)
{
app.UseCors(CorsOptions.AllowAll);
app.MapSignalR();
}
}
And my .NET client app
static void Main(string[] args)
{
MainAsync().Wait();
Console.ReadLine();
}
static async Task MainAsync()
{
try
{
var hubConnection = new HubConnection("http://localhost:8080/");
//hubConnection.TraceLevel = TraceLevels.All;
//hubConnection.TraceWriter = Console.Out;
IHubProxy hubProxy = hubConnection.CreateHubProxy("MyHub");
hubProxy.On("addMessage", data =>
{
Console.WriteLine("Incoming data: {0} {1}", data.name, data.message);
});
ServicePointManager.DefaultConnectionLimit = 10;
await hubConnection.Start();
}
catch (Exception ex)
{
}
}
I do not get any error when running the client. However, nothing gets printed out on the console.
When I uncomment these 2 lines:
hubConnection.TraceLevel = TraceLevels.All;
hubConnection.TraceWriter = Console.Out;
I am able to see some trace outputs in the console
07:56:28.0121460 - 355ca933-de49-400b-b859-c9dde6361151 - WS: OnMessage({"C":"d-
69A14839-B,0|C,0|D,1|E,0","S":1,"M":[]})
07:56:28.0277722 - 355ca933-de49-400b-b859-c9dde6361151 - ChangeState(Connecting
, Connected)
07:56:33.4655493 - 355ca933-de49-400b-b859-c9dde6361151 - WS: OnMessage({"C":"d-
69A14839-B,1|C,0|D,1|E,0","M":[{"H":"MyHub","M":"addMessage","A":["Server","Hello World"]}]}
)
07:56:37.9657773 - 355ca933-de49-400b-b859-c9dde6361151 - WS: OnMessage({})
07:56:47.9975354 - 355ca933-de49-400b-b859-c9dde6361151 - WS: OnMessage({})
"Server" and "Hello World" are the messages that are being sent from the server, so I guess the client is receiving the messages, just that I'm probably printing them out to the console the wrong way
Can anyone help?
Additional Info: I am able to receive the messages fine on my MVC application.
Shouldn't you declare your hubProxy event handler as this?
hubProxy.On<string, string>("Send", (name, message) =>
{
Console.WriteLine("Incoming data: {0} {1}", name, message);
});