コード例 #1
0
        public void TestSendingActorMessageToHybridConnection()
        {
            string relayName            = "TestSendingActorMessageToHybridConnection";
            string sendMessage          = "Hello";
            var    actorSendMessage     = new PingPong.Ping(sendMessage);
            var    actorResponseMessage = new PingPong.Pong();

            CreateHybridConnection(relayName);
            Telegraph.Instance.Register(typeof(Exception), FailOnException);
            HybridConnectionListener listener = null;

            try
            {
                listener = CreateHybridListener(relayName, actorResponseMessage);
                var relayConnection = new Telegraphy.Azure.Relay.Hybrid.RecieveResponseFromRequest <PingPong.Ping, PingPong.Pong>(Connections.HybridRelayConnectionString, relayName);

                IActorMessageSerializationActor   serializer   = new IActorMessageSerializationActor();
                IActorMessageDeserializationActor deserializer = new IActorMessageDeserializationActor();
                deserializer.Register <PingPong.Pong>((object msg) => (PingPong.Pong)msg);

                Telegraph.Instance.Register <SerializeMessage <PingPong.Ping>, IActorMessageSerializationActor>(() => serializer);
                Telegraph.Instance.Register <DeserializeMessage <PingPong.Pong>, IActorMessageDeserializationActor>(() => deserializer);

                Telegraph.Instance.Register <PingPong.Ping, Telegraphy.Azure.Relay.Hybrid.RecieveResponseFromRequest <PingPong.Ping, PingPong.Pong> >(() => relayConnection);
                var  resultTask = Telegraph.Instance.Ask(actorSendMessage);
                bool success    = resultTask.Wait(TimeSpan.FromSeconds(10));
                Assert.IsTrue(success);

                if (success)
                {
                    Assert.IsTrue(resultTask.Result is PingPong.Pong);
                }
            }
            finally
            {
                Telegraph.Instance.UnRegisterAll();
                try { listener?.CloseAsync().Wait(); } catch (Exception) { }
                DeleteRelay(relayName);
            }
        }
コード例 #2
0
        public HybridConnectionListener CreateHybridListener(string hybridConnectionName, PingPong.Pong response)
        {
            RelayConnectionStringBuilder connectionStringBuilder = new RelayConnectionStringBuilder(Connections.HybridRelayConnectionString)
            {
                EntityPath = hybridConnectionName
            };
            //var tokenProvider = TokenProvider.CreateSharedAccessSignatureTokenProvider(connectionStringBuilder.SharedAccessKeyName, connectionStringBuilder.SharedAccessKey);
            //var uri = new Uri(string.Format("https://{0}/{1}", connectionStringBuilder.Endpoint.Host, hybridConnectionName));
            var listener = new HybridConnectionListener(connectionStringBuilder.ToString());

            SerializeMessage <IActorMessage> sMsg       = new SerializeMessage <IActorMessage>(response);
            IActorMessageSerializationActor  serializer = new IActorMessageSerializationActor();

            serializer.OnMessageRecieved(sMsg);
            byte[] responseBytes = (byte[])sMsg.ProcessingResult;

            // Subscribe to the status events.
            listener.Connecting += (o, e) => { Console.WriteLine("Connecting"); };
            listener.Offline    += (o, e) => { Console.WriteLine("Offline"); };
            listener.Online     += (o, e) => { Console.WriteLine("Online"); };

            // Provide an HTTP request handler
            listener.RequestHandler = (context) =>
            {
                // Do something with context.Request.Url, HttpMethod, Headers, InputStream...
                context.Response.StatusCode        = HttpStatusCode.OK;
                context.Response.StatusDescription = "OK";
                context.Response.OutputStream.Write(responseBytes, 0, responseBytes.Length);

                // The context MUST be closed here
                context.Response.Close();
            };

            listener.OpenAsync().Wait();
            Console.WriteLine("Server listening");
            return(listener);
        }