2013-04-14 18 views
5

"someHub" ile bir bağlantıya sahip olan konsol uygulamasına veri göndermek istiyorum. Örnekte a link'dan tanımlandığı gibi yapmaya çalıştım ama sonuç almamıştım. Sunucu tarafında kod:SinyalR: Sunucudan Net istemci yöntemi nasıl aranır?

[HubName("somehub")] 
public class SomeHub : Hub 
{ 
    public override Task OnConnected() 
    { 
     //Here I want to send "hello" on my sonsole application 
     Clients.Caller.sendSomeData("hello"); 

     return base.OnConnected(); 
    } 
} 

Clien yan kodu:

public class Provider 
{ 
    protected HubConnection Connection; 
    private IHubProxy _someHub; 

    public Provider() 
    { 
     Connection = new HubConnection("http://localhost:4702/"); 
     _someHub = Connection.CreateHubProxy("somehub"); 
     Init(); 
    } 

    private void Init() 
    { 
     _someHub.On<string>("sendSomeData", s => 
     { 
      //This code is not reachable 
      Console.WriteLine("Some data from server({0})", s); 
     }); 

     Connection.Start().Wait(); 
    } 
} 

bu uygulanmasına ilişkin en iyi çözüm nedir ve ben istemci yöntemi çağırmak mümkün değilim sebebi nedir?

+0

MapHubs'ı aradınız mı? – halter73

+0

Evet yaptım. İstemciden sunucuya yapılan aramalar şu şekilde çalışır: myHub.Invoke ("GetValue"). ContinueWith (görev => Console.WriteLine ("Sunucudan değer {0}", task.Result)); , ancak sunucudan istemciye - Hayır – Denis

+0

Olası çoğaltması [SignalR + bir eylem yöntemiyle bir Hub'a ileti gönderme] (http://stackoverflow.com/questions/7549179/signalr-posting-a-message- 0-a-hub-yoluyla-eylem-yöntemi) – Liam

cevap

9

Hub'ın dışındaki istemcilerle konuşmaya mı çalışıyorsunuz? Eğer evetse, Hub'ın dışında bir HubContext almanız gerekecektir. Ve sonra tüm müşterileri konuşabilirsiniz. Owin Öz Host kullanarak

IHubContext context = GlobalHost.ConnectionManager.GetHubContext<MyHub>(); 

SignalR Sunucusu

class Program 
    { 
     static void Main(string[] args) 
     { 
      string url = "http://localhost:8081/"; 

      using (WebApplication.Start<Startup>(url)) 
      { 
       Console.WriteLine("Server running on {0}", url); 
       Console.ReadLine(); 
       IHubContext context = GlobalHost.ConnectionManager.GetHubContext<MyHub>(); 
       for (int i = 0; i < 100; i++) 
       { 
        System.Threading.Thread.Sleep(3000); 
        context.Clients.All.addMessage("Current integer value : " + i.ToString()); 
       } 
       Console.ReadLine(); 
      } 

     } 
    } 
    class Startup 
    { 
     public void Configuration(IAppBuilder app) 
     { 
      // Turn cross domain on 
      var config = new HubConfiguration { EnableCrossDomain = true }; 
      config.EnableJavaScriptProxies = true; 

      // This will map out to http://localhost:8081/signalr by default 
      app.MapHubs(config); 
     } 
    } 
    [HubName("MyHub")] 
    public class MyHub : Hub 
    { 
     public void Chatter(string message) 
     { 
      Clients.All.addMessage(message); 
     } 
    } 

Signalr Hub tüketen Signalr İstemci konsol uygulaması.

class Program 
    { 
     static void Main(string[] args) 
     { 
      var connection = new HubConnection("http://localhost:8081/"); 

      var myHub = connection.CreateHubProxy("MyHub"); 

      connection.Start().Wait(); 
      // Static type 
      myHub.On<string>("addMessage", myString => 
      { 
       Console.WriteLine("This is client getting messages from server :{0}", myString); 
      }); 


      myHub.Invoke("Chatter",System.DateTime.Now.ToString()).Wait(); 


      Console.Read(); 
     } 
    } 

, bu kodu çalıştırmak iki ayrı uygulamalar, daha sonra ilk çalıştırma sunucu uygulaması ve ardından istemci konsol uygulaması oluşturmak için, o zaman sadece sunucu konsolunda tuşuna basın ve müşteriye mesaj göndermeye başlar.

+2

Teşekkürler. Ben Clients.Caller.sendSomeData ("merhaba") değiştirildi; İstemciler.Client (Context.ConnectionId) .sendOrders ("merhaba"); ve her şey çalışıyor. – Denis

+1

Bu cevap soruya cevap vermiyor, ilgisiz ve genel bir bilgi cevabının daha fazlası. Son olarak Clients.Caller.sendSomeData ("merhaba") değiştiriliyor; İstemciler.Client (Context.ConnectionId) .sendOrders ("merhaba"); bir fark yaratmamalı. Bu cevabı işaretlemenin doğru olduğunu ve sorunu daha ayrıntılı bir şekilde araştırmanızı öneririm. –

İlgili konular