IXWebSocket
IXWebSocket copied to clipboard
Question about weak_ptr in API
Hi,
Just curious to know why WebSocketServer::OnConnectionCallback takes a weak_ptr<WebSocket> as its first argument and not a shared_ptr?
I don't often use weak_ptr so I'm not sure what I should infer from it's use here. I am just coverting it to a shared_ptr and capturing in the socket's own OnMessageCallback with no apparent ill effects, is this OK? eg:
g_server->setOnConnectionCallback([](std::weak_ptr<ix::WebSocket> wsocket, std::shared_ptr<ix::ConnectionState>) { //
log() << "### WebSocketServer new connection!";
auto socket = wsocket.lock(); // convert to shared_ptr
socket->setOnMessageCallback([socket](const ix::WebSocketMessagePtr& wsMsg) { //
// blah blah blah...
socket->send(result);
});
And looking at the WebSocketServer code I can see it's originally a shared_ptr anyway so it seems a bit odd, although like I say I have no experience with weak_ptr (and not a lot with shared/unique ptrs either!)
Bye, Mark