I'm playing around with the new 1.0alpha2 release of signalR. I want to implement a SignalR server outside an ASP.NET application. So that two console applications can talk to each other.
With the old 0.5.3 version I was able to "Install-Package SignalR.Hosting.Self" to:
var server = new Server("http://127.0.0.1:8088/");
But in the new 1.0alpha2 Release I can't install this NuGet Package...
Can anyone give me a link or maybe a working mini example of two console applications which based on the 1.0alpha2 release. (I can only find not working old 0.5.3 examples...).
Ok. so I've followed your instructions. Now:
My Client Console:
class Programm
{
static void Main(string[] args)
{
var connection = new HubConnection("http://localhost/");
IHubProxy myHub = connection.CreateHubProxy("MyHub");
connection.Start().ContinueWith(task =>
{
if (task.IsFaulted)
Console.WriteLine("No Connection: " + task.Exception.GetBaseException());
else
Console.WriteLine("Connected!");
});
myHub.Invoke("Send");
Console.ReadLine(); // wait...
}
}
And here is my Server Console:
class Program : Hub
{
static void Main(string[] args)
{
Console.ReadKey();
}
public void Send(string message)
{
Debug.WriteLine("Server Method [send] was called");
Console.WriteLine("Server Method [send] was called");
}
}
But this is nonsense I think...
You need to use the new NuGet package since SignalR went official: (says ASP but its used in .NET applications too)
Install-Package Microsoft.AspNet.SignalR -pre Server
Install-Package -pre Microsoft.AspNet.SignalR.Client Client
There is also a prebuilt sample application here that you can then hook your console application into for some testing :
Install-Package Microsoft.AspNet.SignalR.Sample
Using the clients; connecting two console applications one would need to host the hub and other would need to use the client connection pointing to the host.
All of the info that you need is in this client wiki: link
EDIT
Server: (Using Self Host)
class Program
{
static void Main(string[] args)
{
string url = "http://localhost:8081/";
var server = new Server(url);
// Map the default hub url (/signalr)
server.MapHubs();
// Start the server
server.Start();
Console.WriteLine("Server running on {0}", url);
// Keep going until somebody hits 'x'
while (true)
{
ConsoleKeyInfo ki = Console.ReadKey(true);
if (ki.Key == ConsoleKey.X)
{
break;
}
}
}
public class MyHub : Hub
{
public void Send(string message)
{
Clients.All.addMessage(message);
}
}
}
Client:
class Program
{
static void Main(string[] args)
{
var connection = new HubConnection("http://localhost:8081/");
IHubProxy proxy = connection.CreateHubProxy("MyHub");
connection.Start().Wait();
proxy.On("addMessage", data => { Console.WriteLine("From Server: " + data); });
while (true)
{
proxy.Invoke("Send", Console.ReadLine());
}
}
}

PS. ill add the downloads to both solutions in the comments below. I'm sure you'll be fine going ahead.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With