public void Subscribe(SubscribeArg arg) { var newClientInfo = GetClientInfo(); var callback = OperationContext.Current.GetCallbackChannel <IEventCallback>(); arg.Username = arg.Username.ToLower(); if (!_Subscribers.ContainsKey(arg.Username)) { _Subscribers[arg.Username] = new SubscribeContext() { Arg = arg, Callback = callback, Address = newClientInfo.Item1, Port = newClientInfo.Item2 }; } else { if (_Subscribers[arg.Username].Address != newClientInfo.Item1 || _Subscribers[arg.Username].Port != newClientInfo.Item2) { _Subscribers[arg.Username] = new SubscribeContext() { Arg = arg, Callback = callback, Address = newClientInfo.Item1, Port = newClientInfo.Item2 }; } } ICommunicationObject obj = (ICommunicationObject)callback; obj.Closed += (sender, args) => { //current client closed, do nothing. }; obj.Faulted += (sender, args) => { //To do: //Record error }; obj.Closing += (sender, args) => { var currentCallback = sender as IEventCallback; _Subscribers.ToList().ForEach(context => { if (context.Value.Callback == currentCallback) { RemoveSubscriber(context.Value.Arg.Username); ClientLostEvent?.Invoke(arg); } }); }; NewClientSubscribedEvent?.Invoke(arg); }
private void EventService_NewClientSubscribedEvent(SubscribeArg arg) { NewClientSubscribed?.Invoke(this, new DataEventArgs(arg.Username)); }
private void EventService_ClientLostEvent(SubscribeArg arg) { ClientLost?.Invoke(this, new DataEventArgs(arg.Username)); }