2017-04-13 6 views
4

SignalRを使用してクライアント上にいくつかのライブランダムデータを表示したいとします。SignalR Page Refreshにより複数の接続が可能

問題ページを更新するたびに、もう1つの接続 が作成され、複数のデータが表示されます。

ほとんど私のアプローチは間違っていると思います。

私は何をしましたか?

ステップ1:インストールSignalR Nuget Install-Package Microsoft.AspNet.SignalR

ステップ2使用して次のようにStartup.csファイルに加えた変更を。

public partial class Startup 
{ 
    public void Configuration(IAppBuilder app) 
    { 
     ConfigureAuth(app); 
     app.MapSignalR(); //Added this line for SignalR 
    } 
} 

ステップ3:ハブクラスを作成しました。 "ServerStatisticsHub.cs"

public class ServerStatisticsHub : Hub 
{ 
    public void ServerParameter() 
    { 
     Random r = new Random(); 
     int p = 0; 
     int m = 0; 
     int s = 0; 

     while(true) //May be this is the foolish thing I'm doing 
     { 
      p = r.Next(0, 100); 
      m = r.Next(0, 100); 
      s = r.Next(0, 100); 
      Clients.All.broadcastServerStatistics("{\"processor\":" + p + ", \"memory\":" + m + ", \"storage\":" + s + "}"); 
      System.Threading.Thread.Sleep(2000); 
     } 
    } 
} 

ステップ4:ホーム "ServerState.cshtml"でビューを作成しました。

<!DOCTYPE html> 
<html> 
<head> 
    <meta charset="utf-8" /> 
    <meta http-equiv="X-UA-Compatible" content="IE=edge"> 
    <meta content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no" name="viewport"> 
    <title>TestSignalR</title> 
</head> 
<body> 
    <div id="serverProcessor"></div> 
    <div id="serverMemory"></div> 
    <div id="serverStorage"></div> 

    <script src="@Url.Content("~/Scripts/jquery-2.2.3.min.js")"></script>  
    <script src="@Url.Content("~/Scripts/jquery.signalR-2.2.1.min.js")"></script> 
    <script src="@Url.Content("~/signalr/hubs")"></script> 
    <script> 
     $(function() { 
      // Reference the auto-generated proxy for the hub. 
      var serverStatistics = $.connection.serverStatisticsHub; 

      // Create a function that the hub can call back to display messages. 
      serverStatistics.client.broadcastServerStatistics = function (serverStat) { 
       var serverStatistic = JSON.parse(serverStat); 
       console.log(serverStatistic); 

       $('#serverProcessor').html(serverStatistic.processor + "%"); 
       $('#serverMemory').html(serverStatistic.memory + "%"); 
       $('#serverStorage').html(serverStatistic.storage + "%"); 
      }; 

      // Start the connection. 
      $.connection.hub.start().done(function() { 
       serverStatistics.server.serverParameter(); 
      }); 
     }); 

    </script> 
</body> 
</html> 

答えて

2

この問題を解決する回避策が見つかりました。 私はそれを記述する方法を知らない。

ハブクラスファイルで行われた次のコード変更。

Clients.Allを変更し、 "ServerStatisticsHub.cs"

Clients.Client(Context.ConnectionId).broadcastServerStatistics("{\"processor\":" + p + ", \"memory\":" + m + ", \"storage\":" + s + "}"); 

Clients.Client(Context.ConnectionId)。

+1

私は本当に回避策とは言いませんが、これは実際には適切な解決策です。あなたの問題は、クライアントが接続するたびに無限ループを含む関数を呼び出すという事実によって引き起こされました。そこで、クライアント1は接続→無限ループを起動します。クライアント2が接続 - >無限ループが再び開始されました。彼らは別のスレッドで実行されるので、それを再度起動しても前のものは強制終了されません。どのような方法でもループを終了しないので、新しいクライアントごとに新しい無限ループが開始され、各ループはすべての他のクライアントにもメッセージをブロードキャストします。 –

+0

ページがリロードされるたびに無限ループを閉じるための解決策はありますか? –

+0

個人的には、最初は新しいクライアントごとに無限ループを起動しません。なぜ私はあなたがしたことが可能な解決策になる可能性があると言いました。メッセージが接続されたすべてのクライアントにブロードキャストされる必要がない場合、実際には可能性があるからです。 –