/// <summary> /// Defines the entry point of the application. /// </summary> /// <param name="args">The arguments.</param> static void Main(string[] args) { System.Console.WriteLine("Welcome to ChatClient!!!"); // User Identity System.Console.WriteLine("Please enter your name:"); var user = System.Console.ReadLine(); System.Console.WriteLine($"Hello {user} to ChatClient!"); ConfigurationBootstraper.InitUser(user); try { using (_connection = NatsConnectionFactory.ConnectToNats(string.IsNullOrWhiteSpace(ConfigurationBootstraper.AppConfig.NATSServerUrl) ? Defaults.Url : ConfigurationBootstraper.AppConfig.NATSServerUrl)) { // Initializes publisher and subscriber _publisher = new Publisher(_connection, ConfigurationBootstraper.AppConfig.NATSSubject); _subscriber = new Subscriber(_connection, ConfigurationBootstraper.AppConfig.NATSSubject); // Subscribe to the Subject SubscribeOnSubject(); System.Console.WriteLine("Continuous pub/sub chat"); System.Console.WriteLine("======================="); System.Console.WriteLine("Start chatting. Enter 'stop' to stopping the ChatClient."); while (isChattingContinue) { // Continous publish PublishOnSubject(); } // Unsubscribe the client _subscriber?.UnSubscribe(); System.Console.WriteLine("ChatClient stopped."); } } catch (Exception ex) { System.Console.WriteLine($"Unhandled exception occurred: {ex.Message}"); } }
/// <summary> /// Configures the services. This method gets called by the runtime. Use this method to add services to the container. /// </summary> /// <param name="services">The services.</param> public void ConfigureServices(IServiceCollection services) { services.AddControllers(); services.AddSwaggerGen(c => { c.SwaggerDoc("v1", new OpenApiInfo { Title = "ChatClient.Api", Version = "v1" }); }); services.AddAuthentication(IISDefaults.AuthenticationScheme); services.AddAuthorization(); services.AddHttpContextAccessor(); var connection = NatsConnectionFactory.ConnectToNats(NatsServerUrl); var publisher = new Publisher(connection, NatsSubject); var subscriber = new Subscriber(connection, NatsSubject); subscriber.Subscribe(); services.AddSingleton <IPublisher>(publisher); services.AddSingleton <ISubscriber>(subscriber); }