'Semaphore Nodejs long polling protocol

I use an esp32 to connect a server. I don't want to use web socket because of the large amount of requests, so i'll use the long polling protocol. Here's the code that i wrote:

kasRouter.get(path + '/json',(req,res)=>{
    sem.take(async function queue() {
        toggle2 = await utils.getToggleKas()
        if(toggle2 == toggle){
            await utils.sleep(1000)
            await queue()
        }else{
            toggle = toggle2
            res.json({
                'kas' : toggle
            })
            sem.leave()
        }
    })
})

the problem is that when the condition turns to true, it doesn't effect on client-side. Here's a look in the client-side script:

void loop() {
    client.begin(link);
    int httpCode = client.GET();
    while(httpCode<=0){
      delay(1000);
      httpCode = client.GET();
    }
    String payload = client.getString();
    payload.replace(" ","");
    payload.replace("\n","");
    payload.trim();
    StaticJsonDocument<256> doc;
    DeserializationError err = deserializeJson(doc,payload);
    if(err){
      return;
    }
    bool kas = doc["kas"];
    if(kas){
      digitalWrite(2,HIGH);
    }else{
      digitalWrite(2,LOW);
    }
    client.end();
}


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source