(ws client) all commands use spdlog instead of std::cerr or std::cout for logging
This commit is contained in:
@ -4,8 +4,8 @@
|
||||
* Copyright (c) 2018 Machine Zone, Inc. All rights reserved.
|
||||
*/
|
||||
|
||||
#include <iostream>
|
||||
#include <ixwebsocket/IXWebSocketServer.h>
|
||||
#include <spdlog/spdlog.h>
|
||||
#include <sstream>
|
||||
|
||||
namespace ix
|
||||
@ -14,7 +14,7 @@ namespace ix
|
||||
const std::string& hostname,
|
||||
const ix::SocketTLSOptions& tlsOptions)
|
||||
{
|
||||
std::cout << "ws_transfer: Listening on " << hostname << ":" << port << std::endl;
|
||||
spdlog::info("Listening on {}:{}", hostname, port);
|
||||
|
||||
ix::WebSocketServer server(port, hostname);
|
||||
server.setTLSOptions(tlsOptions);
|
||||
@ -25,22 +25,23 @@ namespace ix
|
||||
const WebSocketMessagePtr& msg) {
|
||||
if (msg->type == ix::WebSocketMessageType::Open)
|
||||
{
|
||||
std::cerr << "ws_transfer: New connection" << std::endl;
|
||||
std::cerr << "id: " << connectionState->getId() << std::endl;
|
||||
std::cerr << "Uri: " << msg->openInfo.uri << std::endl;
|
||||
std::cerr << "Headers:" << std::endl;
|
||||
spdlog::info("ws_transfer: New connection");
|
||||
spdlog::info("id: {}", connectionState->getId());
|
||||
spdlog::info("Uri: {}", msg->openInfo.uri);
|
||||
spdlog::info("Headers:");
|
||||
for (auto it : msg->openInfo.headers)
|
||||
{
|
||||
std::cerr << it.first << ": " << it.second << std::endl;
|
||||
spdlog::info("{}: {}", it.first, it.second);
|
||||
}
|
||||
}
|
||||
else if (msg->type == ix::WebSocketMessageType::Close)
|
||||
{
|
||||
std::cerr << "ws_transfer: [client " << connectionState->getId()
|
||||
<< "]: Closed connection, code " << msg->closeInfo.code << " reason "
|
||||
<< msg->closeInfo.reason << std::endl;
|
||||
spdlog::info("ws_transfer: Closed connection: client id {} code {} reason {}",
|
||||
connectionState->getId(),
|
||||
msg->closeInfo.code,
|
||||
msg->closeInfo.reason);
|
||||
auto remaining = server.getClients().erase(webSocket);
|
||||
std::cerr << "ws_transfer: " << remaining << " remaining clients " << std::endl;
|
||||
spdlog::info("ws_transfer: {} remaining clients", remaining);
|
||||
}
|
||||
else if (msg->type == ix::WebSocketMessageType::Error)
|
||||
{
|
||||
@ -49,40 +50,39 @@ namespace ix
|
||||
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;
|
||||
std::cerr << ss.str();
|
||||
spdlog::info(ss.str());
|
||||
}
|
||||
else if (msg->type == ix::WebSocketMessageType::Fragment)
|
||||
{
|
||||
std::cerr << "ws_transfer: Received message fragment " << std::endl;
|
||||
spdlog::info("ws_transfer: Received message fragment ");
|
||||
}
|
||||
else if (msg->type == ix::WebSocketMessageType::Message)
|
||||
{
|
||||
std::cerr << "ws_transfer: Received " << msg->wireSize << " bytes" << std::endl;
|
||||
spdlog::info("ws_transfer: Received {} bytes", msg->wireSize);
|
||||
size_t receivers = 0;
|
||||
for (auto&& client : server.getClients())
|
||||
{
|
||||
if (client != webSocket)
|
||||
{
|
||||
auto readyState = client->getReadyState();
|
||||
auto id = connectionState->getId();
|
||||
|
||||
if (readyState == ReadyState::Open)
|
||||
{
|
||||
++receivers;
|
||||
client->send(msg->str,
|
||||
msg->binary,
|
||||
[id = connectionState->getId()](int current,
|
||||
int total) -> bool {
|
||||
std::cerr << "ws_transfer: [client " << id
|
||||
<< "]: Step " << current << " out of "
|
||||
<< total << std::endl;
|
||||
[&id](int current, int total) -> bool {
|
||||
spdlog::info("{}: [client {}]: Step {} out of {}",
|
||||
"ws_transfer", id, current, total);
|
||||
return true;
|
||||
});
|
||||
|
||||
do
|
||||
{
|
||||
size_t bufferedAmount = client->bufferedAmount();
|
||||
std::cerr << "ws_transfer: [client " << connectionState->getId()
|
||||
<< "]: " << bufferedAmount
|
||||
<< " bytes left to be sent, " << std::endl;
|
||||
|
||||
spdlog::info("{}: [client {}]: {} bytes left to send",
|
||||
"ws_transfer", id, bufferedAmount);
|
||||
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(500));
|
||||
|
||||
@ -96,16 +96,15 @@ namespace ix
|
||||
? "Connecting"
|
||||
: readyState == ReadyState::Closing ? "Closing" : "Closed";
|
||||
size_t bufferedAmount = client->bufferedAmount();
|
||||
std::cerr << "ws_transfer: [client " << connectionState->getId()
|
||||
<< "]: has readystate '" << readyStateString << "' and "
|
||||
<< bufferedAmount << " bytes left to be sent, "
|
||||
<< std::endl;
|
||||
|
||||
spdlog::info("{}: [client {}]: has readystate {} bytes left to be sent",
|
||||
"ws_transfer", id, readyStateString, bufferedAmount);
|
||||
}
|
||||
}
|
||||
}
|
||||
if (!receivers)
|
||||
{
|
||||
std::cerr << "ws_transfer: no remaining receivers" << std::endl;
|
||||
spdlog::info("ws_transfer: no remaining receivers");
|
||||
}
|
||||
}
|
||||
});
|
||||
@ -114,7 +113,7 @@ namespace ix
|
||||
auto res = server.listen();
|
||||
if (!res.first)
|
||||
{
|
||||
std::cerr << res.second << std::endl;
|
||||
spdlog::info(res.second);
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user