2012-06-21 59 views
43

Có một ví dụ nhỏ về bảng điều khiển hoặc ứng dụng winform sử dụng signalR để gửi tin nhắn tới hub .net không ?. Tôi đã thử các ví dụ .net và đã xem xét wiki nhưng nó không có ý nghĩa với tôi mối quan hệ giữa hub (.net) và client (giao diện điều khiển ứng dụng) (không thể tìm thấy một ví dụ về điều này). Ứng dụng có cần địa chỉ và tên của trung tâm để kết nối không ?.Ví dụ về ứng dụng SignalR Console

Nếu ai đó có thể cung cấp một chút nhỏ mã hiển thị ứng dụng kết nối với một trung tâm và gửi "Hello World" hoặc một cái gì đó mà .net hub nhận được ?.

PS. Tôi có một ví dụ chat trung tâm tiêu chuẩn hoạt động tốt, nếu tôi cố gắng gán một tên trung tâm trong Cs cho nó, nó ngừng hoạt động tức là [HubName ("test")], bạn có biết lý do cho điều này không ?.

Cảm ơn.

Mã ứng dụng Bảng điều khiển hiện tại.

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

Máy chủ Hub. (Khác với dự án không gian làm việc)

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

Thông Tin Wiki cho điều này là http://www.asp.net/signalr/overview/signalr-20/hubs-api/hubs-api-guide-net-client

+0

O.K thực sự điều này thực sự đã làm việc chỉ nghĩ rằng tôi đã nhận được kết quả tương tự chỉ cần thêm một số điểm dừng và Console.ReadLine(); cuối cùng. Whoop !. – user685590

Trả lời

67

trước hết bạn nên cài đặt SignalR.Host.Self trên ứng dụng máy chủ và SignalR.Client trên ứng dụng client của bạn bằng cách NuGet:

PM> Install-Package SignalR.Hosting.Self -version 0.5.2

PM> Install-Package Microsoft.AspNet.SignalR.Client

Sau đó, thêm đoạn mã sau vào dự án của bạn;)

(chạy dự án as administrator)

Server console app:

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

Khách hàng giao diện điều khiển ứng dụng:

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

Bạn có thể sử dụng mã trên trong ứng dụng windows nhưng nó thực sự cần thiết? Tôi không chắc chắn ý bạn là gì, bạn có thể thông báo trong cửa sổ theo những cách khác. –

+0

có cách nào khác? Sử dụng SignalR không tốt trong WinForms? – Kiquenet

+0

ứng dụng khách hoạt động với máy chủ 0.5.2 tối đa 1.0.0-alpha2 ví dụ: Gói cài đặt Microsoft.AspNet.SignalR.Client -version 1.0.0-alpha2 https://www.nuget.org/packages/Microsoft.AspNet.SignalR.Client/1.0.0-alpha2 (các phiên bản mã và SignalR sẽ hoạt động với .net 4.0 sử dụng VS2010 SP1) đang cố gắng tìm ra lý do tại sao tôi không thể làm cho nó hoạt động, cuối cùng đã thử khách hàng với các phiên bản đầu tiên của SignalR. –

7

Ví dụ cho SignalR 2.2.1 (tháng 5 năm 2017)

server

Install-Package 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); 
      } 
     } 
    } 
} 

Khách hàng

(gần như giống như trả lời của Mehrdad Bahrain)

Install-Package 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(); 
     } 
    } 
}