0cff5065d9
* add skeleton and broken http client code. GET returns "Resource temporarily unavailable" errors... * linux compile fix * can GET some pages * Update formatting in README.md * unittest for sending large messages * document bug * Feature/send large message (#14) * introduce send fragment * can pass a fin frame * can send messages which are a perfect multiple of the chunk size * set fin only for last fragment * cleanup * last fragment should be of type CONTINUATION * Add simple send and receive programs * speedups receiving + better way to wait for thing * receive speedup by using linked list of chunks instead of large array * document bug * use chunks to receive data * trailing spaces * Update README.md Add note about message fragmentation. * Feature/ws cli (#15) * New command line tool for transfering files / still very beta. * add readme * use cli11 for argument parsing * json -> msgpack * stop using base64 and use binary which can be stored in message pack * add target for building with homebrew * all CMakeLists are referenced by the top level one * add ws_chat and ws_connect sub commands to ws * cleanup * add echo and broadcast server as ws sub-commands * add gitignore * comments * ping pong added to ws * mv cobra_publisher under ws folder * Update README.md * linux build fix * linux build fix * move http_client to a ws sub-command * simple HTTP post support (urlencode parameters) * can specify extra headers * chunk encoding / simple redirect support / -I option * follow redirects is optional * make README vim markdown plugin friendly * cleanup argument parsing + add socket creation factory * add missing file * http gzip compression * cleanup * doc * Feature/send large message (#14) * introduce send fragment * can pass a fin frame * can send messages which are a perfect multiple of the chunk size * set fin only for last fragment * cleanup * last fragment should be of type CONTINUATION * Add simple send and receive programs * speedups receiving + better way to wait for thing * receive speedup by using linked list of chunks instead of large array * document bug * use chunks to receive data * trailing spaces
193 lines
5.3 KiB
C++
193 lines
5.3 KiB
C++
/*
|
|
* ws_chat.cpp
|
|
* Author: Benjamin Sergeant
|
|
* Copyright (c) 2017-2019 Machine Zone, Inc. All rights reserved.
|
|
*/
|
|
|
|
//
|
|
// Simple chat program that talks to a broadcast server
|
|
// Broadcast server can be ran with `ws broadcast_server`
|
|
//
|
|
|
|
#include <iostream>
|
|
#include <sstream>
|
|
#include <queue>
|
|
#include <ixwebsocket/IXWebSocket.h>
|
|
#include <ixwebsocket/IXSocket.h>
|
|
|
|
#include "nlohmann/json.hpp"
|
|
|
|
// for convenience
|
|
using json = nlohmann::json;
|
|
|
|
namespace ix
|
|
{
|
|
class WebSocketChat
|
|
{
|
|
public:
|
|
WebSocketChat(const std::string& url,
|
|
const std::string& user);
|
|
|
|
void subscribe(const std::string& channel);
|
|
void start();
|
|
void stop();
|
|
bool isReady() const;
|
|
|
|
void sendMessage(const std::string& text);
|
|
size_t getReceivedMessagesCount() const;
|
|
|
|
std::string encodeMessage(const std::string& text);
|
|
std::pair<std::string, std::string> decodeMessage(const std::string& str);
|
|
|
|
private:
|
|
std::string _url;
|
|
std::string _user;
|
|
ix::WebSocket _webSocket;
|
|
std::queue<std::string> _receivedQueue;
|
|
|
|
void log(const std::string& msg);
|
|
};
|
|
|
|
WebSocketChat::WebSocketChat(const std::string& url,
|
|
const std::string& user) :
|
|
_url(url),
|
|
_user(user)
|
|
{
|
|
;
|
|
}
|
|
|
|
void WebSocketChat::log(const std::string& msg)
|
|
{
|
|
std::cout << msg << std::endl;
|
|
}
|
|
|
|
size_t WebSocketChat::getReceivedMessagesCount() const
|
|
{
|
|
return _receivedQueue.size();
|
|
}
|
|
|
|
bool WebSocketChat::isReady() const
|
|
{
|
|
return _webSocket.getReadyState() == ix::WebSocket_ReadyState_Open;
|
|
}
|
|
|
|
void WebSocketChat::stop()
|
|
{
|
|
_webSocket.stop();
|
|
}
|
|
|
|
void WebSocketChat::start()
|
|
{
|
|
_webSocket.setUrl(_url);
|
|
|
|
std::stringstream ss;
|
|
log(std::string("Connecting to url: ") + _url);
|
|
|
|
_webSocket.setOnMessageCallback(
|
|
[this](ix::WebSocketMessageType messageType,
|
|
const std::string& str,
|
|
size_t wireSize,
|
|
const ix::WebSocketErrorInfo& error,
|
|
const ix::WebSocketOpenInfo& openInfo,
|
|
const ix::WebSocketCloseInfo& closeInfo)
|
|
{
|
|
std::stringstream ss;
|
|
if (messageType == ix::WebSocket_MessageType_Open)
|
|
{
|
|
ss << "cmd_websocket_chat: user "
|
|
<< _user
|
|
<< " Connected !";
|
|
log(ss.str());
|
|
}
|
|
else if (messageType == ix::WebSocket_MessageType_Close)
|
|
{
|
|
ss << "cmd_websocket_chat: user "
|
|
<< _user
|
|
<< " disconnected !";
|
|
log(ss.str());
|
|
}
|
|
else if (messageType == ix::WebSocket_MessageType_Message)
|
|
{
|
|
auto result = decodeMessage(str);
|
|
|
|
// Our "chat" / "broacast" node.js server does not send us
|
|
// the messages we send, so we don't have to filter it out.
|
|
|
|
// store text
|
|
_receivedQueue.push(result.second);
|
|
|
|
ss << std::endl
|
|
<< result.first << " > " << result.second
|
|
<< std::endl
|
|
<< _user << " > ";
|
|
log(ss.str());
|
|
}
|
|
else if (messageType == ix::WebSocket_MessageType_Error)
|
|
{
|
|
ss << "Connection error: " << error.reason << std::endl;
|
|
ss << "#retries: " << error.retries << std::endl;
|
|
ss << "Wait time(ms): " << error.wait_time << std::endl;
|
|
ss << "HTTP Status: " << error.http_status << std::endl;
|
|
log(ss.str());
|
|
}
|
|
else
|
|
{
|
|
ss << "Invalid ix::WebSocketMessageType";
|
|
log(ss.str());
|
|
}
|
|
});
|
|
|
|
_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)
|
|
{
|
|
_webSocket.send(encodeMessage(text));
|
|
}
|
|
|
|
int ws_chat_main(const std::string& url,
|
|
const std::string& user)
|
|
{
|
|
std::cout << "Type Ctrl-D to exit prompt..." << std::endl;
|
|
WebSocketChat webSocketChat(url, user);
|
|
webSocketChat.start();
|
|
|
|
while (true)
|
|
{
|
|
std::string text;
|
|
std::cout << user << " > " << std::flush;
|
|
std::getline(std::cin, text);
|
|
|
|
if (!std::cin)
|
|
{
|
|
break;
|
|
}
|
|
|
|
webSocketChat.sendMessage(text);
|
|
}
|
|
|
|
std::cout << std::endl;
|
|
webSocketChat.stop();
|
|
}
|
|
}
|