我用以下方法解决了这个问题:
1.从客户端到服务器的 ping 方法以保持连接每 30 秒处于活动状态。
2.服务器绑定时,ReceiveTimeout 为 1 分钟。
3.Foreach回调创建了一个IcommunicationObject,使用Closed事件移除不活动的客户端。
//Adding a client callback
OperationContext context = OperationContext.Current;
ICallback callback = context.GetCallbackChannel();
ICommunicationObject obj = (ICommunicationObject)callback;
obj.Closed += new EventHandler(obj_Closed);
//Event for inactive clients
void obj_Closed(object sender, EventArgs e)
{
if (_callbacks.ContainsValue(((ITecnobelRemoteServiceCallback)sender)))
{
var item = _callbacks.First(kvp => kvp.Value == ((ITecnobelRemoteServiceCallback)sender));
_callbacks.Remove(item.Key);
treeViewClients.Nodes.RemoveByKey(item.Key.Id);
treeViewClients.Refresh();
_registeredUsers--;
listBoxStatus.Items.Add(String.Format("Usuário {0} estava inativo e foi removido", item.Key.Id));
}
}