/// <summary> /// 订阅服务器上的事件 /// </summary> /// <param name="eventName">事件名</param> /// <param name="s">回调的委托</param> public void SubscribeEvent(string eventName, EventProcessingHandler s) { try { //检查事件是否已由客户订阅 Delegate handler = (Delegate)repeatDelegate[eventName]; //如果已经订阅, 加入订阅链 //否则创建新的委托并添加到订阅的Hash表 if (handler != null) { //接入委托链 handler = Delegate.Combine(handler, s); ////在哈希表中重置委托链 repeatDelegate[eventName] = handler; } else { repeatDelegate.Add(eventName, s); EventClient.EventProcessingHandler repeat = new EventClient.EventProcessingHandler(Repeat); //订阅服务器上的事件 es.SubscribeEvent(eventName, repeat); } } catch (Exception ex) { Console.WriteLine(ex.Message); } }
/// <summary> /// Subscribe an event on the EventServer /// </summary> /// <param name="eventName">event name</param> /// <param name="s">delegate on the client side that process the event notification</param> public void SubscribeEvent(string eventName, EventProcessingHandler s) { try { //check if the event has already subscribed by the client Delegate handler = (Delegate)repeatDelegate[eventName]; //if already subscribed, chain up the delegates //otherwise, create a new delegate and add it to the repeatDelegate hashtable if (handler != null) { //chain up the delegates together handler = Delegate.Combine(handler, s); //reset the delegate object in the hashtable repeatDelegate[eventName] = handler; } else { repeatDelegate.Add(eventName, s); EventClient.EventProcessingHandler repeat = new EventClient.EventProcessingHandler(Repeat); //subscribe the "repeat" delegate to the EventServer. es.SubscribeEvent(eventName, repeat); } } catch (Exception ex) { Console.WriteLine(ex.Message); } }
/// <summary> ///取消订阅事件 /// </summary> /// <param name="eventName">事件名称</param> /// <param name="handler">客户端委托</param> public void UnSubscribeEvent(string eventName, EventClient.EventProcessingHandler handler) { //确保只有一个线程一次修改委托链。 lock (this) { Delegate delegateChain = (Delegate)delegateMap[eventName]; //如果委托链不为空,从委托链删除委托 if (delegateChain != null) { //从委托链删除指定的委托 delegateChain = Delegate.Remove(delegateChain, handler); //在哈希表中重置委托链 delegateMap[eventName] = delegateChain; } } }
/// <summary> /// UnsubscribeEvent remove an client side delegate from delegate chain /// </summary> /// <param name="eventName">event name</param> /// <param name="handler"></param> public void UnSubscribeEvent(string eventName, EventClient.EventProcessingHandler handler) { //ensure that only one thread modify the delegate chain at a time. lock (this) { Delegate delegateChain = (Delegate)delegateMap[eventName]; //check if the delegate chain is null. if not null, remove the //client side delegate from the delegate chain. if (delegateChain != null) { //remove the delegate from the chain delegateChain = Delegate.Remove(delegateChain, handler); //reset the delegate chain in the hashtable delegateMap[eventName] = delegateChain; } } }
/// <summary> /// 订阅事件 /// </summary> /// <param name="eventName">事件名</param> /// <param name="handler">客户端委托,用于通知</param> public void SubscribeEvent(string eventName, EventClient.EventProcessingHandler handler) { //确保只有一个线程一次修改委托链。 lock (this) { //检查是否已经注册该事件 Delegate delegateChain = (Delegate)delegateMap[eventName]; //如果委托链为空,说明没注册。添加客户端委托作为最初的处理程序。否则,添加委托链。 if (delegateChain == null) { delegateMap.Add(eventName, handler); } else { //添加委托到委托链 delegateChain = Delegate.Combine(delegateChain, handler); //在哈希表中重置委托链 delegateMap[eventName] = delegateChain; } } }
/// <summary> /// SubscribeEvent add the client delegate to delegate chain /// for a given event name /// </summary> /// <param name="eventName">event name</param> /// <param name="handler">client side delegate</param> public void SubscribeEvent(string eventName, EventClient.EventProcessingHandler handler) { //ensure that only one thread modify the delegate chain at a time. lock (this) { Delegate delegateChain = (Delegate)delegateMap[eventName]; //check if the delegate chain is null. if null, add the //client side delegate as the initial handler. Otherwise, //add delegate to the chain. if (delegateChain == null) { delegateMap.Add(eventName, handler); } else { //add the delegate to the chain delegateChain = Delegate.Combine(delegateChain, handler); //reset the delegate chain in the hashtable delegateMap[eventName] = delegateChain; } } }