//[Ignore] // run this manually, when appropriate public async Task Can_Receive_Inbound_Messages_From_Terminal() { var cts = new CancellationTokenSource(); var server = new SynelServer(); server.AsyncMessageReceivedHandler = async notification => { if (notification.Type == NotificationType.Data) { Console.WriteLine(notification.Data); await notification.AcknowledegeAsync(); } if (notification.Type == NotificationType.Query) { Console.WriteLine(notification.Data); await notification.ReplyAsync(true, 0, "Success"); } cts.Cancel(); }; await server.ListenAsync(cts.Token); }
private static void Listen(bool acknowledge) { var server = new SynelServer(_port); server.MessageReceived += (sender, args) => { var notification = args.Notification; if (notification == null || notification.Data == null) { return; } Output("{0:yyyy-MM-dd HH:mm:sszzz} [{1}|{2}] {3}", DateTimeOffset.Now, notification.Client.RemoteEndPoint.Address, notification.TerminalId, notification.Data); if (acknowledge) { if (notification.Type == NotificationType.Data) { notification.Acknowledege(); } if (notification.Type == NotificationType.Query) { notification.Reply(true, 0, "OK", TextAlignment.Center); } Output(" [ACKNOWLEDGED]"); } OutputLine(); }; var cts = new CancellationTokenSource(); Console.CancelKeyPress += (sender, args) => { cts.Cancel(); args.Cancel = true; Console.WriteLine("Stopped listening."); }; Console.WriteLine("Listening on port {0}. Press Ctrl-C to terminate.", _port); server.ListenAsync(cts.Token).Wait(cts.Token); }