Ejemplo n.º 1
0
        /// <summary>
        /// 启动订阅客户端
        /// </summary>
        public void StartDealerClient()
        {
            Task task = new Task(() =>
            {
                // 创建推送消息的Socket
                using (var client = new ZeroMQ.ZSocket(ZeroMQ.ZSocketType.DEALER))
                {
                    client.Identity = Encoding.UTF8.GetBytes("Client1");
                    client.Connect("tcp://127.0.0.1:1003");
                    while (true)
                    {
                        if (isClosing)
                        {
                            using (var content = new ZeroMQ.ZFrame("Quit"))
                            {
                                client.SendFrame(content);
                            }
                            break;
                        }
                        using (var content = new ZeroMQ.ZFrame("Task " + DateTime.Now.TimeOfDay.ToString()))
                        {
                            client.SendFrame(content);
                        }
                        Thread.Sleep(10000);
                    }
                    Console.WriteLine("DealerClient is Closed.");
                }
            });

            task.Start();
        }
Ejemplo n.º 2
0
        /// <summary>
        /// 启动消息推送客户端
        /// </summary>
        public void StartPushClient()
        {
            Task task = new Task(() =>
            {
                // 创建推送消息的Socket
                using (var client = new ZeroMQ.ZSocket(ZeroMQ.ZSocketType.PUSH))
                {
                    client.Connect("tcp://127.0.0.1:1002");
                    while (true)
                    {
                        if (isClosing)
                        {
                            using (var content = new ZeroMQ.ZFrame("Quit"))
                            {
                                client.SendFrame(content);
                            }
                            break;
                        }
                        using (var content = new ZeroMQ.ZFrame("Push->" + DateTime.Now.TimeOfDay.ToString()))
                        {
                            client.SendFrame(content);
                        }
                        Thread.Sleep(2000);
                    }
                    Console.WriteLine("PushClient is Closed.");
                }
            });

            task.Start();
        }
Ejemplo n.º 3
0
        /// <summary>
        /// 启动订阅客户端
        /// </summary>
        public void StartDealerWorker(string id)
        {
            Task task = new Task(() =>
            {
                using (var worker = new ZeroMQ.ZSocket(ZeroMQ.ZSocketType.DEALER))
                {
                    worker.Identity = Encoding.UTF8.GetBytes(id);
                    worker.Connect("tcp://127.0.0.1:1004");
                    using (var content = new ZeroMQ.ZFrame("hello"))
                    {
                        worker.SendFrame(content);
                    }
                    while (true)
                    {
                        if (isClosing)
                        {
                            using (var quit = new ZeroMQ.ZFrame("Quit"))
                            {
                                worker.SendFrame(quit);
                            }
                            break;
                        }
                        using (var msg = worker.ReceiveMessage())
                        {
                            var content = msg[0].ReadString();
                            Console.WriteLine("Begin Working->" + content);
                            using (var info = new ZeroMQ.ZFrame("i am working..."))
                            {
                                worker.SendFrame(info);
                            }
                        }
                    }
                    Console.WriteLine("BackendClient is Closed.");
                }
            });

            task.Start();
        }
Ejemplo n.º 4
0
        /// <summary>
        /// 启动订阅客户端
        /// </summary>
        public void StartSubClient()
        {
            Task task = new Task(() =>
            {
                // 创建发布服务的Socket
                using (var client = new ZeroMQ.ZSocket(ZeroMQ.ZSocketType.SUB))
                {
                    client.Connect("tcp://127.0.0.1:1001");
                    client.Subscribe("Quit");
                    client.Subscribe("Time"); // 订阅内容标识
                    while (true)
                    {
                        if (isClosing)
                        {
                            break;
                        }
                        using (var msg = client.ReceiveMessage())
                        {
                            if (msg.Count == 1)
                            {
                                break;
                            }
                            if (msg.Count == 2)
                            {
                                var title   = msg[0].ReadString();  // ReadString只能调用一次,多次调用会取不到数据
                                var content = msg[1].ReadString();  // 如需多次使用该消息,像这样存放到临时变量中
                                Console.WriteLine("{0}->{1}", title, content);
                            }
                        }
                    }
                    Console.WriteLine("SubClient is Closed.");
                }
            });

            task.Start();
        }