C # Nanomsg subscriptions to the publication

Nanomsg Profile

nanomsg is a socket library, offers several common communication modes currently available and scalable agreement:

PAIR one-

BUS-many

REQREP allow the construction of a stateless service cluster to handle user requests

PUBSUB the message to subscribe messaging user

PIPELINE summary information from multiple sources, and load balancing between the destination point.

SURVEY allow the state to query multiple applications

Nanomsg subscriptions to the publication using the example

C # Nanomsg very easy to use, only need to look inside NuGet Nanomsg, select the appropriate version can be installed, where choice is NNanomsg,

version 0.5.2. It's easy to

need to publish information server, client subscription information.

Inter-process communication nanomsg use, server-side only need to bind to an address, then PublishSocket send data on it,

and subscribe to one or more client-side topic by SubscribeSocket, then connect mailing address, Receive can receive data through.

Nanomsg are connected together and a message topic, only the front part and the client topic subscription agreement on the entire message can receive data.

The actual code is as follows:

Server side:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using NNanomsg.Protocols;

namespace WPFNanoMsgServer
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        PublishSocket serversocket = new PublishSocket();
        string address;

        public MainWindow()
        {
            InitializeComponent();
        }

        private void button_Click(object sender, RoutedEventArgs e)
        {
            //获取参数并启动
            serversocket.Bind(textBox_address.Text);
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            string topic = textBox_topic.Text;
            string content = textBox_content.Text;
            string message = topic + "|" + content;
            byte[] b = Encoding.UTF8.GetBytes(message);
            serversocket.Send(b);
            if(textBlock_log.Text.Count()>5000)
            {
                textBlock_log.Text = "";
            }
            textBlock_log.Text ="publish msg:"+ message + "\n" + textBlock_log.Text;
        }
    }
}

UI xaml corresponding to the content as follows:

<Window x:Class="WPFNanoMsgServer.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WPFNanoMsgServer"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <Button x:Name="button" Content="启动" HorizontalAlignment="Left" Height="26" Margin="429,20,0,0" VerticalAlignment="Top" Width="56" Click="button_Click"/>
        <TextBox x:Name="textBox_address" HorizontalAlignment="Left" Height="26" Margin="47,20,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="82"/>
        <TextBlock x:Name="textBlock_log" HorizontalAlignment="Left" Height="208" Margin="35,90,0,0" TextWrapping="Wrap" Text="TextBlock" VerticalAlignment="Top" Width="229"/>
        <Label x:Name="label" Content="log" HorizontalAlignment="Left" Height="25" Margin="35,60,0,0" VerticalAlignment="Top" Width="66"/>

        <Button x:Name="button1" Content="发布" HorizontalAlignment="Left" Height="26" Margin="320,105,0,0" VerticalAlignment="Top" Width="58" Click="button1_Click"/>
        <TextBox x:Name="textBox_topic" HorizontalAlignment="Left" Height="27" Margin="309,131,0,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="169"/>
        <TextBox x:Name="textBox_content" HorizontalAlignment="Left" Height="102" Margin="309,170,0,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="169"/>

    </Grid>
</Window>

client-side code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Threading;
using NNanomsg.Protocols;

namespace WPFNanoMsgClient
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        DispatcherTimer timer;
        static string log = "";
        void UpdateLog(object sender, EventArgs e)
        {
            textBlock_log.Text = log;
        }
        void StartTimer()
        {
            timer = new DispatcherTimer();
            timer.Interval = TimeSpan.FromMilliseconds(200);
            timer.Tick += UpdateLog;
            timer.Start();
        }
        public MainWindow()
        {
            InitializeComponent();
            StartTimer();
        }

        bool runFlag = false;
        static string topicstr;
        static string serveraddress;

        public static void DealReceive()
        {
           
        }
        static  void DealReceivedata()
        {
            //await Task.Run({
            //});
            using (var s = new SubscribeSocket())
            {
                //Needs to match the first portion of the message being received.

                //需要订阅多个数据
                //string topicstr = textBox_topic.Text;
                string[] topics = topicstr.Split('#');
                foreach (var singletopin in topics)
                {
                    s.Subscribe(singletopin);
                }


                s.Connect(serveraddress);
                while (true)
                {
                    byte[] b = s.Receive();
                    if (b != null)
                    {
                        Console.WriteLine("Received: " + Encoding.ASCII.GetString(b));
                        if (log.Count() > 500)
                        {
                            log = "";
                        }
                        else
                        {
                            log = "received:" + Encoding.UTF8.GetString(b) + "\n" + log;
                        }
                    }
                    else
                    {
                        Console.WriteLine("x");
                    }
                }
            }
        }

        Thread th = new Thread(DealReceivedata);
        private void button_Click(object sender, RoutedEventArgs e)
        {
            topicstr = textBox_topic.Text;
            serveraddress = textBox_address.Text;
            if(runFlag)
            {
                th.Abort();                
            }
            else
            {
                th.Start();
            }
            runFlag = !runFlag;
                 
        }
    }
}

UI xaml:

<Window x:Class="WPFNanoMsgClient.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WPFNanoMsgClient"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <TextBox x:Name="textBox_address" HorizontalAlignment="Left" Height="21" Margin="32,19,0,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="128"/>
        <TextBox x:Name="textBox_topic" HorizontalAlignment="Left" Height="21" Margin="32,51,0,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="463"/>
        <TextBlock x:Name="textBlock_log" HorizontalAlignment="Left" Height="207" Margin="32,85,0,0" TextWrapping="Wrap" Text="TextBlock" VerticalAlignment="Top" Width="463"/>
        <Button x:Name="button" Content="订阅" HorizontalAlignment="Left" Height="34" Margin="394,6,0,0" VerticalAlignment="Top" Width="95" Click="button_Click"/>

    </Grid>
</Window>

Reference material

https://blog.csdn.net/u012635648/article/details/78665680

Guess you like

Origin www.cnblogs.com/youdias/p/12141728.html