-
My browsers (Chrome and Firefox, same result) received broadcast messages about every 4 seconds. Modified from sample broadcast server. typedef uWS::WebSocket<false, true> TWebSocket;
typedef std::list<TWebSocket*> TWebSocketList;
TWebSocketList m_clients;
const int port = 9918;
/* ws->getUserData returns one of these */
struct PerSocketData {
};
uWS::App().ws<PerSocketData>("/*", {
/* Settings */
//.compression = uWS::SHARED_COMPRESSOR,
.compression = uWS::DISABLED,
.maxPayloadLength = 8 * 1024,
.idleTimeout = 64,
/* Handlers */
.open = [](auto *ws, auto *req) {
/* Let's make every connection subscribe to the "broadcast" topic */
LOG_INFO("ws.open: {0:x}", (uint64_t)ws);
ws->subscribe(CH_STATUS);
TheApp->AddClient(ws);
},
.message = [](auto *ws, std::string_view message, uWS::OpCode opCode) {
LOG_INFO("ws.message from {0:x}: {1}", (uint64_t)ws, message);
/* Simply broadcast every single message we get */
//ws->publish("broadcast", message/*, opCode*/);
//TheApp->Broadcast(CH_STATUS, message);
TheApp->HandleCommand(message, ws);
},
.drain = [](auto *ws) {
LOG_INFO("ws.drain");
/* Check getBufferedAmount here */
},
.ping = [](auto *ws) {
LOG_INFO("ws.ping");
},
.pong = [](auto *ws) {
LOG_INFO("ws.pong");
},
.close = [](auto *ws, int code, std::string_view message) {
/* We automatically unsubscribe from any topic here */
LOG_INFO("ws.close: {0:x}", (uint64_t)ws);
TheApp->DelClient(ws);
}
}).listen(port, [](auto *token) {
if (token) {
LOG_INFO("Listening on port: {}", port);
}
}).run(); My broadcast: TWebSocket*& ws = m_clients.front();
LOG_INFO("Publish: {0:x}", (uint64_t)ws);
ws->publish(CH_STATUS, message); Or anything wrong in my code? Thanks. |
Beta Was this translation helpful? Give feedback.
Replies: 8 comments
-
@alexhultman |
Beta Was this translation helpful? Give feedback.
-
Do you publish from another thread? |
Beta Was this translation helpful? Give feedback.
-
@alexhultman |
Beta Was this translation helpful? Give feedback.
-
That's the issue, this library is not thread safe you have to send from the thread that runs the event loop |
Beta Was this translation helpful? Give feedback.
-
Hi, Could you please provide an example on how to interact with the event loop, i.e. adding an event to the queue so that the event loop thread calls uWS::App::publish() itself ? |
Beta Was this translation helpful? Give feedback.
-
The library is absolutely not thread safe. The only function that can be called from any thread is defer:
|
Beta Was this translation helpful? Give feedback.
-
Loop has to be the other threads loop |
Beta Was this translation helpful? Give feedback.
-
I have this problem too, and this lib is very difficult to use... |
Beta Was this translation helpful? Give feedback.
The library is absolutely not thread safe. The only function that can be called from any thread is defer: