示例#1
0
        public void Init(string wsuri, string realm)
        {
            _factory = new DefaultWampChannelFactory();

            _channel = _factory.CreateJsonChannel(wsuri, realm);

            try
            {
                _channel.Open().Wait();

                services = _channel.RealmProxy.Services;

                // register procedures for remote calling
                services.RegisterCallee(this).Wait();

                // publishing
                onPublishSystemStatusSubject = services.GetSubject <RPiHomeSecurityStatus>("com.rpihomesecurity.onstatus");

                monitor = _channel.RealmProxy.Monitor;
                monitor.ConnectionBroken += OnClose;
                monitor.ConnectionError  += OnError;

                IsConnected = true;
                Console.WriteLine("WAMP connection success");
            }
            catch (Exception e)
            {
                Console.WriteLine("Failure to Initialise WAMP connection. Is crossbar started?" + e.Message);
            }
        }
        public async Task <bool> Run(SubContent <TMensagem> mensagem)
        {
            bool result = false;

            IWampClientConnectionMonitor monitor = this._channel.RealmProxy.Monitor;

            if (string.IsNullOrEmpty(mensagem.NomeFila))
            {
                throw new Exception("Informe um tópico!!");
            }

            monitor.ConnectionBroken += OnClose;
            monitor.ConnectionError  += OnError;

            IWampRealmServiceProvider services = this._channel.RealmProxy.Services;

            ISubject <TMensagem> helloSubject =
                services.GetSubject <TMensagem>(mensagem.NomeFila);

            IDisposable subscription = helloSubject.Subscribe <TMensagem>(x => { mensagem.MessageListener(x); });

            result = true;

            return(result);
        }
示例#3
0
        private void Publish <T>(string topic, T data)
        {
            var subject = services.GetSubject <T>(topic);

            subject.OnNext(data);
        }
        private async static Task Run(string wsuri, string realm)
        {
            Console.WriteLine("Connecting to {0}, realm {1}", wsuri, realm);

            DefaultWampChannelFactory factory = new DefaultWampChannelFactory();

            IWampChannel channel =
                factory.CreateJsonChannel(wsuri, realm);

            IWampClientConnectionMonitor monitor = channel.RealmProxy.Monitor;

            monitor.ConnectionBroken += OnClose;
            monitor.ConnectionError  += OnError;

            await channel.Open().ConfigureAwait(false);

            IWampRealmServiceProvider services = channel.RealmProxy.Services;

            // SUBSCRIBE to a topic and receive events
            ISubject <string> helloSubject =
                services.GetSubject <string>("com.example.onhello");

            IDisposable subscription =
                helloSubject.Subscribe
                    (x => Console.WriteLine("event for 'onhello' received: {0}", x));

            Console.WriteLine("subscribed to topic 'onhello'");


            // REGISTER a procedure for remote calling
            Add2Service callee = new Add2Service();

            await services.RegisterCallee(callee)
            .ConfigureAwait(false);

            Console.WriteLine("procedure add2() registered");


            // PUBLISH and CALL every second... forever
            ISubject <int> onCounterSubject =
                services.GetSubject <int>("com.example.oncounter");

            IMul2Service proxy =
                services.GetCalleeProxy <IMul2Service>();

            int counter = 0;

            while (true)
            {
                // PUBLISH an event
                onCounterSubject.OnNext(counter);
                Console.WriteLine("published to 'oncounter' with counter {0}", counter);
                counter++;


                // CALL a remote procedure
                try
                {
                    int result = await proxy.Multiply(counter, 3)
                                 .ConfigureAwait(false);

                    Console.WriteLine("mul2() called with result: {0}", result);
                }
                catch (WampException ex)
                {
                    if (ex.ErrorUri != "wamp.error.no_such_procedure")
                    {
                        Console.WriteLine("call of mul2() failed: " + ex);
                    }
                }


                await Task.Delay(TimeSpan.FromSeconds(1))
                .ConfigureAwait(false);
            }
        }