2021-05-27 12:09:57 +00:00
|
|
|
#include <drogon/WebSocketClient.h>
|
|
|
|
#include <drogon/HttpAppFramework.h>
|
|
|
|
|
|
|
|
#include <iostream>
|
|
|
|
|
|
|
|
using namespace drogon;
|
|
|
|
using namespace std::chrono_literals;
|
|
|
|
|
|
|
|
int main(int argc, char *argv[])
|
|
|
|
{
|
|
|
|
std::string server;
|
|
|
|
std::string path;
|
|
|
|
optional<uint16_t> port;
|
|
|
|
// Connect to a public echo server
|
|
|
|
if (argc > 1 && std::string(argv[1]) == "-p")
|
|
|
|
{
|
|
|
|
server = "wss://echo.websocket.org";
|
|
|
|
path = "/";
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
server = "ws://127.0.0.1";
|
|
|
|
port = 8848;
|
|
|
|
path = "/chat";
|
|
|
|
}
|
|
|
|
|
2021-06-24 15:29:50 +00:00
|
|
|
std::string serverString;
|
2021-05-27 12:09:57 +00:00
|
|
|
if (port.value_or(0) != 0)
|
2021-06-24 15:29:50 +00:00
|
|
|
serverString = server + ":" + std::to_string(port.value());
|
2021-05-27 12:09:57 +00:00
|
|
|
else
|
2021-06-24 15:29:50 +00:00
|
|
|
serverString = server;
|
|
|
|
auto wsPtr = WebSocketClient::newWebSocketClient(serverString);
|
2021-05-27 12:09:57 +00:00
|
|
|
auto req = HttpRequest::newHttpRequest();
|
|
|
|
req->setPath(path);
|
|
|
|
|
|
|
|
wsPtr->setMessageHandler([](const std::string &message,
|
2021-06-27 01:54:02 +00:00
|
|
|
const WebSocketClientPtr &,
|
2021-05-27 12:09:57 +00:00
|
|
|
const WebSocketMessageType &type) {
|
|
|
|
std::string messageType = "Unknown";
|
|
|
|
if (type == WebSocketMessageType::Text)
|
|
|
|
messageType = "text";
|
|
|
|
else if (type == WebSocketMessageType::Pong)
|
|
|
|
messageType = "pong";
|
|
|
|
else if (type == WebSocketMessageType::Ping)
|
|
|
|
messageType = "ping";
|
|
|
|
else if (type == WebSocketMessageType::Binary)
|
|
|
|
messageType = "binary";
|
|
|
|
else if (type == WebSocketMessageType::Close)
|
|
|
|
messageType = "Close";
|
|
|
|
|
|
|
|
LOG_INFO << "new message (" << messageType << "): " << message;
|
|
|
|
});
|
|
|
|
|
2021-06-27 01:54:02 +00:00
|
|
|
wsPtr->setConnectionClosedHandler([](const WebSocketClientPtr &) {
|
2021-05-27 12:09:57 +00:00
|
|
|
LOG_INFO << "WebSocket connection closed!";
|
|
|
|
});
|
|
|
|
|
|
|
|
LOG_INFO << "Connecting to WebSocket at " << server;
|
|
|
|
wsPtr->connectToServer(
|
|
|
|
req,
|
|
|
|
[](ReqResult r,
|
2021-06-27 01:54:02 +00:00
|
|
|
const HttpResponsePtr &,
|
2021-05-27 12:09:57 +00:00
|
|
|
const WebSocketClientPtr &wsPtr) {
|
|
|
|
if (r != ReqResult::Ok)
|
|
|
|
{
|
|
|
|
LOG_ERROR << "Failed to establish WebSocket connection!";
|
2021-08-15 10:17:05 +00:00
|
|
|
wsPtr->stop();
|
|
|
|
return;
|
2021-05-27 12:09:57 +00:00
|
|
|
}
|
|
|
|
LOG_INFO << "WebSocket connected!";
|
|
|
|
wsPtr->getConnection()->setPingMessage("", 2s);
|
|
|
|
wsPtr->getConnection()->send("hello!");
|
|
|
|
});
|
|
|
|
|
|
|
|
// Quit the application after 15 seconds
|
|
|
|
app().getLoop()->runAfter(15, []() { app().quit(); });
|
|
|
|
|
|
|
|
app().setLogLevel(trantor::Logger::kDebug);
|
|
|
|
app().run();
|
|
|
|
LOG_INFO << "bye!";
|
|
|
|
return 0;
|
2021-08-15 10:17:05 +00:00
|
|
|
}
|