netmq测试

说明:

经对最新版本4.0.1.13测试。请示/响应和推拉模式都是并行的,哪个先传输完成先响应

发布订阅时好像不支持中文topic,没仔细测试

网上有说发布订阅是在客户端处理,在客户端判断是否符合,会有大量无效流量,经测试并非如此,如果不是本地订阅的topic则不会传输到本客户端。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
        PublisherSocket publisher;
        SubscriberSocket sub;
 
        public Form1()
        {
            InitializeComponent();
        }
 
        private void button1_Click(object sender, EventArgs e)
        {
            //发布
            if (this.publisher == null)
            {
                publisher = new PublisherSocket("tcp://*:3307");
            }
            byte[] buf = File.ReadAllBytes("video.rar");
            //publisher.SendMoreFrame("123").SendFrame("123");
            publisher.SendMoreFrame("123").SendFrame(buf);  //topic好像支持中文,未仔细测试
        }
 
        private void button2_Click(object sender, EventArgs e)
        {
            //订阅
            if (this.sub == null)
            {
                this.sub = new SubscriberSocket(this.textBox3.Text);
            }
            //网上有说只要发布的数据都会传到客户端,由客户端判断订阅的topic是否匹配
            //经测试最新版本4.0.1.13并非如此,如果不是本客户端订阅的不会传输到本地
            this.sub.Subscribe(this.textBox4.Text);
            this.textBox1.Text = this.sub.ReceiveFrameString(out bool more1); //发布的key
            //byte[] b = this.sub.ReceiveFrameBytes(out bool more2);
            this.textBox2.Text = this.sub.ReceiveFrameBytes().Length.ToString();
 
            // BinaryFormatter
        }
 
        private void button3_Click(object sender, EventArgs e)
        {
            MyObject obj = new MyObject { IntProperty = 1, StringProperty = "Samplk中文化e" };
            obj.buf = File.ReadAllBytes("e:\\图片2.png"); //图片长度426748
 
 
            BinaryFormatter formatter = new BinaryFormatter();
            MemoryStream msg = new MemoryStream();
            formatter.Serialize(msg, obj);
            byte[] ppp = msg.ToArray(); //长度427000
 
            msg.Position = 0;
            MyObject deserializedObj = formatter.Deserialize(msg) as MyObject;
 
            //DataContractSerializer
        }
 
        private void button4_Click(object sender, EventArgs e)
        {
            MyObject obj = new MyObject { IntProperty = 1, StringProperty = "Samplk中文化e" };
            obj.buf = File.ReadAllBytes("e:\\图片2.png"); //图片长度426748
 
            byte[] binaryData;
            using (var memoryStream = new MemoryStream())
            {
                using (var binaryWriter = new BinaryWriter(memoryStream))
                {
                    string json = JsonConvert.SerializeObject(obj);
                    int len = json.Length; //字符串长度569056
                    binaryWriter.Write(json);
                    binaryData = memoryStream.ToArray();//字符串长度569065
                }
            }
 
            // 反序列化二进制数据回对象
            MyObject deserializedObject;
            using (var memoryStream = new MemoryStream(binaryData))
            {
                using (var binaryReader = new BinaryReader(memoryStream))
                {
                    var json = binaryReader.ReadString();
                    deserializedObject = JsonConvert.DeserializeObject<MyObject>(json) as MyObject;
                }
            }
        }
 
        private void button5_Click(object sender, EventArgs e)
        {
            byte[] buf = File.ReadAllBytes("e:\\图片2.png"); //图片长度426748
            using (var memoryStream = new MemoryStream())
            {
                using (var binaryWriter = new BinaryWriter(memoryStream))
                {
                    string json = JsonConvert.SerializeObject(buf);
                    int len = json.Length; //字符串长度569002
                    binaryWriter.Write(json);
                    byte[] binaryData = memoryStream.ToArray();  //字节数组长度569005
                }
            }
 
            BinaryFormatter formatter = new BinaryFormatter();
            MemoryStream msg = new MemoryStream();
            formatter.Serialize(msg, buf);
            byte[] ppp = msg.ToArray(); //ppp的长度426776
 
        }
 
        private void button7_Click(object sender, EventArgs e)
        {
            //拉数据
            //经测试,两个客户端后发的可以先到,即不是串行的
            PullSocket pull = new PullSocket("tcp://*:3307");
            int m = 1;
            while (true)
            {
                byte[] buf1 = pull.ReceiveFrameBytes();
                File.WriteAllBytes($"d:\\tmp\\{m}.dat", buf1);
                File.WriteAllText($"d:\\tmp\\{m}.txt", DateTime.Now.ToString());
                this.textBox1.Text = DateTime.Now.ToString();
                m++;
                Application.DoEvents();
            }
 
 
        }
 
        private void button6_Click(object sender, EventArgs e)
        {
            //推数据
            PushSocket push = new PushSocket();
            push.Connect(this.textBox3.Text);
            byte[] buf = File.ReadAllBytes("video.rar");
            DateTime t = DateTime.Now;
            push.SendFrame(buf);
            this.textBox1.Text = (DateTime.Now - t).TotalSeconds.ToString();
            push.Close();
        }
 
        private void button9_Click(object sender, EventArgs e)
        {
            //等待模式
            //经测试,
            ResponseSocket res = new ResponseSocket("tcp://*:3307");
            int m = 1;
            while (true)
            {
                byte[] buf1 = res.ReceiveFrameBytes();
                res.SendFrame("OK: " + buf1.Length);
                File.WriteAllBytes($"d:\\tmp\\{m}.dat", buf1);
                File.WriteAllText($"d:\\tmp\\{m}.txt", DateTime.Now.ToString());
                this.textBox1.Text = DateTime.Now.ToString();
                m++;
                Application.DoEvents();
            }
        }
 
        private void button8_Click(object sender, EventArgs e)
        {
            //发消息,经测试,和PUSH一样,也是串行的,网络慢的先发送,网络快的后发,也是快的先被服务器收到
            RequestSocket req = new RequestSocket();
            req.Connect(this.textBox3.Text);
            byte[] buf = File.ReadAllBytes("video.rar");
            DateTime t = DateTime.Now;
            req.SendFrame(buf);
            this.textBox1.Text = (DateTime.Now - t).TotalSeconds.ToString();
            this.textBox2.Text = req.ReceiveFrameString();
            req.Close();
        }
    }
 
    [Serializable]
    public class MyObject
    {
        public int IntProperty { get; set; }
        public string StringProperty { get; set; }
        public byte[] buf { get; set; }
 
    }
}

  

 

posted @   81  阅读(9)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 我干了两个月的大项目,开源了!
· 千万级的大表,如何做性能调优?
· 推荐一款非常好用的在线 SSH 管理工具
· 盘点!HelloGitHub 年度热门开源项目
· Phi小模型开发教程:用C#开发本地部署AI聊天工具,只需CPU,不需要GPU,3G内存就可以运行,
历史上的今天:
2021-08-03 mysql的批量更新:MySqlConnector.MySqlBulkCopy ,修改数据库文件位置
2020-08-03 打开vs解决方案所有引用失效
2017-08-03 开发自己的DataSet查看器
点击右上角即可分享
微信分享提示