(ws_connect) display sent/received bytes statistics on exit

This commit is contained in:
Benjamin Sergeant 2020-02-26 11:23:36 -08:00
parent 6d0c568aaa
commit 140a21c8b3
3 changed files with 29 additions and 1 deletions

View File

@ -1,6 +1,10 @@
# Changelog
All changes to this project will be documented in this file.
## [8.1.6] - 2020-02-26
(ws_connect) display sent/received bytes statistics on exit
## [8.1.5] - 2020-02-23
(server) give thread name to some usual worker threads / unittest is broken !!

View File

@ -6,4 +6,4 @@
#pragma once
#define IX_WEBSOCKET_VERSION "8.1.4"
#define IX_WEBSOCKET_VERSION "8.1.6"

View File

@ -30,6 +30,9 @@ namespace ix
void start();
void stop();
int getSentBytes() { return _sentBytes; }
int getReceivedBytes() { return _receivedBytes; }
void sendMessage(const std::string& text);
private:
@ -38,6 +41,8 @@ namespace ix
ix::WebSocket _webSocket;
bool _disablePerMessageDeflate;
bool _binaryMode;
std::atomic<int> _receivedBytes;
std::atomic<int> _sentBytes;
void log(const std::string& msg);
WebSocketHttpHeaders parseHeaders(const std::string& data);
@ -54,6 +59,8 @@ namespace ix
: _url(url)
, _disablePerMessageDeflate(disablePerMessageDeflate)
, _binaryMode(binaryMode)
, _receivedBytes(0)
, _sentBytes(0)
{
if (disableAutomaticReconnection)
{
@ -68,6 +75,20 @@ namespace ix
{
_webSocket.addSubProtocol(subprotocol);
}
WebSocket::setTrafficTrackerCallback(
[this](int size, bool incoming)
{
if (incoming)
{
_receivedBytes += size;
}
else
{
_sentBytes += size;
}
}
);
}
void WebSocketConnect::log(const std::string& msg)
@ -246,6 +267,9 @@ namespace ix
spdlog::info("");
webSocketChat.stop();
spdlog::info("Received {} bytes", webSocketChat.getReceivedBytes());
spdlog::info("Sent {} bytes", webSocketChat.getSentBytes());
return 0;
}
} // namespace ix