Skip to content

Instantly share code, notes, and snippets.

@MatrixManAtYrService
Last active September 9, 2016 15:48
Show Gist options
  • Save MatrixManAtYrService/2661cbb01a30db7460fb70084491ce18 to your computer and use it in GitHub Desktop.
Save MatrixManAtYrService/2661cbb01a30db7460fb70084491ce18 to your computer and use it in GitHub Desktop.
Simple-WebSocket-Server wrapper programs

I have been using these programs to work with parts of the Simple-WebSocket-Server library in Visual Studio 2015. Notice that SampleServer.cpp reads in a file called payload.txt, and then sends that payload when you send the 'm' command from the server, so you'll want to have a payload.txt in your working directory when you run it.

To connect:

  • Run both the client and the server.
  • [server] Press s<enter> to start the server
  • [client] Press s<enter> to connect the client

To send messages

  • Be connected
  • [server] Press m<enter> to send a message to all connected clients
  • [client] Press m<enter> to send a message to the server
#include <string>
#include <iostream>
#include <iomanip>
//#include <thread>
#include "boost/thread.hpp"
#pragma warning(disable:4244)
#include "Simple-WebSocket-Server/client_ws.hpp"
typedef SimpleWeb::SocketClient<SimpleWeb::WS> WsClient;
using namespace std;
int main(int, char**)
{
shared_ptr<WsClient> client;
boost::thread client_thread;
cout << "s : Set up connection" << endl
<< "l : cLose connection with message" << endl
<< "m : send Message" << endl
<< "q : Quit" << endl;
string line;
while (line != "q")
{
getline(cin, line);
if (line == "s")
{
client = std::make_shared<WsClient>("localhost:8088/echo");
client->onopen = []()
{
cout << "Client Started & Connection Opened" << endl;
};
client->onclose = [](int status, const string& reason)
{
cout << "Closed Connection with code: " << status << " Reason: " << reason << endl;
};
client->onerror = [](const boost::system::error_code& code)
{
cout << "Error: " << code
<< ", error message: " << code.message() << endl;
};
client->onmessage = [](shared_ptr<WsClient::Message> message)
{
cout << message->string() << endl;
};
client_thread = boost::thread([&client]()
{
client->start();
});
cout << "Connection started" << endl;
}
else if (line == "l")
{
client->send_close(10, "Word to your moms, I came to drop bombs, I got more rhymes than the bible's got psalms.", [](const boost::system::error_code code)
{
cout << "Error on send_close Code: " << code
<< " Message: " << code.message() << endl;
});
cout << "Connection closed with message" << endl;
}
else if (line == "m")
{
auto msg = std::make_shared<WsClient::SendStream>();
*msg << "It's tricky to rock a rhyme to rock a rhyme that's right on time it's tricky!";
client->send(msg, [&](const boost::system::error_code code)
{
if (code)
{
cout << "Error while sending message. Code: " << code
<< " Message: " << code.message() << endl;
}
});
cout << "Message sent" << endl;
}
}
if (client != nullptr)
{
client->stop();
client_thread.join();
}
}
#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();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment