【发布时间】:2010-05-19 13:12:20
【问题描述】:
正如我的标题中所写,
背景: 我有 2 种不同类型的应用程序 (WPF-silverlight) 可以相互通信 - 医生应用程序和患者应用程序 - 但这并不意味着只有 2 个应用程序会运行,例如: 我可以运行 3 个医生应用程序和 7 个患者应用程序。 并且所有这些应用程序都通过 tcp 连接使用 wcf 进行通信。 通信是实时的(如信使应用程序)
流动 每次有应用程序在线(运行)时,我都会在 wcf 注册它的连接,因为我需要让其他应用程序知道(实时)有新客户端连接或新客户端断开连接。
问题: 可以让其他应用知道有传入的应用/客户端, 但我的问题是,如何让其他应用知道此客户端是否已断开连接,
如果用户正确关闭应用程序(例如单击关闭按钮)很好 - 所以在 wpf 中,我可以调用 wcf 取消注册连接,
但是如果连接异常终止(例如,直接拔掉你电脑的电源线)怎么办? 有什么方法可以让我知道这个客户端是否仍然连接?
当我在我的 VS2008 中按 f5 并关闭,然后再次打开并关闭(重复)然后,当我调试时,仍然有很多连接存储在那里,但实际上客户端已经被破坏,我意识到了这个问题。
所以有人知道这个最佳解决方案吗? 非常感谢示例
我的代码 sn-p:
Dictionary<Guid, Client> Connections = new Dictionary<Guid, Client>();
// above is the variable where i put the connections
object syncObj = new object();
public ITcpServiceCallback CurrentCallback { get { return OperationContext.Current.GetCallbackChannel<ITcpServiceCallback>(); } }
// this function is called when the program started
public List<Client> ShakeHand( Client client, RoleType appType ) {
if( GetClientsByCallback( CurrentCallback ).Count < 1 && GetClientsByID( client.ID ).Count < 1 ) {
List<Client> retVal = new List<Client>();
lock( syncObj ) {
if( appType == RoleType.Doctor ) {
List<Client> doctors = Helpers.GetDoctor( AppDomain.CurrentDomain.BaseDirectory + "App_Data/doctor.xml" );
foreach( Client doctor in doctors ) {
doctor.Status = ConnectionStatus.Offline;
foreach( Client con in Connections.Values ) {
if( con.Role == RoleType.Doctor && con.ID == doctor.ID ) {
doctor.Status = ConnectionStatus.Online;
break;
}
}
retVal.Add( doctor );
}
} else { //b la.. bla similiar like if above
}
client.Callback = CurrentCallback;
client.Status = ConnectionStatus.Online;
// this is the code where i add the connection
Connections.Add( Guid.NewGuid(), client );
}
return retVal;
}
return null;
}
提前谢谢你
【问题讨论】: