test sub/pub

This commit is contained in:
qicosmos
2019-08-27 16:13:37 +08:00
parent 82fe83f2ce
commit 34645d29cc
2 changed files with 42 additions and 35 deletions
+33 -31
View File
@@ -393,50 +393,52 @@ void test_sub1() {
std::cout << data << "\n";
});
client.subscribe("key", "unique_token", [](string_view data) {
client.subscribe("key", "048a796c8a3c6a6b7bd1223bf2c8cee05232e927b521984ba417cb2fca6df9d1", [](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) {
client.subscribe("key1", "048a796c8a3c6a6b7bd1223bf2c8cee05232e927b521984ba417cb2fca6df9d1", [](string_view data) {
std::cout << data << "\n";
});
rpc_client client1;
bool stop = false;
std::thread thd1([&client, &stop] {
while (true) {
try {
int r = client.call<int>("add", 2, 3);
std::cout << "add result: " << r << "\n";
std::this_thread::sleep_for(std::chrono::milliseconds(1));
}
catch (const std::exception& ex) {
std::cout << ex.what() << "\n";
}
}
});
/*rpc_client client1;
bool r1 = client1.connect("127.0.0.1", 9000);
if (!r1) {
return;
}
//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";
// }
// }
//});
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);
}
catch (const std::exception& ex) {
std::cout << ex.what() << "\n";
}
}
});
*/
std::string str;
std::cin >> str;
+9 -4
View File
@@ -6,7 +6,9 @@ using namespace rpc_service;
#include "qps.h"
struct dummy{
int add(rpc_conn conn, int a, int b) { return a + b; }
int add(rpc_conn conn, int a, int b) {
return a + b;
}
};
std::string translate(rpc_conn conn, const std::string& orignal) {
@@ -116,10 +118,13 @@ int main() {
std::thread thd([&server] {
person p{ 1, "tom", 20 };
while (true) {
std::this_thread::sleep_for(std::chrono::seconds(1));
server.publish("key", "hello subscriber");
server.publish_by_token("key", "unique_token", p);
server.publish_by_token("key1", "unique_token", "hello subscriber1");
auto list = server.get_token_list();
for (auto& token : list) {
server.publish_by_token("key", token, p);
server.publish_by_token("key1", token, "hello subscriber1");
}
std::this_thread::sleep_for(std::chrono::milliseconds(50));
}
});