Skip to content

Commit

Permalink
TestMultiClientsRunInOneEventLoop
Browse files Browse the repository at this point in the history
  • Loading branch information
ithewei committed Sep 23, 2022
1 parent 1e1fbae commit 4ddebfc
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 36 deletions.
34 changes: 22 additions & 12 deletions evpp/TcpClientEventLoop_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,21 +73,16 @@ class MyTcpClient : public TcpClient {
};
typedef std::shared_ptr<MyTcpClient> MyTcpClientPtr;

int main(int argc, char* argv[]) {
if (argc < 2) {
printf("Usage: %s port\n", argv[0]);
return -10;
}
int port = atoi(argv[1]);

int TestMultiClientsRunInOneEventLoop(int port, int nclients) {
EventLoopThreadPtr loop_thread(new EventLoopThread);
loop_thread->start();

MyTcpClientPtr cli1(new MyTcpClient(loop_thread->loop()));
cli1->connect(port);

MyTcpClientPtr cli2(new MyTcpClient(loop_thread->loop()));
cli2->connect(port);
std::map<int, MyTcpClientPtr> clients;
for (int i = 0; i < nclients; ++i) {
MyTcpClient* client = new MyTcpClient(loop_thread->loop());
client->connect(port);
clients[i] = MyTcpClientPtr(client);
}

// press Enter to stop
while (getchar() != '\n');
Expand All @@ -96,3 +91,18 @@ int main(int argc, char* argv[]) {

return 0;
}

int main(int argc, char* argv[]) {
if (argc < 2) {
printf("Usage: %s port\n", argv[0]);
return -10;
}
int port = atoi(argv[1]);

int nclients = 100;
if (argc > 2) {
nclients = atoi(argv[2]);
}

return TestMultiClientsRunInOneEventLoop(port, nclients);
}
88 changes: 64 additions & 24 deletions examples/websocket_client_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
*
* @build make examples
* @server bin/websocket_server_test 8888
* @client bin/websocket_client_test ws://127.0.0.1:8888/
* @client bin/websocket_client_test ws://127.0.0.1:8888/test
* @clients bin/websocket_client_test ws://127.0.0.1:8888/test 100
* @js html/websocket_client.html
*
*/
Expand All @@ -12,43 +13,82 @@

using namespace hv;

class MyWebSocketClient : public WebSocketClient {
public:
MyWebSocketClient(EventLoopPtr loop = NULL) : WebSocketClient(loop) {}
~MyWebSocketClient() {}

int connect(const char* url) {
// set callbacks
onopen = []() {
printf("onopen\n");
};
onmessage = [this](const std::string& msg) {
printf("onmessage(type=%s len=%d): %.*s\n", opcode() == WS_OPCODE_TEXT ? "text" : "binary",
(int)msg.size(), (int)msg.size(), msg.data());
};
onclose = []() {
printf("onclose\n");
};

// reconnect: 1,2,4,8,10,10,10...
reconn_setting_t reconn;
reconn_setting_init(&reconn);
reconn.min_delay = 1000;
reconn.max_delay = 10000;
reconn.delay_policy = 2;
setReconnect(&reconn);

http_headers headers;
headers["Origin"] = "http://example.com/";
return open(url, headers);
};
};
typedef std::shared_ptr<MyWebSocketClient> MyWebSocketClientPtr;

int TestMultiClientsRunInOneEventLoop(const char* url, int nclients) {
EventLoopThreadPtr loop_thread(new EventLoopThread);
loop_thread->start();

std::map<int, MyWebSocketClientPtr> clients;
for (int i = 0; i < nclients; ++i) {
MyWebSocketClient* client = new MyWebSocketClient(loop_thread->loop());
client->connect(url);
clients[i] = MyWebSocketClientPtr(client);
}

// press Enter to stop
while (getchar() != '\n');
loop_thread->stop();
loop_thread->join();

return 0;
}

int main(int argc, char** argv) {
if (argc < 2) {
printf("Usage: %s url\n", argv[0]);
return -10;
}
const char* url = argv[1];

WebSocketClient ws;
ws.onopen = []() {
printf("onopen\n");
};
ws.onmessage = [&ws](const std::string& msg) {
printf("onmessage(type=%s len=%d): %.*s\n", ws.opcode() == WS_OPCODE_TEXT ? "text" : "binary",
(int)msg.size(), (int)msg.size(), msg.data());
};
ws.onclose = []() {
printf("onclose\n");
};

// reconnect: 1,2,4,8,10,10,10...
reconn_setting_t reconn;
reconn_setting_init(&reconn);
reconn.min_delay = 1000;
reconn.max_delay = 10000;
reconn.delay_policy = 2;
ws.setReconnect(&reconn);
int nclients = 0;
if (argc > 2) {
nclients = atoi(argv[2]);
}
if (nclients > 0) {
return TestMultiClientsRunInOneEventLoop(url, nclients);
}

http_headers headers;
headers["Origin"] = "http://example.com/";
ws.open(url, headers);
MyWebSocketClient ws;
ws.connect(url);

std::string str;
while (std::getline(std::cin, str)) {
if (str == "close") {
ws.close();
} else if (str == "open") {
ws.open(url, headers);
ws.connect(url);
} else if (str == "stop") {
ws.stop();
break;
Expand Down

0 comments on commit 4ddebfc

Please sign in to comment.