11

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.

Null Reference
  • 10,480
  • 37
  • 100
  • 168

1 Answers1

12

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);
});
David Brabant
  • 36,511
  • 13
  • 77
  • 101
  • Thanks. Is there a limit to how many string parameters I can have? – Null Reference Mar 03 '14 at 08:17
  • 3
    You can always use something like: hubProxy.On("Send",(message) => Console.WriteLine("Incoming data: {0} {1}", message.Member1, message.Member2); }); – David Brabant Mar 03 '14 at 08:21
  • @DavidBrabant I thought that `"addMessage"` was the correct first parameter? You would `.Invoke` `"Send"` right? – Klors Sep 18 '14 at 17:58
  • @klors The method name on the server MyHub is Send. The client invokes Send, which in turn broadcasts "addMessage" to all clients. If Send and addMessage were the same function, you'd have an infinite recursion. – dthorpe Dec 07 '14 at 18:06
  • @dthorpe thanks for the response - unfortunately in the intervening time I've completely forgotten everything I thought I knew about SignalR, so I'll have to take your word for it until I'm forced to revisit the subject. Cheers – Klors Dec 08 '14 at 16:09
  • Please tell me where should I use this method in Main or In Hub Class? –  Dec 06 '16 at 10:32