Exemplo n.º 1
0
        public async Task ChannelSubscribe()
        {
            var realtime = new AblyRealtime(new ClientOptions(PlaceholderKey)
            {
                AutoConnect = false
            });
            IRealtimeChannel channel = realtime.Channels.Get("test");

            // Or ...
            IRealtimeChannel channel2 = realtime.Channels.Get("shortcut");

            channel.Subscribe(message =>
            {
                var name = message.Name;
                var data = message.Data;
            });

            channel.On(args =>
            {
                var state = args.Current;    // Current channel State
                var error = args.Error;      // If the channel error-ed it will be reflected here
            });

            channel.On(ChannelEvent.Attached, args =>
            {
                // Do stuff when channel is attached
            });

            channel.Publish("greeting", "Hello World!");
            channel.Publish("greeting", "Hello World!", (success, error) =>
            {
                // If 'Publish' succeeded 'success' is 'true'
                // If 'Publish' failed 'success' is 'false' and 'error' will contain the specific error
            });

            var result = await channel.PublishAsync("greeting", "Hello World!");

            // You can check if the message failed
            if (result.IsFailure)
            {
                var error = result.Error;    // The error reason can be accessed as well
            }

            var secret           = Crypto.GenerateRandomKey();
            var encryptedChannel = realtime.Channels.Get("encrypted", new ChannelOptions(secret));

            encryptedChannel.Subscribe(message =>
            {
                var data = message.Data;    // Sensitive data (encrypted before published)
            });

            encryptedChannel.Publish("name (not encrypted)", "sensitive data (encrypted before published)");
        }
Exemplo n.º 2
0
            public async Task ShouldEmitTheFollowingStates(ChannelState state)
            {
                ChannelState newState = ChannelState.Initialized;

                _channel.On(x =>
                {
                    newState = x.Current;
                    Done();
                });

                (_channel as RealtimeChannel).SetChannelState(state);

                WaitOne();

                _channel.State.Should().Be(state);
                newState.Should().Be(state);
            }
Exemplo n.º 3
0
            public void WhenInitializedDetachedOrDetaching_ShouldDoNothing(ChannelState state)
            {
                SetState(state);
                bool changed = false;

                _channel.On((args) =>
                {
                    changed = true;
                });

                _channel.Detach();

                changed.Should().BeFalse();
            }
Exemplo n.º 4
0
            public async Task WhenAttachingOrAttached_ShouldDoNothing(ChannelState state)
            {
                await SetState(state);

                bool         stateChanged = false;
                ChannelState newState     = ChannelState.Initialized;

                _channel.On((args) =>
                {
                    newState     = args.Current;
                    stateChanged = true;
                });

                _channel.Attach();

                await Task.Delay(100);

                stateChanged.Should().BeFalse("This should not happen. State changed to: " + newState);
            }