2017-03-07 92 views
1

我在我的服務器應用程序中使用了服務堆棧。 這是啓動服務的代碼:心跳解釋

 public override void Configure(Container container) 
    { 
     LogManager.LogFactory = new KCServiceObjects.ServiceLoggerFactory(); 
     ServiceStack.Text.JsConfig.EmitCamelCaseNames = true; 

     Plugins.Add(new ServerEventsFeature() 
     { 
      HeartbeatInterval = TimeSpan.FromSeconds(60), 
      NotifyChannelOfSubscriptions = true, 

     }); 
     Plugins.Add(new ValidationFeature()); 

     container.Register<IServerEvents>(c => new MemoryServerEvents()); 
     notifier = new FrontendMessages(container.Resolve<IServerEvents>(), broker); 
     container.Register(c => notifier); 
     container.Register<IWebServiceEventManager>(c => 
        new WebServiceEventManager(broker)); 

     SetConfig(new HostConfig 
     { 
      DebugMode = true, 
      DefaultContentType = MimeTypes.Json, 
      EnableFeatures = Feature.All.Remove(Feature.Html), 
      GlobalResponseHeaders = 
      { 
       { "Access-Control-Allow-Origin", "*" }, 
       { "Access-Control-Allow-Methods", "GET, POST, PUT, DELETE" }, 
       { "Access-Control-Allow-Headers", "Content-Type" }, 
      }, 
     }); 
    } 

這是.NET客戶端:

   clientEvents = new ServerEventsClient(string.Format("http://{0}:{1}/", sIP, 20001), "messages"); 
      client = (IServiceClient)(clientEvents.ServiceClient); 

      clientEvents.Resolver = resolver; 
      clientEvents.RegisterReceiver<GlobalReceiver>(); 
      clientEvents.OnConnect = (e) => 
      { 
       var msg = JsonObject.Parse(e.Json); 
       ConnectionInfo = new ServerEventConnect 
       { 
        HeartbeatIntervalMs = DefaultHeartbeatMs, 
        IdleTimeoutMs = DefaultIdleTimeoutMs, 
       }.Populate(e, msg); 

       ConnectionInfo.Id = msg.Get("id"); 
       ConnectionInfo.HeartbeatUrl = msg.Get("heartbeatUrl"); 
       ConnectionInfo.HeartbeatIntervalMs = msg.Get<long>("heartbeatIntervalMs"); 
       ConnectionInfo.IdleTimeoutMs = msg.Get<long>("idleTimeoutMs"); 
       ConnectionInfo.UnRegisterUrl = msg.Get("unRegisterUrl"); 
       ConnectionInfo.UserId = msg.Get("userId"); 
       ConnectionInfo.DisplayName = msg.Get("displayName"); 
       ConnectionInfo.ProfileUrl = msg.Get("profileUrl"); 


      }; 

在心跳不工作的時刻,但我敢肯定,我已經錯過了東西我的代碼。查看日誌,服務器發送一個STOP()然後一個START()。 如何在C#中實現?客戶端是否必須每隔n秒向服務器發送消息?

感謝所有 萊昂納多

回答

2

有幾件事情:

如果要更改間隔,你也需要改變的IdleTimeout,如:

Plugins.Add(new ServerEventsFeature { 
    HeartbeatInterval = TimeSpan.FromSeconds(60), 
    IdleTimeout = TimeSpan.FromSeconds(180), 
}); 

你不需要設置NotifyChannelOfSubscriptions = true,這是默認值。

你並不需要註冊MemoryServerEvents,它的默認:

//container.Register<IServerEvents>(c => new MemoryServerEvents()); 

切勿自己填充ConnectionInfo就像你在OnConnect處理程序做的,如果操作不當,它可能會破壞行爲。

而是加入CORS GlobalResponseHeaders的,只是註冊CorsFeature插件,e.g:

Plugins.Add(new CorsFeature()); 
+1

感謝。現在心跳正在起作用! –