2018-10-09 06:42:45 +02:00
|
|
|
/*
|
|
|
|
* ws_connect.cpp
|
|
|
|
* Author: Benjamin Sergeant
|
|
|
|
* Copyright (c) 2017-2018 Machine Zone, Inc. All rights reserved.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <iostream>
|
|
|
|
#include <sstream>
|
|
|
|
#include <ixwebsocket/IXWebSocket.h>
|
|
|
|
#include <ixwebsocket/IXSocket.h>
|
|
|
|
|
2019-07-25 20:54:50 +02:00
|
|
|
#include "linenoise.hpp"
|
|
|
|
|
|
|
|
|
2019-02-23 05:49:26 +01:00
|
|
|
namespace ix
|
2018-10-09 06:42:45 +02:00
|
|
|
{
|
|
|
|
class WebSocketConnect
|
|
|
|
{
|
|
|
|
public:
|
2019-04-29 23:31:29 +02:00
|
|
|
WebSocketConnect(const std::string& _url,
|
2019-06-06 22:48:53 +02:00
|
|
|
bool disableAutomaticReconnection,
|
|
|
|
bool disablePerMessageDeflate);
|
2018-10-09 06:42:45 +02:00
|
|
|
|
|
|
|
void subscribe(const std::string& channel);
|
|
|
|
void start();
|
|
|
|
void stop();
|
|
|
|
|
|
|
|
void sendMessage(const std::string& text);
|
|
|
|
|
|
|
|
private:
|
|
|
|
std::string _url;
|
|
|
|
ix::WebSocket _webSocket;
|
2019-06-06 22:48:53 +02:00
|
|
|
bool _disablePerMessageDeflate;
|
2019-02-23 05:49:26 +01:00
|
|
|
|
|
|
|
void log(const std::string& msg);
|
2018-10-09 06:42:45 +02:00
|
|
|
};
|
|
|
|
|
2019-04-29 23:31:29 +02:00
|
|
|
WebSocketConnect::WebSocketConnect(const std::string& url,
|
2019-06-06 22:48:53 +02:00
|
|
|
bool disableAutomaticReconnection,
|
|
|
|
bool disablePerMessageDeflate) :
|
|
|
|
_url(url),
|
|
|
|
_disablePerMessageDeflate(disablePerMessageDeflate)
|
2018-10-09 06:42:45 +02:00
|
|
|
{
|
2019-04-29 23:31:29 +02:00
|
|
|
if (disableAutomaticReconnection)
|
|
|
|
{
|
2019-04-30 06:12:34 +02:00
|
|
|
_webSocket.disableAutomaticReconnection();
|
2019-04-29 23:31:29 +02:00
|
|
|
}
|
2018-10-09 06:42:45 +02:00
|
|
|
}
|
|
|
|
|
2019-02-23 05:49:26 +01:00
|
|
|
void WebSocketConnect::log(const std::string& msg)
|
|
|
|
{
|
|
|
|
std::cout << msg << std::endl;
|
|
|
|
}
|
|
|
|
|
2018-10-09 06:42:45 +02:00
|
|
|
void WebSocketConnect::stop()
|
|
|
|
{
|
|
|
|
_webSocket.stop();
|
|
|
|
}
|
|
|
|
|
|
|
|
void WebSocketConnect::start()
|
|
|
|
{
|
2018-11-10 03:23:49 +01:00
|
|
|
_webSocket.setUrl(_url);
|
2018-10-09 06:42:45 +02:00
|
|
|
|
2019-06-06 22:48:53 +02:00
|
|
|
if (_disablePerMessageDeflate)
|
|
|
|
{
|
|
|
|
_webSocket.disablePerMessageDeflate();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ix::WebSocketPerMessageDeflateOptions webSocketPerMessageDeflateOptions(
|
|
|
|
true, false, false, 15, 15);
|
|
|
|
_webSocket.setPerMessageDeflateOptions(webSocketPerMessageDeflateOptions);
|
|
|
|
}
|
2018-11-12 18:00:55 +01:00
|
|
|
|
2018-10-09 06:42:45 +02:00
|
|
|
std::stringstream ss;
|
|
|
|
log(std::string("Connecting to url: ") + _url);
|
|
|
|
|
|
|
|
_webSocket.setOnMessageCallback(
|
2019-06-09 20:33:17 +02:00
|
|
|
[this](const ix::WebSocketMessagePtr& msg)
|
2018-10-09 06:42:45 +02:00
|
|
|
{
|
|
|
|
std::stringstream ss;
|
2019-06-09 20:33:17 +02:00
|
|
|
if (msg->type == ix::WebSocketMessageType::Open)
|
2018-10-09 06:42:45 +02:00
|
|
|
{
|
|
|
|
log("ws_connect: connected");
|
2019-06-09 20:33:17 +02:00
|
|
|
std::cout << "Uri: " << msg->openInfo.uri << std::endl;
|
2018-11-12 18:00:55 +01:00
|
|
|
std::cout << "Handshake Headers:" << std::endl;
|
2019-06-09 20:33:17 +02:00
|
|
|
for (auto it : msg->openInfo.headers)
|
2018-11-12 18:00:55 +01:00
|
|
|
{
|
|
|
|
std::cout << it.first << ": " << it.second << std::endl;
|
|
|
|
}
|
2018-10-09 06:42:45 +02:00
|
|
|
}
|
2019-06-09 20:33:17 +02:00
|
|
|
else if (msg->type == ix::WebSocketMessageType::Close)
|
2018-10-09 06:42:45 +02:00
|
|
|
{
|
2018-10-31 18:27:17 +01:00
|
|
|
ss << "ws_connect: connection closed:";
|
2019-06-09 20:33:17 +02:00
|
|
|
ss << " code " << msg->closeInfo.code;
|
|
|
|
ss << " reason " << msg->closeInfo.reason << std::endl;
|
2018-10-31 18:27:17 +01:00
|
|
|
log(ss.str());
|
2018-10-09 06:42:45 +02:00
|
|
|
}
|
2019-06-09 20:33:17 +02:00
|
|
|
else if (msg->type == ix::WebSocketMessageType::Message)
|
2018-10-09 06:42:45 +02:00
|
|
|
{
|
2019-06-09 20:33:17 +02:00
|
|
|
std::cerr << "Received " << msg->wireSize << " bytes" << std::endl;
|
2019-03-04 22:56:30 +01:00
|
|
|
|
2018-10-09 06:42:45 +02:00
|
|
|
ss << "ws_connect: received message: "
|
2019-06-09 20:33:17 +02:00
|
|
|
<< msg->str;
|
2018-10-09 06:42:45 +02:00
|
|
|
log(ss.str());
|
|
|
|
}
|
2019-06-09 20:33:17 +02:00
|
|
|
else if (msg->type == ix::WebSocketMessageType::Error)
|
2018-10-09 06:42:45 +02:00
|
|
|
{
|
2019-06-09 20:33:17 +02:00
|
|
|
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;
|
2018-10-09 06:42:45 +02:00
|
|
|
log(ss.str());
|
|
|
|
}
|
2019-06-09 20:33:17 +02:00
|
|
|
else if (msg->type == ix::WebSocketMessageType::Fragment)
|
2019-05-07 21:12:27 +02:00
|
|
|
{
|
|
|
|
std::cerr << "Received message fragment" << std::endl;
|
|
|
|
}
|
2019-06-09 20:33:17 +02:00
|
|
|
else if (msg->type == ix::WebSocketMessageType::Ping)
|
2019-05-07 21:12:27 +02:00
|
|
|
{
|
|
|
|
std::cerr << "Received ping" << std::endl;
|
|
|
|
}
|
2019-06-09 20:33:17 +02:00
|
|
|
else if (msg->type == ix::WebSocketMessageType::Pong)
|
2019-05-07 21:12:27 +02:00
|
|
|
{
|
|
|
|
std::cerr << "Received pong" << std::endl;
|
|
|
|
}
|
2018-10-09 06:42:45 +02:00
|
|
|
else
|
|
|
|
{
|
|
|
|
ss << "Invalid ix::WebSocketMessageType";
|
|
|
|
log(ss.str());
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
_webSocket.start();
|
|
|
|
}
|
|
|
|
|
|
|
|
void WebSocketConnect::sendMessage(const std::string& text)
|
|
|
|
{
|
2019-06-09 19:22:27 +02:00
|
|
|
_webSocket.sendText(text);
|
2018-10-09 06:42:45 +02:00
|
|
|
}
|
|
|
|
|
2019-06-06 22:48:53 +02:00
|
|
|
int ws_connect_main(const std::string& url,
|
|
|
|
bool disableAutomaticReconnection,
|
|
|
|
bool disablePerMessageDeflate)
|
2018-10-09 06:42:45 +02:00
|
|
|
{
|
|
|
|
std::cout << "Type Ctrl-D to exit prompt..." << std::endl;
|
2019-06-06 22:48:53 +02:00
|
|
|
WebSocketConnect webSocketChat(url,
|
|
|
|
disableAutomaticReconnection,
|
|
|
|
disablePerMessageDeflate);
|
2018-10-09 06:42:45 +02:00
|
|
|
webSocketChat.start();
|
|
|
|
|
|
|
|
while (true)
|
|
|
|
{
|
2019-07-25 20:54:50 +02:00
|
|
|
// Read line
|
|
|
|
std::string line;
|
|
|
|
auto quit = linenoise::Readline("> ", line);
|
2018-10-09 06:42:45 +02:00
|
|
|
|
2019-07-25 20:54:50 +02:00
|
|
|
if (quit)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (line == "/stop")
|
2018-10-31 18:27:17 +01:00
|
|
|
{
|
|
|
|
std::cout << "Stopping connection..." << std::endl;
|
|
|
|
webSocketChat.stop();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2019-07-25 20:54:50 +02:00
|
|
|
if (line == "/start")
|
2018-10-31 18:27:17 +01:00
|
|
|
{
|
|
|
|
std::cout << "Starting connection..." << std::endl;
|
|
|
|
webSocketChat.start();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2019-07-25 20:54:50 +02:00
|
|
|
webSocketChat.sendMessage(line);
|
2018-10-09 06:42:45 +02:00
|
|
|
|
2019-07-25 20:54:50 +02:00
|
|
|
// Add text to history
|
|
|
|
linenoise::AddHistory(line.c_str());
|
2018-10-09 06:42:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
std::cout << std::endl;
|
|
|
|
webSocketChat.stop();
|
|
|
|
|
2019-02-23 05:49:26 +01:00
|
|
|
return 0;
|
2018-10-09 06:42:45 +02:00
|
|
|
}
|
|
|
|
}
|
2019-02-23 05:49:26 +01:00
|
|
|
|