WCF service - is CallBackChannel alive
-
hi guys, this seems to be easy, but i just cant find right answer. i have WCF duplex service to host online poker game (silverlight on client). if one client plug of his cable or meteor strikes at his home i need to disconnect him. it is easy to implement method Disconnect, but i cant call it if user is unsuspectedly disconnected... my approach is: 1. enter - open page 2. connect - client calls method Connect wich is OperationContract on server; server have array field of all connected clients - CallbackContract 3. do something - send to server your move (irrelevant logic for this question) 4. check clients - this is what i dont have. i want to remove disconected users from my array field of all connected clients 5. recive info - server says what happend to all clients via CallbackContract OperationContext.Current.Channel.Closed and OperationContext.Current.Channel.Faulted sounds like what i need, but they act strange?
public class Poker : IPokerService
{
HttpContext appContext;HttpContext AppContext { get { if(appContext == null) appContext = HttpContext.Current; return appContext; } } Dictionary<string, IPokerClient> ClientList { get { object temp = AppContext.Application\["ClientList"\]; if (temp == null) { temp = new Dictionary<string, IPokerClient>(); AppContext.Application\["ClientList"\] = temp; } return (Dictionary<string, IPokerClient>)temp; } } public void Connect(string id) { Dictionary<string, IPokerClient> clientList = ClientList; try { IPokerClient client = OperationContext.Current.GetCallbackChannel<IPokerClient>(); if (!clientList.ContainsKey(id)) clientList.Add(id, client); else clientList\[id\] = client; OperationContext.Current.Channel.Faulted += new EventHandler(Channel\_Faulted); OperationContext.Current.Channel.Closed += new EventHandler(Channel\_Closed); } catch { //later } } void Channel\_Closed(object sender, EventArgs e) { }