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秒向服務器發送消息?
感謝所有 萊昂納多
感謝。現在心跳正在起作用! –