nuclearc
nuclearc

Reputation: 33

c++ - WebSocketPP multiple clients

I have problem with WebSocketPP Server. I want it to handle multiple clients. Here is my OnOpen method:

void Server::onOpen(
    Server* srv,
    WSServer* ws,
    websocketpp::connection_hdl& hdl)
{
    ServerPlayerTracker con;
    con.con = &hdl;
    con.protocolVersion = 0;
    con.verified = false;
    con.playerID = srv->playerCount++;
    con.roomID = 0;

    srv->players.push_back(con);
}

But in disconnection i have problem. I cant find what player with ID disconnected. Here is my OnClose method:

void Server::onClose(
    Server* srv,
    WSServer* ws,
    websocketpp::connection_hdl& hdl)
{
    for (int i = 0; i < srv->players.size(); i++)
    {
        if (srv->players[i].connected)
        {
            if ((*srv->players[i].con).lock() == hdl.lock())
            {
                printf("[!] Player disconnected with ID: %d\n", 
                    srv->players[i].playerID);
                srv->players.erase(srv->players.begin() + i);
            }
        }
    }
}

In line (*srv->players[i].con).lock() == hdl.lock() it throws exception like 'this was 0xFFFFFFFFFFFFFFF7.' in file 'memory' line 75. I think it's problem with converting weak_ptr to shared_ptr. Is there any way to fix that?

Upvotes: 0

Views: 1892

Answers (1)

aaa
aaa

Reputation: 715

My comments seemed enough to fix the problem (see comments). For future reference and to indicate that this problem has been answered, I have created this answer.


I'm not 100% sure what is (or is not) working in your current code, since it's quite different from the way the connections are stored and retrieved within the example code (See websocketPP github/documentation example "associative storage").

Using the example, it should be rather easy to set up a multiple client structure, in the way it was intended by the library creator.

For your specific error, I believe you're on the right track about the shared/weak pointer conversion. Best solution would be to use the list in the way it's used in the example.

Especially interesting is the "con_list" which saves all connections. It's a typedef of std::map<connection_hdl,connection_data,std::owner_less<conn‌​ection_hdl>> con_list; con_list m_connections; and should enable you to store and retrieve connections (and their session data).

Upvotes: 1

Related Questions