Beispiel #1
0
        public void PingPong()
        {
            using (NetMQContext context = NetMQContext.Create())
            {
                using (WebSocket4Net.WebSocket webSocket = new WebSocket("ws://localhost:82", "WSNetMQ"))
                {
                    webSocket.EnableAutoSendPing   = true;
                    webSocket.AutoSendPingInterval = 1; // one second

                    using (WSRouter router = context.CreateWSRouter())
                    {
                        router.Bind("ws://localhost:82");

                        ManualResetEvent manualResetEvent = new ManualResetEvent(false);
                        webSocket.Opened += (sender, args) => manualResetEvent.Set();

                        webSocket.Open();
                        webSocket.Error += (sender, args) => Console.WriteLine("Error");
                        manualResetEvent.WaitOne();

                        Thread.Sleep(5000);

                        Assert.AreEqual(webSocket.State, WebSocketState.Open);
                    }
                }
            }
        }
Beispiel #2
0
        static void Main(string[] args)
        {
            using (NetMQContext context = NetMQContext.Create())
            {
                using (WSRouter router = context.CreateWSRouter())
                    using (WSPublisher publisher = context.CreateWSPublisher())
                    {
                        router.Bind("ws://localhost:80");
                        publisher.Bind("ws://localhost:81");

                        router.ReceiveReady += (sender, eventArgs) =>
                        {
                            string identity = router.ReceiveString();
                            string message  = router.ReceiveString();

                            router.SendMore(identity).Send("OK");

                            publisher.SendMore("chat").Send(message);
                        };

                        Poller poller = new Poller();
                        poller.AddSocket(router);

                        // we must add the publisher to the poller although we are not registering to any event.
                        // The internal stream socket handle connections and subscriptions and use the events internally
                        poller.AddSocket(publisher);
                        poller.Start();
                    }
            }
        }
        static void Main(string[] args)
        {
            using (WSRouter router = new WSRouter())
                using (WSPublisher publisher = new WSPublisher())
                {
                    router.Bind("ws://localhost:80");
                    publisher.Bind("ws://localhost:81");

                    router.ReceiveReady += (sender, eventArgs) =>
                    {
                        byte[] identity = router.ReceiveFrameBytes();
                        string message  = router.ReceiveFrameString();

                        router.SendMoreFrame(identity);
                        router.SendFrame("OK");

                        publisher.SendMoreFrame("chat");
                        publisher.SendFrame(message);
                    };

                    NetMQPoller poller = new NetMQPoller();
                    poller.Add(router);

                    // we must add the publisher to the poller although we are not registering to any event.
                    // The internal stream socket handle connections and subscriptions and use the events internally
                    poller.Add(publisher);
                    poller.Run();
                }
        }
Beispiel #4
0
        static void Main(string[] args)
        {
            using (var router = new WSRouter())
            {
                using (var publisher = new WSPublisher())
                {
                    router.Bind("ws://localhost:80");
                    publisher.Bind("ws://localhost:81");

                    router.ReceiveReady += (sender, eventArgs) =>
                    {
                        var identity = eventArgs.WSSocket.ReceiveFrameBytes();

                        using (var datasendercontext = new DataSenderContext())
                        {
                            datasendercontext.Start(eventArgs, publisher, new Identity()
                            {
                                Receiver = identity
                            });
                        }
                    };

                    var poller = new NetMQPoller();
                    poller.Add(router);
                    poller.Run();
                }
            }
        }
        private static void Main(string[] args)
        {
            var deciSecond = new TimeSpan(10000);

            using (var subscriber = new SubscriberSocket()) // For receiving updates from presentation host
                using (var publisher = new WSPublisher())   // For publishing updates from presentation host to audience
                    using (var responder = new WSRouter())  // Handling on-demand requests for late-joining or crashing clients
                    {
                        subscriber.Bind("tcp://*:3000");
                        subscriber.SubscribeToAnyTopic();
                        publisher.Bind("ws://*:3001");
                        responder.Bind("ws://*:3002");

                        byte step = 0;
                        subscriber.ReceiveReady += (_, __) => {
                            if (!subscriber.TryReceiveFrameBytes(deciSecond, out var received))
                            {
                                return;
                            }
                            step = received[0];
                            Console.Out.WriteLine("Sending " + step + " to audience.");
                            publisher.TrySendFrame(deciSecond, new[] { step });
                        };
                        responder.ReceiveReady += (_, __) => {
                            NetMQMessage msg = null;
                            if (!responder.TryReceiveMultipartMessage(deciSecond, ref msg))
                            {
                                return;
                            }
                            var identity = msg.Pop().Buffer;
                            var request  = msg.Pop().ConvertToString();
                            msg.Clear();
                            if (request == "Which slide are we on?")
                            {
                                responder.TrySendMultipartBytes(deciSecond, identity, new[] { step });
                            }
                            else
                            {
                                if (!responder.TrySendFrame(deciSecond, identity, true))
                                {
                                    return;
                                }
                                responder.TrySendFrameEmpty(deciSecond);
                            }
                        };
                        new NetMQPoller {
                            subscriber, responder
                        }.Run();                               // Polling both subscriber and router sockets.
                    }
        }
Beispiel #6
0
        public void RequestReply()
        {
            using (NetMQContext context = NetMQContext.Create())
            {
                using (WebSocket4Net.WebSocket webSocket = new WebSocket("ws://localhost:82", "WSNetMQ"))
                {
                    using (WSRouter router = context.CreateWSRouter())
                    {
                        router.Bind("ws://localhost:82");

                        ManualResetEvent manualResetEvent = new ManualResetEvent(false);
                        webSocket.Opened += (sender, args) => manualResetEvent.Set();

                        webSocket.Open();
                        webSocket.Error += (sender, args) => Console.WriteLine("Error");
                        manualResetEvent.WaitOne();

                        Assert.AreEqual(webSocket.State, WebSocketState.Open);

                        byte[] message = new byte[2];
                        message[0] = 0;
                        message[1] = (byte)'H';

                        // should exit the router thread
                        webSocket.Send(message, 0, message.Length);

                        byte[] identity = router.Receive();
                        string msg      = router.ReceiveString();

                        Assert.AreEqual("H", msg);

                        byte[] receivedMessage = null;
                        manualResetEvent.Reset();

                        webSocket.DataReceived += (sender, args) =>
                        {
                            receivedMessage = args.Data;
                            manualResetEvent.Set();
                        };

                        router.SendMore(identity).Send("W");

                        Assert.IsTrue(manualResetEvent.WaitOne(1000));

                        Assert.AreEqual(0, receivedMessage[0]);
                        Assert.AreEqual('W', receivedMessage[1]);
                    }
                }
            }
        }
Beispiel #7
0
        static void Main(string[] args)
        {
            using (WSRouter router = new WSRouter())
                using (WSPublisher publisher = new WSPublisher())
                {
                    router.Bind("ws://localhost:80");
                    publisher.Bind("ws://localhost:81");

                    router.ReceiveReady += (sender, eventArgs) =>
                    {
                        var    identity = eventArgs.WSSocket.ReceiveFrameBytes();
                        string message  = eventArgs.WSSocket.ReceiveFrameString();

                        eventArgs.WSSocket.SendMoreFrame(identity).SendFrame("OK");

                        publisher.SendMoreFrame("chat").SendFrame(message);
                    };

                    NetMQPoller poller = new NetMQPoller();
                    poller.Add(router);

                    poller.Run();
                }
        }