|
#include <string> |
|
#include <iostream> |
|
#include <fstream> |
|
#include <iomanip> |
|
#include <memory> |
|
#pragma warning(disable:4251) |
|
#pragma warning(disable:4458) |
|
#pragma warning(disable:4244) |
|
#include "Simple-WebSocket-Server/server_ws.hpp" |
|
typedef SimpleWeb::SocketServer<SimpleWeb::WS> WsServer; |
|
|
|
using namespace std; |
|
|
|
int main(int, char**) |
|
{ |
|
|
|
std::stringstream ss; |
|
std::ifstream fileStr("payload.txt"); |
|
std::string temp; |
|
while (std::getline(fileStr, temp)) { |
|
ss << temp; |
|
} |
|
|
|
shared_ptr<WsServer> server; |
|
thread server_thread; |
|
|
|
cout << "s : Start server" << endl |
|
<< "t : sTop server" << endl |
|
<< "m : send Message to all clients" << endl |
|
<< "q : Quit" << endl; |
|
|
|
string line; |
|
while (line != "q") |
|
{ |
|
getline(cin, line); |
|
|
|
if (line == "s") |
|
{ |
|
server = make_shared<WsServer>(8088, 4); |
|
|
|
auto& tunnel = server->endpoint["^/echo/?$"]; |
|
tunnel.onmessage = [&server](shared_ptr<WsServer::Connection> connection, shared_ptr<WsServer::Message> message) |
|
{ |
|
auto message_str = message->string(); |
|
cout << message_str << endl; |
|
|
|
auto sendThisBack = make_shared<WsServer::SendStream>(); |
|
*sendThisBack << "Server echo: " << message_str; |
|
server->send(connection, sendThisBack, [](const boost::system::error_code code) |
|
{ |
|
if (code) |
|
{ |
|
cout << "Error while responding: " << code << ", error message: " << code.message() << endl; |
|
} |
|
}); |
|
}; |
|
|
|
tunnel.onopen = [](shared_ptr<WsServer::Connection> connection) |
|
{ |
|
cout << "Opened Connection: " << (size_t)connection.get() << " from: " << connection->remote_endpoint_address |
|
<< ":" << connection->remote_endpoint_port << endl; |
|
}; |
|
|
|
tunnel.onclose = [](shared_ptr<WsServer::Connection> connection, int status, const string& reason) |
|
{ |
|
cout << "Closed Connection: " << (size_t)connection.get() << " from: " << connection->remote_endpoint_address |
|
<< ":" << connection->remote_endpoint_port << endl; |
|
|
|
//See RFC 6455 7.4.1. for status codes |
|
cout << "ConnectsTo code: " << status << " Reason: " << reason << endl; |
|
}; |
|
|
|
tunnel.onerror = [](shared_ptr<WsServer::Connection> connection, const boost::system::error_code& code) |
|
{ |
|
//See http://www.boost.org/doc/libs/1_55_0/doc/html/boost_asio/reference.html, Error Codes for error code meanings |
|
cout << "Error in connection " << (size_t)connection.get() << ". " << "Error: " << code |
|
<< ", error message: " << code.message() << endl; |
|
}; |
|
|
|
server_thread = thread([&server]() |
|
{ |
|
server->start(); |
|
}); |
|
cout << "Server started" << endl; |
|
} |
|
else if (line == "c") |
|
{ |
|
server->stop(); |
|
server_thread.join(); |
|
server = nullptr; |
|
cout << "Server stopped" << endl; |
|
} |
|
else if (line == "m") |
|
{ |
|
int i = 0; |
|
for (auto connection : server->get_connections()) |
|
{ |
|
i++; |
|
string payload = ss.str(); |
|
auto msg = make_shared<WsServer::SendStream>(); |
|
*msg << payload; |
|
cout << "sending message..."; |
|
server->send(connection, msg, [&](const boost::system::error_code code) |
|
{ |
|
if (code) |
|
{ |
|
cout << "Error while sending to connnection: " << i << " Code: " << code |
|
<< " Message: " << code.message() << endl; |
|
} |
|
}); |
|
cout << "... sent message"; |
|
} |
|
} |
|
} |
|
|
|
if (server != nullptr) |
|
{ |
|
server->stop(); |
|
server_thread.join(); |
|
} |
|
} |