Ejemplo n.º 1
0
        public static void ClientCode()
#endif
        {
            DefaultWampChannelFactory channelFactory = new DefaultWampChannelFactory();

            IWampClientAuthenticator authenticator;

            if (false)
            {
                authenticator = new WampCraClientAuthenticator(authenticationId: "joe", secret: "secret2");
            }
            else
            {
                authenticator =
                    new WampCraClientAuthenticator(authenticationId: "peter", secret: "secret1");
            }

            IWampChannel channel =
                channelFactory.ConnectToRealm("realm1")
                .WebSocketTransport("ws://127.0.0.1:8080/ws")
                .JsonSerialization()
                .CraAuthentication("peter", "secret1")
                .Build();

            channel.RealmProxy.Monitor.ConnectionEstablished +=
                (sender, args) =>
            {
                Console.WriteLine("connected session with ID " + args.SessionId);

                dynamic details = args.WelcomeDetails.OriginalValue.Deserialize <dynamic>();

                Console.WriteLine("authenticated using method '{0}' and provider '{1}'", details.authmethod,
                                  details.authprovider);

                Console.WriteLine("authenticated with authid '{0}' and authrole '{1}'", details.authid,
                                  details.authrole);
            };

            channel.RealmProxy.Monitor.ConnectionBroken += (sender, args) =>
            {
                dynamic details = args.Details.OriginalValue.Deserialize <dynamic>();
                Console.WriteLine("disconnected " + args.Reason + " " + details.reason + details);
            };

            IWampRealmProxy realmProxy = channel.RealmProxy;

#if NET45
            await channel.Open().ConfigureAwait(false);
#else
            channel.Open().Wait();
#endif
            // call a procedure we are allowed to call (so this should succeed)
            //
            IAdd2Service proxy = realmProxy.Services.GetCalleeProxy <IAdd2Service>();

            try
            {
#if NET45
                var five = await proxy.Add2Async(2, 3)
                           .ConfigureAwait(false);
#else
                var five = proxy.Add2Async(2, 3).Result;
#endif
                Console.WriteLine("call result {0}", five);
            }
            catch (Exception e)
            {
                Console.WriteLine("call error {0}", e);
            }

            // (try to) register a procedure where we are not allowed to (so this should fail)
            //
            Mul2Service service = new Mul2Service();

            try
            {
#if NET45
                await realmProxy.Services.RegisterCallee(service)
                .ConfigureAwait(false);
#else
                realmProxy.Services.RegisterCallee(service)
                .Wait();
#endif

                Console.WriteLine("huh, function registered!");
            }
#if NET45
            catch (WampException ex)
            {
                Console.WriteLine("registration failed - this is expected: " + ex.ErrorUri);
            }
#else
            catch (AggregateException ex)
            {
                WampException innerException = ex.InnerException as WampException;
                Console.WriteLine("registration failed - this is expected: " + innerException.ErrorUri);
            }
#endif

            // (try to) publish to some topics
            //
            string[] topics =
            {
                "com.example.topic1",
                "com.example.topic2",
                "com.foobar.topic1",
                "com.foobar.topic2"
            };


            foreach (string topic in topics)
            {
                IWampTopicProxy topicProxy = realmProxy.TopicContainer.GetTopicByUri(topic);

                try
                {
#if NET45
                    await topicProxy.Publish(new PublishOptions()
                    {
                        Acknowledge = true
                    },
                                             new object[] { "hello" })
                    .ConfigureAwait(false);
#else
                    topicProxy.Publish(new PublishOptions()
                    {
                        Acknowledge = true
                    },
                                       new object[] { "hello" })
                    .Wait();
#endif
                    Console.WriteLine("event published to topic " + topic);
                }
#if NET45
                catch (WampException ex)
                {
                    Console.WriteLine("publication to topic " + topic + " failed: " + ex.ErrorUri);
                }
#else
                catch (AggregateException ex)
                {
                    WampException innerException = ex.InnerException as WampException;
                    Console.WriteLine("publication to topic " + topic + " failed: " + innerException.ErrorUri);
                }
#endif
            }
        }
Ejemplo n.º 2
0
        static async Task Main(string[] arguments)
        {
            DefaultWampChannelFactory channelFactory = new DefaultWampChannelFactory();

            IWampChannel channel =
                channelFactory.ConnectToRealm("realm1")
                .WebSocketTransport(new Uri("ws://127.0.0.1:8080/ws"))
                .JsonSerialization()
                .CraAuthentication("peter", "secret1")
                //.CraAuthentication("joe", "secret2")
                .Build();

            channel.RealmProxy.Monitor.ConnectionEstablished +=
                (sender, args) =>
            {
                Console.WriteLine("connected session with ID " + args.SessionId);

                WelcomeDetails details = args.WelcomeDetails;

                Console.WriteLine($"authenticated using method '{details.AuthenticationMethod}' and provider '{details.AuthenticationProvider}'");

                Console.WriteLine($"authenticated with authid '{details.AuthenticationId}' and authrole '{details.AuthenticationRole}'");
            };

            channel.RealmProxy.Monitor.ConnectionBroken += (sender, args) =>
            {
                GoodbyeAbortDetails details = args.Details;
                Console.WriteLine($"disconnected {args.Reason} {details.Message}");
            };

            IWampRealmProxy realmProxy = channel.RealmProxy;

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

            // call a procedure we are allowed to call (so this should succeed)
            //
            IAdd2Service proxy = realmProxy.Services.GetCalleeProxy <IAdd2Service>();

            try
            {
                var five = await proxy.Add2Async(2, 3);

                Console.WriteLine("call result {0}", five);
            }
            catch (Exception e)
            {
                Console.WriteLine("call error {0}", e);
            }

            // (try to) register a procedure where we are not allowed to (so this should fail)
            //
            Mul2Service service = new Mul2Service();

            try
            {
                await realmProxy.Services.RegisterCallee(service)
                .ConfigureAwait(false);

                Console.WriteLine("huh, function registered!");
            }
            catch (WampException ex)
            {
                Console.WriteLine("registration failed - this is expected: " + ex.ErrorUri);
            }

            // (try to) publish to some topics
            //
            string[] topics =
            {
                "com.example.topic1",
                "com.example.topic2",
                "com.foobar.topic1",
                "com.foobar.topic2"
            };

            foreach (string topic in topics)
            {
                IWampTopicProxy topicProxy = realmProxy.TopicContainer.GetTopicByUri(topic);

                try
                {
                    await topicProxy.Publish(new PublishOptions()
                    {
                        Acknowledge = true
                    },
                                             new object[] { "hello" })
                    .ConfigureAwait(false);

                    Console.WriteLine("event published to topic " + topic);
                }
                catch (WampException ex)
                {
                    Console.WriteLine("publication to topic " + topic + " failed: " + ex.ErrorUri);
                }
            }

            Console.ReadLine();
        }