示例#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);
            }
        }
示例#2
0
        private async static Task Run(string wsuri, string realm)
        {
            Console.WriteLine("Connecting to {0}, realm {1}", wsuri, realm);

            WampChannelFactory factory = new WampChannelFactory();

            IWampChannel channel =
                factory.ConnectToRealm(realm)
                .WebSocketTransport(wsuri)
                .JsonSerialization()
                .Build();

            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
            HelloSubscriber subscriber = new HelloSubscriber();

            IAsyncDisposable subscriptionDisposable =
                await services.RegisterSubscriber(subscriber)
                .ConfigureAwait(false);

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

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

            IAsyncDisposable registrationDisposable =
                await services.RegisterCallee(callee)
                .ConfigureAwait(false);

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


            // PUBLISH and CALL every second... forever
            CounterPublisher publisher =
                new CounterPublisher();

            IDisposable publisherDisposable =
                channel.RealmProxy.Services.RegisterPublisher(publisher);

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

            int counter = 0;

            while (true)
            {
                // PUBLISH an event
                publisher.Publish(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);
            }
        }
        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);
            }
        }