/// <summary> /// Sends the message to all clients. /// </summary> /// <param name="message">Message to be sent.</param> /// <returns>Number of clients having received this message.</returns> public void SendMessage(string message) { lock (_clients) { Console.WriteLine("\"{0}\" message will be sent to all clients.", message); AsyncCallback asyncCallback = new AsyncCallback(OurAsyncCallbackHandler); foreach (DictionaryEntry entry in _clients) { IChatClient iChatClient = (IChatClient)entry.Value; ReceiveMessageEventHandler remoteAsyncDelegate = new ReceiveMessageEventHandler(iChatClient.ReceiveMessage); AsyncCallBackData asyncCallBackData = new AsyncCallBackData(); asyncCallBackData.RemoteAsyncDelegate = remoteAsyncDelegate; asyncCallBackData.MbrBeingCalled = (MarshalByRefObject)iChatClient; IAsyncResult RemAr = remoteAsyncDelegate.BeginInvoke(message, asyncCallback, asyncCallBackData); } } }
/// <summary> /// 测试代码 /// </summary> /// <param name="handler"></param> public void TestForPublish(ReceiveMessageEventHandler handler) { PublishTcpService.ReceiveMessageEventHandler += handler; }