netmq测试
说明:
经对最新版本4.0.1.13测试。请示/响应和推拉模式都是并行的,哪个先传输完成先响应
发布订阅时好像不支持中文topic,没仔细测试
网上有说发布订阅是在客户端处理,在客户端判断是否符合,会有大量无效流量,经测试并非如此,如果不是本地订阅的topic则不会传输到本客户端。
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; }
}
}
浙公网安备 33010602011771号