2013-04-02 108 views
1

我正在開發SignalR應用程序,其中一組.NET控制檯客戶端連接到Web服務器。我如何使用SignalR從服務器調用特定的客戶端。呼叫SignalR中的特定客戶端

在我當前的應用程序中,當我從服務器端單擊按鈕時。它觸發所有控制檯客戶端。 但我想要觸發的是保留server.html上的所有客戶端信息並調用特定的客戶端。

目前,這是我的控制檯應用程序

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

     //Establishing the connection 
     connection.Start().ContinueWith(task => 
     { 
      if (task.IsFaulted) 
      { 
       Console.WriteLine("Failed to start: {0}", task.Exception.GetBaseException()); 
      } 
      else 
      { 
       Console.WriteLine("Success! Connected with client connection id {0}", connection.ConnectionId); 
      } 
     }); 

     //Reciveing data from the server 
     connection.Received += data => 
     { 
      Console.WriteLine("Receiving print request from server"); 
      Console.WriteLine(data);  
     }; 

     Console.ReadLine(); 
    } 
} 

這是服務器端。

這就要求客戶

<script type="text/javascript"> 
     $(function() { 
      var connection = $.connection('/printer'); 

      connection.received(function (data) { 
       //$('#messages').append('<li>' + data + '</li>'); 
      }); 

      connection.start().done(function() { 
       $('#print').click(function() { 
        var printThis = { value: 113, reportId: 'Report', printer: '3rd Floor Lazer Printer', Copies: 1 }; 
        connection.send(JSON.stringify(printThis)); 
       }); 
      }); 
     }); 
    </script> 

的Global.asax服務器HTML

protected void Application_Start(object sender, EventArgs e) 
    { 
     RouteTable.Routes.MapHubs(); 
     RouteTable.Routes.MapConnection<MyConnection>("printer", "/printer"); 

    } 
+1

你閱讀文檔做https://github.com/SignalR/SignalR/wiki /集線器#調用特定連接方法 – davidfowl

+0

是的。但是如果你能看到我使用的是連接而不是集線器。繼續進行不建議的連接? – Nipuna

回答