// subscribes on server and receives a message public async Task SendMessageToClient() { var pipeName = Guid.NewGuid().ToString(); var clientConnectedEvent = new ManualResetEventSlim(); var @event = new ManualResetEventSlim(); int recvCount = 0; Task Handler(MyMessage x) { if (++recvCount == 10) { @event.Set(); } return(Task.CompletedTask); } // creating remote hub var t = Task.Run(async() => { var hub = new MetaPubSub(); hub.StartServer(pipeName); // wait for the subscriber clientConnectedEvent.Wait(5000); // publishing a message at the remote hub for (int i = 0; i < 10; i++) { await hub.Publish(new MyMessage()); } }); // local hub creation var hub = new MetaPubSub(); await hub.ConnectToServer(pipeName); await hub.SubscribeOnServer <MyMessage>(Handler); // delay allowing the server to process the subscription request await Task.Delay(100); clientConnectedEvent.Set(); @event.Wait(5000); // unsubscribing await hub.Unsubscribe <MyMessage>(Handler); Assert.IsTrue(@event.IsSet && recvCount == 10); }
static async void RunClient() { try { // local hub creation hub = new MetaPubSub(); // connecting the remote server await hub.ConnectToServer("Meta"); // subscribing await hub.SubscribeOnServer <PingCommand>(OnPingCommand); } catch (Exception ex) { Console.WriteLine(ex.Message); } }
async Task BasicExample() { int count = 0; Task Handler(MyMessage x) { count++; return(Task.CompletedTask); } // Creating the server hub. // The server and the client hubs should be created in separate processes, // this example is for demo only. var serverHub = new MetaPubSub(); // Starting the hub as a server named 'Meta'. serverHub.StartServer("Meta"); // Client hub creation. There are can be several hubs connected to the same server. var clientHub = new MetaPubSub(); // Connecting to the remote server. await clientHub.ConnectToServer("Meta"); // Subscribing to MyMessage on the server and locally at the same time. await clientHub.SubscribeOnServer <MyMessage>(Handler); // The server publishes a message. await serverHub.Publish(new MyMessage()); // Client hub publishes a message and it will be received locally without being sent to the server. await clientHub.Publish(new MyMessage()); // Client hub sends a message to the server where it will be published and sent back. await clientHub.PublishOnServer(new MyMessage()); // All three messages should be received. Debug.Assert(count == 3); // Unsubscribing both on the server-side and locally. await clientHub.Unsubscribe <MyMessage>(Handler); }