2018-10-25 21:01:47 +02:00
|
|
|
/*
|
2019-02-23 06:47:57 +01:00
|
|
|
* ws_ping_pong.cpp
|
2018-10-25 21:01:47 +02:00
|
|
|
* Author: Benjamin Sergeant
|
2019-02-23 06:47:57 +01:00
|
|
|
* Copyright (c) 2018-2019 Machine Zone, Inc. All rights reserved.
|
2018-10-25 21:01:47 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <iostream>
|
|
|
|
#include <ixwebsocket/IXSocket.h>
|
2019-09-23 19:25:23 +02:00
|
|
|
#include <ixwebsocket/IXWebSocket.h>
|
|
|
|
#include <sstream>
|
2018-10-25 21:01:47 +02:00
|
|
|
|
2019-02-23 06:47:57 +01:00
|
|
|
namespace ix
|
2018-10-25 21:01:47 +02:00
|
|
|
{
|
|
|
|
class WebSocketPingPong
|
|
|
|
{
|
2019-09-23 19:25:23 +02:00
|
|
|
public:
|
|
|
|
WebSocketPingPong(const std::string& _url);
|
2018-10-25 21:01:47 +02:00
|
|
|
|
2019-09-23 19:25:23 +02:00
|
|
|
void subscribe(const std::string& channel);
|
|
|
|
void start();
|
|
|
|
void stop();
|
2018-10-25 21:01:47 +02:00
|
|
|
|
2019-09-23 19:25:23 +02:00
|
|
|
void ping(const std::string& text);
|
|
|
|
void send(const std::string& text);
|
2018-10-25 21:01:47 +02:00
|
|
|
|
2019-09-23 19:25:23 +02:00
|
|
|
private:
|
|
|
|
std::string _url;
|
|
|
|
ix::WebSocket _webSocket;
|
2019-02-23 06:47:57 +01:00
|
|
|
|
2019-09-23 19:25:23 +02:00
|
|
|
void log(const std::string& msg);
|
2018-10-25 21:01:47 +02:00
|
|
|
};
|
|
|
|
|
2019-09-23 19:25:23 +02:00
|
|
|
WebSocketPingPong::WebSocketPingPong(const std::string& url)
|
|
|
|
: _url(url)
|
2018-10-25 21:01:47 +02:00
|
|
|
{
|
|
|
|
;
|
|
|
|
}
|
|
|
|
|
2019-02-23 06:47:57 +01:00
|
|
|
void WebSocketPingPong::log(const std::string& msg)
|
|
|
|
{
|
|
|
|
std::cout << msg << std::endl;
|
|
|
|
}
|
|
|
|
|
2018-10-25 21:01:47 +02:00
|
|
|
void WebSocketPingPong::stop()
|
|
|
|
{
|
|
|
|
_webSocket.stop();
|
|
|
|
}
|
|
|
|
|
|
|
|
void WebSocketPingPong::start()
|
|
|
|
{
|
2018-11-10 03:23:49 +01:00
|
|
|
_webSocket.setUrl(_url);
|
2018-10-25 21:01:47 +02:00
|
|
|
|
|
|
|
std::stringstream ss;
|
|
|
|
log(std::string("Connecting to url: ") + _url);
|
|
|
|
|
2019-09-23 19:25:23 +02:00
|
|
|
_webSocket.setOnMessageCallback([this](const ix::WebSocketMessagePtr& msg) {
|
|
|
|
std::cerr << "Received " << msg->wireSize << " bytes" << std::endl;
|
|
|
|
|
|
|
|
std::stringstream ss;
|
|
|
|
if (msg->type == ix::WebSocketMessageType::Open)
|
2018-10-25 21:01:47 +02:00
|
|
|
{
|
2019-09-23 19:25:23 +02:00
|
|
|
log("ping_pong: connected");
|
2019-03-04 22:56:30 +01:00
|
|
|
|
2019-09-23 19:25:23 +02:00
|
|
|
std::cout << "Uri: " << msg->openInfo.uri << std::endl;
|
|
|
|
std::cout << "Handshake Headers:" << std::endl;
|
|
|
|
for (auto it : msg->openInfo.headers)
|
2018-10-25 21:01:47 +02:00
|
|
|
{
|
2019-09-23 19:25:23 +02:00
|
|
|
std::cout << it.first << ": " << it.second << std::endl;
|
2018-10-25 21:01:47 +02:00
|
|
|
}
|
2019-09-23 19:25:23 +02:00
|
|
|
}
|
|
|
|
else if (msg->type == ix::WebSocketMessageType::Close)
|
|
|
|
{
|
|
|
|
ss << "ping_pong: disconnected:"
|
|
|
|
<< " code " << msg->closeInfo.code << " reason " << msg->closeInfo.reason
|
|
|
|
<< msg->str;
|
|
|
|
log(ss.str());
|
|
|
|
}
|
|
|
|
else if (msg->type == ix::WebSocketMessageType::Message)
|
|
|
|
{
|
|
|
|
ss << "ping_pong: received message: " << msg->str;
|
|
|
|
log(ss.str());
|
|
|
|
}
|
|
|
|
else if (msg->type == ix::WebSocketMessageType::Ping)
|
|
|
|
{
|
|
|
|
ss << "ping_pong: received ping message: " << msg->str;
|
|
|
|
log(ss.str());
|
|
|
|
}
|
|
|
|
else if (msg->type == ix::WebSocketMessageType::Pong)
|
|
|
|
{
|
|
|
|
ss << "ping_pong: received pong message: " << msg->str;
|
|
|
|
log(ss.str());
|
|
|
|
}
|
|
|
|
else if (msg->type == ix::WebSocketMessageType::Error)
|
|
|
|
{
|
|
|
|
ss << "Connection error: " << msg->errorInfo.reason << std::endl;
|
|
|
|
ss << "#retries: " << msg->errorInfo.retries << std::endl;
|
|
|
|
ss << "Wait time(ms): " << msg->errorInfo.wait_time << std::endl;
|
|
|
|
ss << "HTTP Status: " << msg->errorInfo.http_status << std::endl;
|
|
|
|
log(ss.str());
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ss << "Invalid ix::WebSocketMessageType";
|
|
|
|
log(ss.str());
|
|
|
|
}
|
|
|
|
});
|
2018-10-25 21:01:47 +02:00
|
|
|
|
|
|
|
_webSocket.start();
|
|
|
|
}
|
|
|
|
|
|
|
|
void WebSocketPingPong::ping(const std::string& text)
|
|
|
|
{
|
2018-11-10 03:23:49 +01:00
|
|
|
if (!_webSocket.ping(text).success)
|
2018-10-25 23:46:23 +02:00
|
|
|
{
|
2019-09-23 19:25:23 +02:00
|
|
|
std::cerr << "Failed to send ping message. Message too long (> 125 bytes) or endpoint "
|
|
|
|
"is disconnected"
|
2018-10-25 23:46:23 +02:00
|
|
|
<< std::endl;
|
|
|
|
}
|
2018-10-25 21:01:47 +02:00
|
|
|
}
|
|
|
|
|
2018-10-26 03:51:19 +02:00
|
|
|
void WebSocketPingPong::send(const std::string& text)
|
|
|
|
{
|
|
|
|
_webSocket.send(text);
|
|
|
|
}
|
|
|
|
|
2019-02-23 06:47:57 +01:00
|
|
|
int ws_ping_pong_main(const std::string& url)
|
2018-10-25 21:01:47 +02:00
|
|
|
{
|
|
|
|
std::cout << "Type Ctrl-D to exit prompt..." << std::endl;
|
|
|
|
WebSocketPingPong webSocketPingPong(url);
|
|
|
|
webSocketPingPong.start();
|
|
|
|
|
|
|
|
while (true)
|
|
|
|
{
|
|
|
|
std::string text;
|
|
|
|
std::cout << "> " << std::flush;
|
|
|
|
std::getline(std::cin, text);
|
|
|
|
|
|
|
|
if (!std::cin)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2018-10-26 03:51:19 +02:00
|
|
|
if (text == "/close")
|
|
|
|
{
|
|
|
|
webSocketPingPong.send(text);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
webSocketPingPong.ping(text);
|
|
|
|
}
|
2018-10-25 21:01:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
std::cout << std::endl;
|
|
|
|
webSocketPingPong.stop();
|
2019-03-04 22:56:30 +01:00
|
|
|
|
|
|
|
return 0;
|
2018-10-25 21:01:47 +02:00
|
|
|
}
|
2019-09-23 19:25:23 +02:00
|
|
|
} // namespace ix
|