Skip to content

Commit

Permalink
优化服务端事件处理
Browse files Browse the repository at this point in the history
  • Loading branch information
lanthora committed Mar 11, 2024
1 parent ad01951 commit 2871326
Showing 1 changed file with 3 additions and 6 deletions.
9 changes: 3 additions & 6 deletions src/websocket/server.cc
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@ class WebSocketHandler : public Poco::Net::HTTPRequestHandler {
}
void handleRequest(Poco::Net::HTTPServerRequest &request, Poco::Net::HTTPServerResponse &response) {
std::shared_ptr<Poco::Net::WebSocket> ws = std::make_shared<Poco::Net::WebSocket>(request, response);
ws->setReceiveTimeout(this->server->timeout);
ws->setReceiveTimeout(Poco::Timespan(std::chrono::seconds(this->server->timeout)));

char buffer[1500] = {0};
int length = 0;
int flags = 0;

while (true) {
while (this->server->running) {
try {
length = ws->receiveFrame(buffer, sizeof(buffer), flags);
int frameOp = flags & Poco::Net::WebSocket::FRAME_OP_BITMASK;
Expand Down Expand Up @@ -56,10 +56,6 @@ class WebSocketHandler : public Poco::Net::HTTPRequestHandler {
}

} catch (Poco::TimeoutException const &e) {
if (!this->server->running) {
ws->close();
break;
}
continue;
} catch (std::exception &e) {
WebSocketMessage msg;
Expand All @@ -71,6 +67,7 @@ class WebSocketHandler : public Poco::Net::HTTPRequestHandler {
}
spdlog::debug("unknown websocket request: length {} flags {}", length, flags);
}
ws->close();
}

private:
Expand Down

0 comments on commit 2871326

Please sign in to comment.