drogonframework / drogonframework/drogon
WebSocketConnectionPtr send doesn't works all the times
- Dominant language
- C++
- Stars
- 14.3k
- Forks
- 1.4k
- Avg merge
- 1d 12h
- Merged PRs (30d)
- 15
Description
I'm trying to send message to a specific WebSocketConnectionPtr after an HTTP request, but the message is not always really sent.
- WebSocketConnectionPtr->connected() is true;
- WebSocketController correctly receive message from that connection;
- I checked WebSocketConnectionPtr address and are the same between calls;
This is how I manage a bridge between HttpController and WebSocketController:
```
class WSBridge{
public:
static WSBridge& getInstance(){
static PubSubManager instance;
return instance;
}
~WSBridge();
void addConnection(std::string id, drogon::WebSocketConnectionPtr wsConnection){
connections[id].push_back(wsConnection);
}
void removeConnection(std::string id, drogon::WebSocketConnectionPtr wsConnection){
auto conns = connections[id];
conns.erase(std::remove(conns.begin(), conns.end(), wsConnection), conns.end());
if (conns.size() == 0) {
connections.erase(broadcaster_id);
} else {
connections[id] = conns;
}
}
void broadcast(std::string id, std::string message){
if (connections.find(id) != connections.end()) {
for (auto& connection : connections[id]) {
connection->send(message);
}
}
}
private:
explicit WSBridge();
std::unordered_map> connections;
};
```
I've first used drogon::PubSubService and the behaviour is the same, so I moved to unordered_map, maybe I rewrite PubSubService logics.
Adding connection in `handleNewConnection` with `PubSubManager::getInstance().addConnection(id, wsConnPtr);`
Sending message with `PubSubManager::getInstance().broadcast(id, "A MSG");`
**Expected behavior**
Receiving always messages on broadcast calls
**Additional context**
Sometimes I need to restart drogon once and reconnect the WS clients to correctly receive message, sometimes even after 2 or 3 tries it doens't work. I still can't reproduce it 100%, it is still a random behaviour.
It seems like the bugged WebSocketConnectionPtr is not properly connected, indeed when this behaviour occurs and shutdown drogon the WS is still "connected" and is not true, the one it works properly disconnect as soon I shutdown drogon
Tesing on Windows 10 with Postman client and drogon server on windows too
Contributor guide
Assessment
This issue has not been assessed yet.