ws echo_server has a -g option to print a greeting message on connect

This commit is contained in:
Benjamin Sergeant 2019-06-08 09:16:33 -07:00
parent e1a7395880
commit 9623ceb4d5
4 changed files with 14 additions and 6 deletions

View File

@ -4,6 +4,7 @@ All notable changes to this project will be documented in this file.
## [Unreleased] - 2019-06-xx ## [Unreleased] - 2019-06-xx
### Changed ### Changed
- IXSocketMbedTLS: better error handling in close and connect - IXSocketMbedTLS: better error handling in close and connect
- ws echo_server has a -g option to print a greeting message on connect
## [3.1.2] - 2019-06-06 ## [3.1.2] - 2019-06-06
### Added ### Added

View File

@ -81,6 +81,7 @@ int main(int argc, char** argv)
bool stress = false; bool stress = false;
bool disableAutomaticReconnection = false; bool disableAutomaticReconnection = false;
bool disablePerMessageDeflate = false; bool disablePerMessageDeflate = false;
bool greetings = false;
int port = 8008; int port = 8008;
int redisPort = 6379; int redisPort = 6379;
int statsdPort = 8125; int statsdPort = 8125;
@ -120,6 +121,7 @@ int main(int argc, char** argv)
CLI::App* echoServerApp = app.add_subcommand("echo_server", "Echo server"); CLI::App* echoServerApp = app.add_subcommand("echo_server", "Echo server");
echoServerApp->add_option("--port", port, "Port"); echoServerApp->add_option("--port", port, "Port");
echoServerApp->add_option("--host", hostname, "Hostname"); echoServerApp->add_option("--host", hostname, "Hostname");
echoServerApp->add_flag("-g", greetings, "Verbose");
CLI::App* broadcastServerApp = app.add_subcommand("broadcast_server", "Broadcasting server"); CLI::App* broadcastServerApp = app.add_subcommand("broadcast_server", "Broadcasting server");
broadcastServerApp->add_option("--port", port, "Port"); broadcastServerApp->add_option("--port", port, "Port");
@ -252,7 +254,7 @@ int main(int argc, char** argv)
} }
else if (app.got_subcommand("echo_server")) else if (app.got_subcommand("echo_server"))
{ {
ret = ix::ws_echo_server_main(port, hostname); ret = ix::ws_echo_server_main(port, greetings, hostname);
} }
else if (app.got_subcommand("broadcast_server")) else if (app.got_subcommand("broadcast_server"))
{ {

View File

@ -24,7 +24,7 @@ namespace ix
int ws_ping_pong_main(const std::string& url); int ws_ping_pong_main(const std::string& url);
int ws_echo_server_main(int port, const std::string& hostname); int ws_echo_server_main(int port, bool greetings, const std::string& hostname);
int ws_broadcast_server_main(int port, const std::string& hostname); int ws_broadcast_server_main(int port, const std::string& hostname);
int ws_transfer_main(int port, const std::string& hostname); int ws_transfer_main(int port, const std::string& hostname);

View File

@ -10,18 +10,18 @@
namespace ix namespace ix
{ {
int ws_echo_server_main(int port, const std::string& hostname) int ws_echo_server_main(int port, bool greetings, const std::string& hostname)
{ {
std::cout << "Listening on " << hostname << ":" << port << std::endl; std::cout << "Listening on " << hostname << ":" << port << std::endl;
ix::WebSocketServer server(port, hostname); ix::WebSocketServer server(port, hostname);
server.setOnConnectionCallback( server.setOnConnectionCallback(
[](std::shared_ptr<ix::WebSocket> webSocket, [greetings](std::shared_ptr<ix::WebSocket> webSocket,
std::shared_ptr<ConnectionState> connectionState) std::shared_ptr<ConnectionState> connectionState)
{ {
webSocket->setOnMessageCallback( webSocket->setOnMessageCallback(
[webSocket, connectionState](ix::WebSocketMessageType messageType, [webSocket, connectionState, greetings](ix::WebSocketMessageType messageType,
const std::string& str, const std::string& str,
size_t wireSize, size_t wireSize,
const ix::WebSocketErrorInfo& error, const ix::WebSocketErrorInfo& error,
@ -38,6 +38,11 @@ namespace ix
{ {
std::cerr << it.first << ": " << it.second << std::endl; std::cerr << it.first << ": " << it.second << std::endl;
} }
if (greetings)
{
webSocket->sendText("Welcome !");
}
} }
else if (messageType == ix::WebSocketMessageType::Close) else if (messageType == ix::WebSocketMessageType::Close)
{ {