8000 Problem with broadcastPing - always returns true · Issue #242 · Links2004/arduinoWebSockets · GitHub
[go: up one dir, main page]

Skip to content
Problem with broadcastPing - always returns true #242
Closed
@philbowles

Description

@philbowles

I am trying to reduce heap usage as a result of the problem you identified for me earlier (issue #241). I have an issue with lack of notifications when a client disconnects (e.g. netwrok failure). I am trying to resolve this by pinging the client on a timer and then freeing up resources when the client disappears for whatever reason.

I have a 1-second timer that calls broadcastPing() [no params] , but it returns 'true' every time, no matter the status of the client(s) or browser - even when the browser is closed...

If I change my timer to sendPing(0) [ with only one client connected ] it works exactly as expected, returning 0 when the client goes away and 1 when present. I also receive the connect/disconnect events correctly in both cases, but I would like to be able to detect the case when the client disappears and these events do not occur - without broadcastPing() working as expected, I can't see how to do this.

I have looked at the code and it doesn't look right to me - unless I have missed something:

bool WebSocketsServer::broadcastPing(uint8_t * payload, size_t length) {
    WSclient_t * client;
    bool ret = true;
    for(uint8_t i = 0; i < WEBSOCKETS_SERVER_CLIENT_MAX; i++) {
        client = &_clients[i];
        if(clientIsConnected(client)) {
            if(!sendFrame(client, WSop_ping, payload, length)) {
                ret = false;
            }
        }
#if (WEBSOCKETS_NETWORK_TYPE == NETWORK_ESP8266)
        delay(0);
#endif
    }
    return ret;
}

ret never gets set true if there are no clients, only if there appear to be, but one or more doesn't send a reply frame. Shouldn't the broadcast return false if no clients are connected?

Doesn't it make more sense to invert the logic thus:

bool WebSocketsServer::broadcastPing(uint8_t * payload, size_t length) {
    WSclient_t * client;
    bool ret = false;
    for(uint8_t i = 0; i < WEBSOCKETS_SERVER_CLIENT_MAX; i++) {
        client = &_clients[i];
        if(clientIsConnected(client)) {
            if(sendFrame(client, WSop_ping, payload, length)) {
                ret = true;
            }
        }
#if (WEBSOCKETS_NETWORK_TYPE == NETWORK_ESP8266)
        delay(0);
#endif
    }
    return ret;
}

to return false unless at least one client replies?

When I use the modified code, it works exactly as I expected (i.e. same us using sendPing(0);)

Am I missing something?

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      0