-
Notifications
You must be signed in to change notification settings - Fork 279
Getter to Class Connection Status #59
Comments
I would maybe do something like this: #include "server_ws.hpp"
#include <unordered_map>
using namespace std;
typedef SimpleWeb::SocketServer<SimpleWeb::WS> WsServer;
class Player {
};
int main() {
WsServer server;
server.config.port = 8080;
unordered_map<std::shared_ptr<WsServer::Connection>, Player> connections;
auto &echo = server.endpoint["^/echo/?$"];
echo.on_message = [&server](shared_ptr<WsServer::Connection> connection, shared_ptr<WsServer::Message> message) {
};
echo.on_open = [&connections](shared_ptr<WsServer::Connection> connection) {
connections.emplace(connection, Player());
};
//See RFC 6455 7.4.1. for status codes
echo.on_close = [&connections](shared_ptr<WsServer::Connection> connection, int status, const string & /*reason*/) {
connections.erase(connection);
};
//See http://www.boost.org/doc/libs/1_55_0/doc/html/boost_asio/reference.html, Error Codes for error code meanings
echo.on_error = [&connections](shared_ptr<WsServer::Connection> connection, const boost::system::error_code &ec) {
connections.erase(connection);
};
server.start();
} edit: decided to use shared_ptr in |
Of course the above example takes it for granted that you run the server in one thread. If more threads are used, you would need a |
Thank you, that fast answer, i will try your solution. |
If it is needed, I'll add the getter. You mean a getter for socket::is_open I take it? |
I was thinking something like the javascript implementation of WebSocket with a readyState that could be an enum
so we can have a: socket::getReadyState; |
I do not immediately see a need for this. Also, one can easily implement this oneself using the on_open, on_close/on_error, on_message handlers. |
yes it's true but i think it will be nice if we can get the status of the socket somehow without having to redo the work twice. |
Hi,
First of all, thank you for this great library !
I was wondering, how do I get the status of a Connection ?
Let me explain, in my program, each of the Server's Clients are linked with a
User
, therefore when a Client connects to theWsServer
, I instantiate a new User and store theConnection
in it.But when i need to clean my list of
User
, I would like to know the status of the socket so that I can delete theUser
when the connection is lost.With that idea in mind, in my class User you can see that I had to create a boolean
closed
, this variable is set totrue
by theWsServer.on_close
function.The User class looks like that:
Do you think it is possible to add a getter to the
Connection
Class to get it's status ?The text was updated successfully, but these errors were encountered: