Skip to content

Commit

Permalink
Add WebSocketClient::setHttpRequest getHttpResponse
Browse files Browse the repository at this point in the history
  • Loading branch information
ithewei committed Nov 14, 2022
1 parent f0ee909 commit 08b0766
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
16 changes: 14 additions & 2 deletions examples/websocket_client_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,10 @@ class MyWebSocketClient : public WebSocketClient {

int connect(const char* url) {
// set callbacks
onopen = []() {
printf("onopen\n");
onopen = [this]() {
const HttpResponsePtr& resp = getHttpResponse();
printf("onopen\n%s\n", resp->body.c_str());
// printf("response:\n%s\n", resp->Dump(true, true).c_str());
};
onmessage = [this](const std::string& msg) {
printf("onmessage(type=%s len=%d): %.*s\n", opcode() == WS_OPCODE_TEXT ? "text" : "binary",
Expand All @@ -39,6 +41,16 @@ class MyWebSocketClient : public WebSocketClient {
reconn.delay_policy = 2;
setReconnect(&reconn);

/*
HttpRequestPtr req = std::make_shared<HttpRequest>();
req->method = HTTP_POST;
req->headers["Origin"] = "http://example.com";
req->json["app_id"] = "123456";
req->json["app_secret"] = "abcdefg";
printf("request:\n%s\n", req->Dump(true, true).c_str());
setHttpRequest(req);
*/

http_headers headers;
headers["Origin"] = "http://example.com/";
return open(url, headers);
Expand Down
10 changes: 10 additions & 0 deletions http/client/WebSocketClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,16 @@ class HV_EXPORT WebSocketClient : public TcpClientTmpl<WebSocketChannel> {
ping_interval = ms;
}

// NOTE: call before open
void setHttpRequest(const HttpRequestPtr& req) {
http_req_ = req;
}

// NOTE: call when onopen
const HttpResponsePtr& getHttpResponse() {
return http_resp_;
}

private:
enum State {
CONNECTING,
Expand Down

0 comments on commit 08b0766

Please sign in to comment.