public void Setup() { void Increment(string message) { } _hubSinglePublisherSingleSubscriber.Subscribe <string>(Increment); _hubSinglePublisherSingleSubscriberSimple.Subscribe <string>(Increment); void IncrementMultiple(int msg) { } _hubSinglePublisherMultipleSubscriber.Subscribe <int>(IncrementMultiple); _hubSinglePublisherMultipleSubscriber.Subscribe <int>(IncrementMultiple); _hubSinglePublisherMultipleSubscriber.Subscribe <int>(IncrementMultiple); _hubSinglePublisherMultipleSubscriberSimple.Subscribe <int>(IncrementMultiple); _hubSinglePublisherMultipleSubscriberSimple.Subscribe <int>(IncrementMultiple); _hubSinglePublisherMultipleSubscriberSimple.Subscribe <int>(IncrementMultiple); _hubMultiplePublisherSingleSubscriber.Subscribe <string>(Increment); _hubMultiplePublisherSingleSubscriberSimple.Subscribe <string>(Increment); _hubMultiplePublisherSingleSubscriberAndGlobalAuditHandler.RegisterGlobalHandler((type, msg) => { }); _hubMultiplePublisherSingleSubscriberAndGlobalAuditHandlerSimple.RegisterGlobalHandler((type, msg) => { }); _hubMultiplePublisherSingleSubscriberAndGlobalAuditHandler.Subscribe <string>(x => { }); _hubMultiplePublisherSingleSubscriberAndGlobalAuditHandlerSimple.Subscribe <string>(x => { }); }
public void Connect() { IPAddress ipAddress = IPAddress.Any; IPEndPoint localEndPoint = new IPEndPoint(ipAddress, 5000); Socket listener = new Socket(localEndPoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp); listener.Bind(localEndPoint); listener.Listen(10); messageHub = new MessageHub(); messageHandler = new MessageHandler(clientRoomRepository, socketRepository, this, messageRepository); messageHub.RegisterGlobalHandler((type, message) => { messageHandler.Publish(type, message); }); publisher = new Publisher(messageHub); this.Socket = listener; while (true) { // The threads are put into waiting state allDone.Reset(); Console.WriteLine("Waiting for a connection..."); //start accepting new connections //AcceptCallback is called when a new connection request is received //Listener as object to pass state information to callback this.Socket.BeginAccept(new AsyncCallback(AcceptCallback), this.Socket); //block theards allDone.WaitOne(); } }
static void Main(string[] args) { IMessageHub hub = MessageHub.Instance; var intSub = hub.Subscribe <int>(p => { Console.WriteLine($"Id is: {p}"); }); Action <string> strAction = message => Console.WriteLine($"Message is: {message}"); Action <Type, object> globalHandler = (t, obj) => { Console.WriteLine($"GlobalHandler: Publish<{t.Name}>({obj.ToString()})"); }; hub.RegisterGlobalHandler(globalHandler); Action <Type, object> globalHandler2 = (t, obj) => { Console.WriteLine($"LoggingHandler: Publish<{t.Name}>({obj.ToString()})"); }; hub.RegisterGlobalHandler(globalHandler2); hub.OnError += (sender, e) => { Console.WriteLine($"OnError: {e.UserAction}({e.Token.ToString()}) throw Exception:{e.Exception.Message.ToString()}"); }; var strSub = hub.Subscribe(strAction); var orderConfirmationSub = hub.Subscribe <OrderConfirmation>(OrderAction); hub.Publish(new Order() { Id = "2001934102", Name = "lalala" }); hub.Publish(new OrderConfirmation() { Id = "2001934102", Name = "lalala", Status = "Confirmed" }); hub.Publish(123); hub.Publish("this is a string"); Console.WriteLine($"Hub.IsRegistered<int>: {hub.IsRegistered<int>()}"); Console.WriteLine(); Console.WriteLine("Registerd Type List: "); hub.GetRegisteredType().ToList().ForEach(f => { Console.WriteLine(f.Name); }); Console.WriteLine(); hub.UnSubscribe(intSub); hub.Publish(0987654321); Console.WriteLine($"Hub.GetGeneration: {GC.GetGeneration(hub)}"); Console.WriteLine($"Hub.IsRegistered<int>: {hub.IsRegistered(0987654321)}"); Console.WriteLine($"Hub.IsRegistered<int>: {hub.IsRegistered((0987654321).GetType())}"); Console.WriteLine($"Hub.IsRegistered<typeof(int)>: {hub.IsRegistered(typeof(int))}"); Console.ReadKey(); }