2016-08-25 3 views
0

でグループを作成するには:どのように私は私のsignalRハブでグループを作成する必要がありますがContext.ConnectionIdラインがnull SignalR

public class MetricHub : Hub 
    { 
     public MetricHub() 
     { 
      Groups.Add(Context.ConnectionId, "Syslog"); 
      Groups.Add(Context.ConnectionId, "Heatmap"); 
     } 
    } 

と、これはMetricHubクラスを使用していますbackgroundTickerクラスです:

using AutoMapper; 
using Makbin.Data; 
using Makbin.Web.Model; 
using Makbin.Web.ViewModels; 
using Microsoft.AspNet.SignalR; 
using Newtonsoft.Json; 
using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Threading; 
using System.Web; 
using System.Web.Hosting; 

namespace Makbin.Web.Hubs 
{ 
    public class test 
    { 
     public int id { get; set; } 
     public string name { get; set; } 
    } 
    public class BackgroundTicker : IRegisteredObject 
    { 

     private readonly MakbinRepository _repository; 
     private Timer devicesTimer; 
     private IHubContext hub; 



     public BackgroundTicker() 
     { 

      _repository = new MakbinRepository(); 

      HostingEnvironment.RegisterObject(this); 

      hub = GlobalHost.ConnectionManager.GetHubContext<MetricHub>();   



      devicesTimer = new Timer(OnDevicesTimerElapsed, null, 
       TimeSpan.FromSeconds(5), TimeSpan.FromSeconds(5)); 
     } 


     private void OnDevicesTimerElapsed(object sender) 
     { 

      var result = _repository.Peripherals.Select(x => new { x.PeripheralId, x.Severity }); 
      var finalResult = JsonConvert.SerializeObject(result, Formatting.None, 
         new JsonSerializerSettings() 
         { 
          ReferenceLoopHandling = ReferenceLoopHandling.Ignore 
         }); 

      hub.Clients.Group("Heatmap").broadcastMessage(DateTime.UtcNow.ToString(), finalResult); 
     } 

     public void Stop(bool immediate) 
     { 
      //deviceDetailTimer.Dispose(); 
      devicesTimer.Dispose(); 

      HostingEnvironment.UnregisterObject(this); 
     } 
    } 
} 

このコードの目的は、データを送信する前にグループを定義することです。 ここでは、Syslog、Heatmapという2つのグループを定義しました。また、クライアントへの私の接続は一方向(クライアントからサーバーへ)です。

答えて

0

Configuration機能にsignalR(認証を設定した後)をマッピングすることを忘れないでください:

public void Configuration(IAppBuilder app) 
{ 

    // Enable the application to use a cookie to store information for the signed i user 
    app.UseCookieAuthentication(new CookieAuthenticationOptions 
    { 
     AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie, 
     LoginPath = new PathString("/Home/Index") 
    }); 

    ... 


    app.MapSignalR(); 
} 

はsignalRでスタート行き方:

http://www.asp.net/signalr/overview/getting-started/tutorial-getting-started-with-signalr

+0

を私は任意の認証部分を持っていません私のプロジェクトでは、認証が重要ですか? –

+1

重要な行は必須ではありません。「app.MapSignalR();」 –

+0

@AminMohammadiはこれで問題を解決しましたか? –

関連する問題