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

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