0

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();
}
Bergi
  • 630,263
  • 148
  • 957
  • 1,375
Diaskyyy
  • 29
  • 6
  • "*I don't want to use web socket because of the large amount of requests, so i'll use the long polling protocol.*" - what does that mean? A web socket is *more* efficient that doing lots of polling requests. – Bergi Mar 31 '22 at 20:03
  • Does your server work with other long-polling clients? – Bergi Mar 31 '22 at 20:10
  • i only want that a client waits for a server response, without asking all the time the same request – Diaskyyy Mar 31 '22 at 21:20
  • and there will be only a client – Diaskyyy Mar 31 '22 at 21:21
  • so i limited the semaphore capacity to 1 and every time the client joins, waits for a response and then when got it, waits for the next response. – Diaskyyy Mar 31 '22 at 21:22

0 Answers0