I am trying to create a client signalr app. At present, I am able to get my MVC clients to send messages to and from the Hub, however, I’m facing some difficulties with my .NET client app
Server Hub code:
namespace ServerHub
{
public class ChatterBox : Hub
{
public void Send(string message)
{
Clients.All.addMessage(message);
}
}
}
Console App code (almost directly lifted from github)
using Microsoft.AspNet.SignalR.Client.Hubs;
using ServerHub;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Say("hello from console");
}
public static void Say(string message)
{
//var connection = new HubConnection("http://localhost/");
//IHubProxy myHub = connection.CreateHubProxy("ChatterBox");
var connection = new HubConnection("http://localhost/");
//Make proxy to hub based on hub name on server
var myHub = connection.CreateHubProxy("ChatterBox");
//Start connection
connection.Start().ContinueWith(task =>
{
if (task.IsFaulted)
{
Console.WriteLine("Failed to start: {0}", task.Exception.GetBaseException());
}
else
{
Console.WriteLine("Success! Connected with client connection id {0}", connection.ConnectionId);
// Do more stuff here
}
});
connection.Send("Hello").ContinueWith(task =>
{
if (task.IsFaulted)
{
Console.WriteLine("Send failed {0}", task.Exception.GetBaseException());
}
else
{
Console.WriteLine("Success");
}
});
}
}
}
I get the error message below, during connection.Send()
Start must be called before data can be sent.
Where did I go wrong?
EDIT:
2nd attempt:
var connection = new HubConnection("http://localhost/");
//Make proxy to hub based on hub name on server
var myHub = connection.CreateHubProxy("ChatterBox");
//Start connection
connection.Start().ContinueWith(task =>
{
if (task.IsFaulted)
{
Console.WriteLine("Failed to start: {0}", task.Exception.GetBaseException());
}
else
{
Console.WriteLine("Success! Connected with client connection id {0}",
connection.ConnectionId);
// Do more stuff here
connection.Send("Hello");
}
});
myHub.Invoke("Send", "lol");
Still gives me an error
EDIT: 3rd Attempt
var connection = new HubConnection("http://localhost:60610/");
//Make proxy to hub based on hub name on server
var myHub = connection.CreateHubProxy("ChatterBox");
//Start connection
connection.Start().ContinueWith(task =>
{
if (task.IsFaulted)
{
Console.WriteLine("Failed to start: {0}", task.Exception.GetBaseException());
}
else
{
//Console.WriteLine("Success! Connected with client connection id {0}", connection.ConnectionId);
myHub.Invoke("Send", "lol");
}
});
Seems to be working
I also needed to set the port number – e.g., if I’m browsing my MVC website like this: http://localhost:60610/, this is the address I need to set in my console app.
Can I say that during deployment, it won’t be an issue because it’ll be port 80 by default?
Read this http://msdn.microsoft.com/en-us/library/dd460717.aspx
TL;DR Start is async and that’s not how you write sequential asynchronous code:
If you can use .NET 4.5 it’ll make your life alot easier:
UPDATE: Let’s try this again. The code below will work but you should really read that link to learn how to write async code. Otherwise you’ll just run into these problems over and over.