Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Only one or some clients are allowed to connect. #34

Open
liu-yabin opened this issue Mar 9, 2022 · 0 comments
Open

Only one or some clients are allowed to connect. #34

liu-yabin opened this issue Mar 9, 2022 · 0 comments

Comments

@liu-yabin
Copy link

For server performance reasons. I need to implement only a specified number of client connections. Here is my WsInstanceListener::onAfterCreate() code:

std::atomic<int32_t> WsInstanceListener::sockets_(0);
std::mutex WsInstanceListener::socket_mutex_;

void WsInstanceListener::onAfterCreate(const oatpp::websocket::WebSocket& socket,
                                       const std::shared_ptr<const ParameterMap>& params) {
    std::lock_guard<std::mutex> guard(socket_mutex_);
    sockets_++;
    OATPP_LOGD(kTag, "New incoming connection. Connection count=%d", sockets_.load());
    auto listener = std::make_shared<WsListener>(socket);
    socket.setListener(listener);
    if (sockets_ >= 2) {
        socket.sendOneFrameText("another connection has already established.");
        socket.sendClose();
    }
}

and get the follow error:

 D |2022-03-09 11:05:34 1646795134469424| WsInstanceListener:New incoming connection. Connection count=1
 D |2022-03-09 11:05:37 1646795137843434| WsInstanceListener:New incoming connection. Connection count=2
 D |2022-03-09 11:05:37 1646795137847096| [oatpp::web::protocol::websocket::WebSocket::listen()]:Unhandled error occurred. Message='[oatpp::web::protocol::websocket::WebSocket::readFrameHeader()]: Error reading frame header'
 D |2022-03-09 11:05:37 1646795137849690| WsInstanceListener:Connection closed. Connection count=1

the message another connection has already established. not sended. Is there a way to implement this function.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant