2016-12-26 9 views

答えて

0

このリンクReferenceを参照できます。

0

あなたはすべてのユーザーにブロードキャストが必要だと思います。次の例は、すべてのクライアントにマッサージをブロードキャストする基本的なコードを示しています。SendNotifications(" massage")と呼ぶ毎回、すべてのユーザーがあなたのマッサージを受け取ります。

public class EmployeeHub : Hub 
{ 

    public void SendNotifications(string message) 
{ 
    Clients.All.receiveNotification(message); 
} 
} 

とウェブページ:

<body> 
<input id="text1" type="text" /> 
<input id="button1" type="button" value="Send" /> 
<ul id="discussion"> 
</ul> 
<!--Reference the jQuery library. --> 
<script src="Scripts/jquery-1.6.4.min.js" type="text/javascript"></script> 
<!--Reference the SignalR library. --> 
<script src="Scripts/jquery.signalR-1.1.3.js" type="text/javascript"></script> 
<!--Reference the autogenerated SignalR hub script. --> 
<script src="signalr/hubs"></script> 
<script type="text/javascript"> 
    $(function() { 
     // Declare a proxy to reference the hub. 
     var notifications = $.connection.employeeHub; 
     // Create a function that the hub can call to broadcast messages. 
     notifications.client.receiveNotification = function (message) { 
      // alert(" says '" + message + "'"); 
      // Html encode display name and message.     
      var encodedMsg = $('<div />').text(message).html(); 
      // Add the message to the page. 
      $('#discussion').append('<li>' + encodedMsg + '</li>'); 
     }; 
     // Start the connection. 
     $.connection.hub.start().done(function() { 

     }); 
    }); 
</script> 

+0

有効ですが、このコードは質問に記載されている例(タイマーや他のサーバー側のイベントなど)では機能しません。したがって、downvotingです。 – thab

1

使用this answerとサーバー側でバックグラウンドタスクを実行します。

ライフサイクルがリクエストごとにあるため、ハブの内部にはありません。

+0

この回答は正しい – thab

関連する問題