3

我正在玩signalR 的新1.0alpha2 版本。我想在 ASP.NET 应用程序之外实现 SignalR 服务器。这样两个控制台应用程序就可以相互通信。

使用旧的 0.5.3 版本,我能够“Install-Package SignalR.Hosting.Self”到:

var server = new Server("http://127.0.0.1:8088/");

但是在新的 1.0alpha2 版本中,我无法安装这个 NuGet 包......

谁能给我一个链接或两个基于1.0alpha2版本的控制台应用程序的工作迷你示例。(我只能找到不工作的旧 0.5.3 示例......)。


行。所以我听从了你的指示。现在:

我的客户端控制台:

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...
  }
}

这是我的服务器控制台:

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");
  }
}

但我认为这是胡说八道......

4

1 回答 1

4

自从 SignalR 正式发布以来,您需要使用新的 NuGet 包:(说是 ASP,但它也用于 .NET 应用程序)

Install-Package Microsoft.AspNet.SignalR -pre服务器

Install-Package -pre Microsoft.AspNet.SignalR.Client客户

这里还有一个预构建的示例应用程序,然后您可以将控制台应用程序连接到其中进行一些测试:

Install-Package Microsoft.AspNet.SignalR.Sample

使用客户端;连接两个控制台应用程序,一个需要托管集线器,另一个需要使用指向主机的客户端连接。

您需要的所有信息都在此客户端 wiki 中:链接


编辑

服务器:(使用自主机)

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);

        }
    }

}

客户:

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。生病在下面的评论中将下载添加到这两个解决方案中。我相信你会没事的。

于 2012-11-22T13:52:51.953 回答