Skip to content

how to reject a connection in server side ? #41

Description

@XDinEuro

Hi, I am using oatpp-websocket example from https://github.com/oatpp/example-websocket/blob/master/.

I have one use case, the server need to decline new connection when there exists two client.

I tried to change the code here from

void WSInstanceListener::onAfterCreate(const oatpp::websocket::WebSocket& socket, const std::shared_ptr<const ParameterMap>& params) {

  SOCKETS ++;
  OATPP_LOGD(TAG, "New Incoming Connection. Connection count=%d", SOCKETS.load());

  /* In this particular case we create one WSListener per each connection */
  /* Which may be redundant in many cases */
  socket.setListener(std::make_shared<WSListener>());
}

to

void WSInstanceListener::onAfterCreate(const oatpp::websocket::WebSocket& socket, const std::shared_ptr<const ParameterMap>& params) {
    SOCKETS++;
    OATPP_LOGD(TAG, "New Incoming Connection. Connection count=%d", SOCKETS.load());
    if (SOCKETS >= 3) {
        SOCKETS = 2;
        OATPP_LOGD(TAG, "More than two socket connected, will not listen to new ones");
        socket.sendClose();
    }
    else {
        socket.setListener(std::make_shared<WSListener>());
    }
}

It works properly at first few attemps, but after few times the server stop responding to any connections even though I disconnect the previous two clients.

Another issue is, after I call socket.sendClose(),
WSInstanceListener::onBeforeDestroy() is not triggered. Thus I need to manually do SOCKETS = 2.

Is there is better way to gracefully cut down the connections, or more straghtforward,

Is there a way to reject coming connections ?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions