sendoneframetextasync failed。
jackieliuqihe opened this issue · comments
hello,
here is my question,when i use websocket,i use a map store the oatpp::websocket::AsyncWebSocket objects,but when i want to send message by websocket,i call the function sendoneframetextasync,but it just new an object,does not call the function act(),so cann't send message,please teach me how to solve it.thank you.
Hello @jackieliuqihe ,
Thanks for the question!
In short, you have to store your sockets as a map as shared_ptr
like this:
std::unordered_map<oatpp::String, std::shared_ptr<oatpp::websocket::AsyncWebSocket>> peerById;
You also need an oatpp::async::Executor
to run your coroutines.
And you have to call sendOneFrameTextAsync
from Coroutine.
Here is the code snippet from the room-char example:
void Peer::sendMessage(const oatpp::String& message) {
class SendMessageCoroutine : public oatpp::async::Coroutine<SendMessageCoroutine> {
private:
oatpp::async::Lock* m_lock;
std::shared_ptr<AsyncWebSocket> m_websocket;
oatpp::String m_message;
public:
SendMessageCoroutine(oatpp::async::Lock* lock,
const std::shared_ptr<AsyncWebSocket>& websocket,
const oatpp::String& message)
: m_lock(lock)
, m_websocket(websocket)
, m_message(message)
{}
Action act() override {
return oatpp::async::synchronize(m_lock, m_websocket->sendOneFrameTextAsync(m_message)).next(finish());
}
};
m_asyncExecutor->execute<SendMessageCoroutine>(&m_writeLock, m_socket, message);
}
For more details please see the Rooms Chat Example
Best Regards,
Leonid
Closing this.