improve publish interface, support publish struct

This commit is contained in:
qicosmos
2019-08-27 14:12:14 +08:00
parent 0892629ee8
commit 64373139fa
5 changed files with 59 additions and 34 deletions
+33 -26
View File
@@ -394,6 +394,12 @@ void test_sub1() {
});
client.subscribe("key", "unique_token", [](string_view data) {
msgpack_codec codec;
person p = codec.unpack<person>(data.data(), data.size());
std::cout << p.name << "\n";
});
client.subscribe("key1", "unique_token", [](string_view data) {
std::cout << data << "\n";
});
@@ -403,33 +409,34 @@ void test_sub1() {
return;
}
client1.publish("key", "hello subscriber");
client1.publish_by_token("key", "sub_key", "ok");
std::thread thd([&client1] {
while (true) {
try {
client1.publish("key", "hello subscriber");
client1.publish_by_token("key", "unique_token", "ok");
std::this_thread::sleep_for(std::chrono::seconds(1));
}
catch (const std::exception& ex) {
std::cout << ex.what() << "\n";
}
}
});
//person p{10, "jack", 21};
//client1.publish("key", "hello subscriber");
//client1.publish_by_token("key", "sub_key", p);
//
//std::thread thd([&client1, p] {
// while (true) {
// try {
// client1.publish("key", "hello subscriber");
// client1.publish_by_token("key", "unique_token", p);
// std::this_thread::sleep_for(std::chrono::seconds(1));
// }
// catch (const std::exception& ex) {
// std::cout << ex.what() << "\n";
// }
// }
//});
std::thread thd1([&client1] {
while (true) {
try {
int r = client1.call<int>("add", 2, 3);
std::cout << "add result: " << r << "\n";
}
catch (const std::exception& ex) {
std::cout << ex.what() << "\n";
}
}
});
//std::thread thd1([&client1] {
// while (true) {
// try {
// int r = client1.call<int>("add", 2, 3);
// std::cout << "add result: " << r << "\n";
// }
// catch (const std::exception& ex) {
// std::cout << ex.what() << "\n";
// }
// }
//});
std::string str;
std::cin >> str;