3

SignalR を使用して、アプリケーション内の実行時間の長いプロセスのユーザー エクスペリエンスを向上させることに関心があり、最初のテスト SignalR プロジェクトを作成しました。空の Web プロジェクトを作成し、NuGet を使用して SignalR.Sample パッケージをインストールしました。StockTicker.html ページの例は完全に機能します。次に、独自のハブとテスト ページを作成しました。

using System.Threading;
using Microsoft.AspNet.SignalR.Hubs;

namespace SignalR.Test
{
   [HubName("testHub")]
   public class TestHub : Hub
   {
      public void LongRunningProcess()
      {
         Thread.Sleep(1000);
         this.Clients.Caller.updateStatus("25% Completed");
         Thread.Sleep(1000);
         this.Clients.Caller.updateStatus("50% Completed");
         Thread.Sleep(1000);
         this.Clients.Caller.updateStatus("75% Completed");
         Thread.Sleep(1000);
         this.Clients.Caller.updateStatus("Done");
      }
   }
}


<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
   <title>SignalR - Long Running Process</title>
</head>
<body>
   <h1>Long Running Process</h1>

   <p>Status:</p>

   <ul id="status">
      <li>Loading hub...</li>
   </ul>

   <script src="/bundles/jquery"></script>
   <script src="/Scripts/jquery.signalR-1.0.0-alpha2.js"></script>
   <script src="/signalr/hubs"></script>
   <script type="text/javascript">
      $(function () {
         var hub = $.connection.testHub;

         hub.updateStatus = function (message) {
            $("#status").append("<li>" + message + "</li>");
         };

         $.connection.hub.start().done(function () {
            $("#status").children().first().text("Hub loaded.");
            hub.longRunningProcess();
         })
         .fail(function() {
            $("#status").children().first().text("Hub failed");
         });
      })
   </script>
</body>
</html>

ページを実行すると、次の (Firebug) エラーが発生します。

TypeError: hub.longRunningProcess is not a function
    hub.longRunningProcess();

/signalr/hubs を見ると、ファイルの末尾に次のスクリプトが表示されます。

signalR.testHub = signalR.hub.createHubProxy('testHub'); 
signalR.testHub.client = { };
signalR.testHub.server = {
    longRunningProcess: function () {
        return signalR.testHub.invoke.apply(signalR.testHub, $.merge(["LongRunningProcess"], $.makeArray(arguments)));
     }
};

私がどこで間違っているかについてのアドバイス/ポインタは大歓迎です。

4

1 に答える 1

5

問題を見つけました。クライアントスクリプトは次のようになります。

      $(function () {
         var hub = $.connection.testHub;

         hub.client.updateStatus = function (message) {
            $("#status").append("<li>" + message + "</li>");
         };

         $.connection.hub.start().done(function () {
            $("#status").children().first().text("Hub loaded.");
            hub.server.longRunningProcess();
         })
         .fail(function() {
            $("#status").children().first().text("Hub failed");
         });
      })

ハブメソッド宣言に.clientプロパティと.serverプロパティが追加されていることに注意してください。たとえば、hub.client.updateStatus()やhub.server.longRunningProcess()などです。

于 2012-11-22T06:07:48.540 に答える