Ejemplo n.º 1
0
        static async Task Main(string[] args)
        {
            var client = new BlipClientBuilder()
                         .UsingAccessKey("", "")
                         .Build();

            Trace.Listeners.Add(new TextWriterTraceListener(Console.Out));

            var listener = new BlipChannelListener(client, true, Log.Logger);

            listener.AddMessageReceiver(
                new LambdaMessageReceiver(async(message, cancellationToken) =>
            {
                Console.WriteLine("Message '{0}' received from '{1}': {2}", message.Id, message.From, message.Content);
                await client.SendMessageAsync($"You said '{message.Content}'.", message.From, cancellationToken);
            }),
                m => Task.FromResult(m.Type == MediaType.TextPlain));

            listener.AddNotificationReceiver(
                new LambdaNotificationReceiver((notification, cancellationToken) =>
            {
                Console.WriteLine("Notification '{0}' received from '{1}': {2}", notification.Id, notification.From, notification.Event);
                return(Task.CompletedTask);
            }));

            listener.AddCommandReceiver(
                new LambdaCommandReceiver((command, cancellationToken) =>
            {
                Console.WriteLine("Command '{0}' received from '{1}': {2} {3}", command.Id, command.From, command.Method, command.Uri);
                return(Task.CompletedTask);
            }));

            using (var cts = new CancellationTokenSource(TimeSpan.FromSeconds(30)))
            {
                await client.StartAsync(listener, cts.Token);
            }

            Console.WriteLine("Client started. Press enter to stop.");
            Console.ReadLine();

            using (var cts = new CancellationTokenSource(TimeSpan.FromSeconds(30)))
            {
                await client.StopAsync(cts.Token);
            }

            Console.WriteLine("Client stopped. Press enter to exit.");
            Console.ReadLine();
        }
Ejemplo n.º 2
0
        public static async Task <IBlipClient> BuildClientAsync(
            Application application,
            Func <IBlipClient> builder,
            IServiceContainer serviceContainer,
            ITypeResolver typeResolver,
            CancellationToken cancellationToken,
            Action <IServiceContainer> serviceOverrides = null,
            ILogger logger = null)
        {
            RegisterSettingsContainer(application, serviceContainer, typeResolver);
            RegisterSettingsTypes(application, serviceContainer, typeResolver);
            RegisterStateManager(application, serviceContainer, typeResolver);

            serviceContainer.RegisterExtensions();
            serviceContainer.RegisterService(typeof(IServiceProvider), serviceContainer);
            serviceContainer.RegisterService(typeof(IServiceContainer), serviceContainer);
            serviceContainer.RegisterService(typeof(Application), application);
            serviceContainer.RegisterService(typeof(ISessionManager), () => new SessionManager(serviceContainer.GetService <IBucketExtension>()));

            if (logger != null)
            {
                serviceContainer.RegisterService(typeof(ILogger), logger);
            }

            var client = builder();

            serviceContainer.RegisterService(typeof(ISender), client);
            serviceOverrides?.Invoke(serviceContainer);

            var stateManager   = serviceContainer.GetService <IStateManager>();
            var sessionManager = serviceContainer.GetService <ISessionManager>();

            if (application.RegisterTunnelReceivers)
            {
                RegisterTunnelReceivers(application);
            }

            var channelListener = new BlipChannelListener(client, !application.DisableNotify, logger);

            await AddMessageReceivers(application, serviceContainer, client, channelListener, typeResolver, stateManager, sessionManager);
            await AddNotificationReceivers(application, serviceContainer, client, channelListener, typeResolver, stateManager, sessionManager);
            await AddCommandReceivers(application, serviceContainer, channelListener, typeResolver);

            await client.StartAsync(channelListener, cancellationToken).ConfigureAwait(false);

            return(client);
        }