Skip to content

Commit

Permalink
ithewei#223: WebSocketClient::WebSocketClient(EventLoopPtr loop = NULL)
Browse files Browse the repository at this point in the history
  • Loading branch information
ithewei committed Jul 20, 2022
1 parent aba82b9 commit 040638c
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 17 deletions.
19 changes: 12 additions & 7 deletions evpp/TcpClientEventLoop_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@

using namespace hv;

class MyTcpClient : public TcpClientEventLoopTmpl<SocketChannel> {
class MyTcpClient : public TcpClient {
public:
MyTcpClient(EventLoopPtr loop = NULL) : TcpClientEventLoopTmpl<SocketChannel>(loop) {
MyTcpClient(EventLoopPtr loop = NULL) : TcpClient(loop) {
onConnection = [this](const SocketChannelPtr& channel) {
std::string peeraddr = channel->peeraddr();
if (channel->isConnected()) {
Expand Down Expand Up @@ -67,7 +67,8 @@ class MyTcpClient : public TcpClientEventLoopTmpl<SocketChannel> {
withTLS();
#endif
printf("client connect to port %d, connfd=%d ...\n", port, connfd);
return startConnect();
start();
return connfd;
}
};
typedef std::shared_ptr<MyTcpClient> MyTcpClientPtr;
Expand All @@ -79,15 +80,19 @@ int main(int argc, char* argv[]) {
}
int port = atoi(argv[1]);

EventLoopPtr loop(new EventLoop);
EventLoopThreadPtr loop_thread(new EventLoopThread);
loop_thread->start();

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

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

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

return 0;
}
11 changes: 8 additions & 3 deletions examples/websocket_client_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,17 @@ int main(int argc, char** argv) {

std::string str;
while (std::getline(std::cin, str)) {
if (!ws.isConnected()) break;
if (str == "quit") {
if (str == "close") {
ws.close();
} else if (str == "open") {
ws.open(url, headers);
} else if (str == "stop") {
ws.stop();
break;
} else {
if (!ws.isConnected()) break;
ws.send(str);
}
ws.send(str);
}

return 0;
Expand Down
11 changes: 5 additions & 6 deletions http/client/WebSocketClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,16 @@

namespace hv {

WebSocketClient::WebSocketClient()
: TcpClientTmpl<WebSocketChannel>()
WebSocketClient::WebSocketClient(EventLoopPtr loop)
: TcpClientTmpl<WebSocketChannel>(loop)
{
state = WS_CLOSED;
ping_interval = DEFAULT_WS_PING_INTERVAL;
ping_cnt = 0;
}

WebSocketClient::~WebSocketClient() {
close();
stop();
}

/*
Expand Down Expand Up @@ -45,6 +45,7 @@ int WebSocketClient::open(const char* _url, const http_headers& headers) {

int connfd = createsocket(http_req_->port, http_req_->host.c_str());
if (connfd < 0) {
hloge("createsocket %s:%d return %d!", http_req_->host.c_str(), http_req_->port, connfd);
return connfd;
}

Expand Down Expand Up @@ -181,9 +182,7 @@ int WebSocketClient::open(const char* _url, const http_headers& headers) {
}

int WebSocketClient::close() {
if (channel == NULL) return -1;
channel->close();
stop();
closesocket();
state = WS_CLOSED;
return 0;
}
Expand Down
2 changes: 1 addition & 1 deletion http/client/WebSocketClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class HV_EXPORT WebSocketClient : public TcpClientTmpl<WebSocketChannel> {
// PATCH: onmessage not given opcode
enum ws_opcode opcode() { return channel ? channel->opcode : WS_OPCODE_CLOSE; }

WebSocketClient();
WebSocketClient(EventLoopPtr loop = NULL);
~WebSocketClient();

// url = ws://ip:port/path
Expand Down

0 comments on commit 040638c

Please sign in to comment.