2012-06-21 78 views
43

有沒有使用signalR將消息發送到.net集線器的控制檯或winform應用程序的小例子?我已經嘗試了.net的例子,並且看過了wiki,但是它對hub(.net)和客戶端(控制檯應用程序)之間的關係沒有意義(找不到這個例子)。該應用程序是否只需要集線器的地址和名稱來連接?SignalR控制檯應用程序示例

如果有人可以提供一小段代碼,顯示應用程序連接到集線器併發送「Hello World」或.net集線器收到的東西?。

PS。我有一個標準的集線器聊天示例,如果我嘗試在Cs中指定一個集線器名稱,它會停止工作,即[HubName(「test」)],你知道這個的原因嗎?

謝謝。

當前控制檯應用代碼。

static void Main(string[] args) 
{ 
    //Set connection 
    var connection = new HubConnection("http://localhost:41627/"); 
    //Make proxy to hub based on hub name on server 
    var myHub = connection.CreateProxy("chat"); 
    //Start connection 
    connection.Start().ContinueWith(task => 
    { 
     if (task.IsFaulted) 
     { 
      Console.WriteLine("There was an error opening the connection:{0}", task.Exception.GetBaseException()); 
     } 
     else 
     { 
      Console.WriteLine("Connected"); 
     } 
    }).Wait(); 

    //connection.StateChanged += connection_StateChanged; 

    myHub.Invoke("Send", "HELLO World ").ContinueWith(task => { 
     if(task.IsFaulted) 
     { 
      Console.WriteLine("There was an error calling send: {0}",task.Exception.GetBaseException()); 
     } 
     else 
     { 
      Console.WriteLine("Send Complete."); 
     } 
    }); 
} 

Hub服務器。 (不同項目工作區)

public class Chat : Hub 
{ 
    public void Send(string message) 
    { 
     // Call the addMessage method on all clients 
     Clients.addMessage(message); 
    } 
} 

信息wiki上的是http://www.asp.net/signalr/overview/signalr-20/hubs-api/hubs-api-guide-net-client

+0

O.K其實這實際工作只是認爲我得到了相同的結果只是增加了一些停車點和控制檯。的ReadLine();最後。吶喊! – user685590

回答

67

首先你應該用的NuGet上您的客戶端應用程序的服務器應用程序和SignalR.Client安裝SignalR.Host.Self:

PM>安裝,包裝SignalR.Hosting.Self -Version 0.5.2

PM>安裝,包裝Microsoft.AspNet.SignalR.Client

然後將下面的代碼添加到您的項目;)

(運行項目爲管理員)

服務器控制檯應用程序:

using System; 
using SignalR.Hubs; 

namespace SignalR.Hosting.Self.Samples { 
    class Program { 
     static void Main(string[] args) { 
      string url = "http://127.0.0.1:8088/"; 
      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; 
       } 
      } 
     } 

     [HubName("CustomHub")] 
     public class MyHub : Hub { 
      public string Send(string message) { 
       return message; 
      } 

      public void DoSomething(string param) { 
       Clients.addMessage(param); 
      } 
     } 
    } 
} 

客戶端控制檯應用程序:

using System; 
using SignalR.Client.Hubs; 

namespace SignalRConsoleApp { 
    internal class Program { 
     private static void Main(string[] args) { 
      //Set connection 
      var connection = new HubConnection("http://127.0.0.1:8088/"); 
      //Make proxy to hub based on hub name on server 
      var myHub = connection.CreateHubProxy("CustomHub"); 
      //Start connection 

      connection.Start().ContinueWith(task => { 
       if (task.IsFaulted) { 
        Console.WriteLine("There was an error opening the connection:{0}", 
             task.Exception.GetBaseException()); 
       } else { 
        Console.WriteLine("Connected"); 
       } 

      }).Wait(); 

      myHub.Invoke<string>("Send", "HELLO World ").ContinueWith(task => { 
       if (task.IsFaulted) { 
        Console.WriteLine("There was an error calling send: {0}", 
             task.Exception.GetBaseException()); 
       } else { 
        Console.WriteLine(task.Result); 
       } 
      }); 

      myHub.On<string>("addMessage", param => { 
       Console.WriteLine(param); 
      }); 

      myHub.Invoke<string>("DoSomething", "I'm doing something!!!").Wait(); 


      Console.Read(); 
      connection.Stop(); 
     } 
    } 
} 
+0

你可以在windows應用程序中使用上面的代碼,但它真的有必要嗎?!我不確定你的意思,你可以用其他方式在窗口中通知。 –

+0

哪些其他方法?在WinForms中不好使用SignalR? – Kiquenet

+0

客戶端與服務器0.5.2一起工作到1.0.0-alpha2例如Install-Package Microsoft.AspNet.SignalR.Client -version 1.0.0-alpha2 https://www.nuget.org/packages/Microsoft.AspNet.SignalR.Client/1.0.0-alpha2(代碼和SignalR版本應與.net 4.0使用VS2010 SP1)試圖弄清楚爲什麼我無法使它工作,最終在SignalR早期版本中嘗試過客戶端。 –

7

示例SignalR 2.2.1(2017年5月)

服務器

安裝-封裝Microsoft.AspNet.SignalR.SelfHost -Version 2.2.1

[assembly: OwinStartup(typeof(Program.Startup))] 
namespace ConsoleApplication116_SignalRServer 
{ 
    class Program 
    { 
     static IDisposable SignalR; 

     static void Main(string[] args) 
     { 
      string url = "http://127.0.0.1:8088"; 
      SignalR = WebApp.Start(url); 

      Console.ReadKey(); 
     } 

     public class Startup 
     { 
      public void Configuration(IAppBuilder app) 
      { 
       app.UseCors(CorsOptions.AllowAll); 
       app.MapSignalR(); 
      } 
     } 

     [HubName("MyHub")] 
     public class MyHub : Hub 
     { 
      public void Send(string name, string message) 
      { 
       Clients.All.addMessage(name, message); 
      } 
     } 
    } 
} 

客戶

(與Mehrdad Bahrainy的回覆幾乎相同)

安裝,包裝Microsoft.AspNet.SignalR.Client -Version 2.2.1

namespace ConsoleApplication116_SignalRClient 
{ 
    class Program 
    { 
     private static void Main(string[] args) 
     { 
      var connection = new HubConnection("http://127.0.0.1:8088/"); 
      var myHub = connection.CreateHubProxy("MyHub"); 

      Console.WriteLine("Enter your name");  
      string name = Console.ReadLine(); 

      connection.Start().ContinueWith(task => { 
       if (task.IsFaulted) 
       { 
        Console.WriteLine("There was an error opening the connection:{0}", task.Exception.GetBaseException()); 
       } 
       else 
       { 
        Console.WriteLine("Connected"); 

        myHub.On<string, string>("addMessage", (s1, s2) => { 
         Console.WriteLine(s1 + ": " + s2); 
        }); 

        while (true) 
        { 
         string message = Console.ReadLine(); 

         if (string.IsNullOrEmpty(message)) 
         { 
          break; 
         } 

         myHub.Invoke<string>("Send", name, message).ContinueWith(task1 => { 
          if (task1.IsFaulted) 
          { 
           Console.WriteLine("There was an error calling send: {0}", task1.Exception.GetBaseException()); 
          } 
          else 
          { 
           Console.WriteLine(task1.Result); 
          } 
         }); 
        } 
       } 

      }).Wait(); 

      Console.Read(); 
      connection.Stop(); 
     } 
    } 
} 
相關問題