예제 #1
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) =>
                    {
                        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();
                }
        }
        public void RequestReply()
        {
            using (WebSocket4Net.WebSocket webSocket = new WebSocket("ws://localhost:82", "WSNetMQ"))
            {
                using (WSRouter router = new WSRouter())
                {
                    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.ReceiveFrameBytes();
                    string msg      = router.ReceiveFrameString();

                    Assert.AreEqual("H", msg);

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

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

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

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

                    Assert.AreEqual(0, receivedMessage[0]);
                    Assert.AreEqual('W', receivedMessage[1]);
                }
            }
        }