2018-09-27 23:56:48 +02:00
|
|
|
/*
|
2019-02-23 05:49:26 +01:00
|
|
|
* ws_chat.cpp
|
2018-09-27 23:56:48 +02:00
|
|
|
* Author: Benjamin Sergeant
|
2019-02-23 05:49:26 +01:00
|
|
|
* Copyright (c) 2017-2019 Machine Zone, Inc. All rights reserved.
|
2018-09-27 23:56:48 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
//
|
2019-03-01 06:54:03 +01:00
|
|
|
// Simple chat program that talks to a broadcast server
|
|
|
|
// Broadcast server can be ran with `ws broadcast_server`
|
|
|
|
//
|
|
|
|
|
2019-09-23 19:25:23 +02:00
|
|
|
#include "nlohmann/json.hpp"
|
2020-01-09 22:45:31 +01:00
|
|
|
#include <iostream>
|
2018-10-09 06:42:45 +02:00
|
|
|
#include <ixwebsocket/IXSocket.h>
|
2019-09-23 19:25:23 +02:00
|
|
|
#include <ixwebsocket/IXWebSocket.h>
|
|
|
|
#include <queue>
|
2019-12-25 06:55:34 +01:00
|
|
|
#include <spdlog/spdlog.h>
|
2019-12-30 17:46:18 +01:00
|
|
|
#include <sstream>
|
2018-09-27 23:56:48 +02:00
|
|
|
|
|
|
|
// for convenience
|
|
|
|
using json = nlohmann::json;
|
|
|
|
|
2019-02-23 05:49:26 +01:00
|
|
|
namespace ix
|
2018-09-27 23:56:48 +02:00
|
|
|
{
|
|
|
|
class WebSocketChat
|
|
|
|
{
|
2019-09-23 19:25:23 +02:00
|
|
|
public:
|
|
|
|
WebSocketChat(const std::string& url, const std::string& user);
|
2018-09-27 23:56:48 +02:00
|
|
|
|
2019-09-23 19:25:23 +02:00
|
|
|
void subscribe(const std::string& channel);
|
|
|
|
void start();
|
|
|
|
void stop();
|
|
|
|
bool isReady() const;
|
2018-09-27 23:56:48 +02:00
|
|
|
|
2019-09-23 19:25:23 +02:00
|
|
|
void sendMessage(const std::string& text);
|
|
|
|
size_t getReceivedMessagesCount() const;
|
2018-09-27 23:56:48 +02:00
|
|
|
|
2019-09-23 19:25:23 +02:00
|
|
|
std::string encodeMessage(const std::string& text);
|
|
|
|
std::pair<std::string, std::string> decodeMessage(const std::string& str);
|
2018-09-27 23:56:48 +02:00
|
|
|
|
2019-09-23 19:25:23 +02:00
|
|
|
private:
|
|
|
|
std::string _url;
|
|
|
|
std::string _user;
|
|
|
|
ix::WebSocket _webSocket;
|
|
|
|
std::queue<std::string> _receivedQueue;
|
2019-02-23 05:49:26 +01:00
|
|
|
|
2019-09-23 19:25:23 +02:00
|
|
|
void log(const std::string& msg);
|
2018-09-27 23:56:48 +02:00
|
|
|
};
|
|
|
|
|
2019-09-23 19:25:23 +02:00
|
|
|
WebSocketChat::WebSocketChat(const std::string& url, const std::string& user)
|
|
|
|
: _url(url)
|
|
|
|
, _user(user)
|
2018-09-27 23:56:48 +02:00
|
|
|
{
|
|
|
|
;
|
|
|
|
}
|
|
|
|
|
2019-02-23 05:49:26 +01:00
|
|
|
void WebSocketChat::log(const std::string& msg)
|
|
|
|
{
|
2019-12-25 06:55:34 +01:00
|
|
|
spdlog::info(msg);
|
2019-02-23 05:49:26 +01:00
|
|
|
}
|
|
|
|
|
2018-09-27 23:56:48 +02:00
|
|
|
size_t WebSocketChat::getReceivedMessagesCount() const
|
|
|
|
{
|
|
|
|
return _receivedQueue.size();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool WebSocketChat::isReady() const
|
|
|
|
{
|
2019-05-11 23:22:06 +02:00
|
|
|
return _webSocket.getReadyState() == ix::ReadyState::Open;
|
2018-09-27 23:56:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void WebSocketChat::stop()
|
|
|
|
{
|
|
|
|
_webSocket.stop();
|
|
|
|
}
|
|
|
|
|
|
|
|
void WebSocketChat::start()
|
|
|
|
{
|
2019-02-23 05:49:26 +01:00
|
|
|
_webSocket.setUrl(_url);
|
2018-09-27 23:56:48 +02:00
|
|
|
|
|
|
|
std::stringstream ss;
|
2019-02-23 05:49:26 +01:00
|
|
|
log(std::string("Connecting to url: ") + _url);
|
2018-09-27 23:56:48 +02:00
|
|
|
|
2019-09-23 19:25:23 +02:00
|
|
|
_webSocket.setOnMessageCallback([this](const WebSocketMessagePtr& msg) {
|
|
|
|
std::stringstream ss;
|
|
|
|
if (msg->type == ix::WebSocketMessageType::Open)
|
2018-09-27 23:56:48 +02:00
|
|
|
{
|
2019-09-23 19:25:23 +02:00
|
|
|
log("ws chat: connected");
|
2019-12-25 06:55:34 +01:00
|
|
|
spdlog::info("Uri: {}", msg->openInfo.uri);
|
|
|
|
spdlog::info("Headers:");
|
2019-09-23 19:25:23 +02:00
|
|
|
for (auto it : msg->openInfo.headers)
|
2018-09-27 23:56:48 +02:00
|
|
|
{
|
2019-12-25 06:55:34 +01:00
|
|
|
spdlog::info("{}: {}", it.first, it.second);
|
2018-09-27 23:56:48 +02:00
|
|
|
}
|
|
|
|
|
2019-12-25 06:55:34 +01:00
|
|
|
spdlog::info("ws chat: user {} connected !", _user);
|
2019-09-23 19:25:23 +02:00
|
|
|
log(ss.str());
|
|
|
|
}
|
|
|
|
else if (msg->type == ix::WebSocketMessageType::Close)
|
|
|
|
{
|
2019-12-25 06:55:34 +01:00
|
|
|
ss << "ws chat user disconnected: " << _user;
|
|
|
|
ss << " code " << msg->closeInfo.code;
|
|
|
|
ss << " reason " << msg->closeInfo.reason << std::endl;
|
2019-09-23 19:25:23 +02:00
|
|
|
log(ss.str());
|
|
|
|
}
|
|
|
|
else if (msg->type == ix::WebSocketMessageType::Message)
|
|
|
|
{
|
|
|
|
auto result = decodeMessage(msg->str);
|
2018-09-27 23:56:48 +02:00
|
|
|
|
2019-09-23 19:25:23 +02:00
|
|
|
// Our "chat" / "broacast" node.js server does not send us
|
|
|
|
// the messages we send, so we don't have to filter it out.
|
2018-09-27 23:56:48 +02:00
|
|
|
|
2019-09-23 19:25:23 +02:00
|
|
|
// store text
|
|
|
|
_receivedQueue.push(result.second);
|
|
|
|
|
|
|
|
ss << std::endl
|
|
|
|
<< result.first << "(" << msg->wireSize << " bytes)"
|
|
|
|
<< " > " << result.second << std::endl
|
|
|
|
<< _user << " > ";
|
|
|
|
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-09-27 23:56:48 +02:00
|
|
|
|
|
|
|
_webSocket.start();
|
|
|
|
}
|
|
|
|
|
|
|
|
std::pair<std::string, std::string> WebSocketChat::decodeMessage(const std::string& str)
|
|
|
|
{
|
|
|
|
auto j = json::parse(str);
|
|
|
|
|
|
|
|
std::string msg_user = j["user"];
|
|
|
|
std::string msg_text = j["text"];
|
|
|
|
|
|
|
|
return std::pair<std::string, std::string>(msg_user, msg_text);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string WebSocketChat::encodeMessage(const std::string& text)
|
|
|
|
{
|
|
|
|
json j;
|
|
|
|
j["user"] = _user;
|
|
|
|
j["text"] = text;
|
|
|
|
|
|
|
|
std::string output = j.dump();
|
|
|
|
return output;
|
|
|
|
}
|
|
|
|
|
|
|
|
void WebSocketChat::sendMessage(const std::string& text)
|
|
|
|
{
|
2019-06-09 19:22:27 +02:00
|
|
|
_webSocket.sendText(encodeMessage(text));
|
2018-09-27 23:56:48 +02:00
|
|
|
}
|
|
|
|
|
2019-09-23 19:25:23 +02:00
|
|
|
int ws_chat_main(const std::string& url, const std::string& user)
|
2018-09-27 23:56:48 +02:00
|
|
|
{
|
2019-12-25 06:55:34 +01:00
|
|
|
spdlog::info("Type Ctrl-D to exit prompt...");
|
2019-02-23 05:49:26 +01:00
|
|
|
WebSocketChat webSocketChat(url, user);
|
2018-09-27 23:56:48 +02:00
|
|
|
webSocketChat.start();
|
|
|
|
|
|
|
|
while (true)
|
|
|
|
{
|
2019-12-25 06:55:34 +01:00
|
|
|
// Read line
|
|
|
|
std::string line;
|
2020-01-08 02:37:38 +01:00
|
|
|
std::cout << user << " > " << std::flush;
|
|
|
|
std::getline(std::cin, line);
|
2018-09-27 23:56:48 +02:00
|
|
|
|
2020-01-08 02:37:38 +01:00
|
|
|
if (!std::cin)
|
2018-09-27 23:56:48 +02:00
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2019-12-25 06:55:34 +01:00
|
|
|
webSocketChat.sendMessage(line);
|
2018-09-27 23:56:48 +02:00
|
|
|
}
|
|
|
|
|
2019-12-25 06:55:34 +01:00
|
|
|
spdlog::info("");
|
2018-09-27 23:56:48 +02:00
|
|
|
webSocketChat.stop();
|
2019-03-04 22:56:30 +01:00
|
|
|
|
|
|
|
return 0;
|
2018-09-27 23:56:48 +02:00
|
|
|
}
|
2019-09-23 19:25:23 +02:00
|
|
|
} // namespace ix
|