Strumenti Utente

Strumenti Sito


programmazione:idee:web:spa_websocket

Differenze

Queste sono le differenze tra la revisione selezionata e la versione attuale della pagina.

Link a questa pagina di confronto

Entrambe le parti precedenti la revisioneRevisione precedente
Prossima revisione
Revisione precedente
programmazione:idee:web:spa_websocket [23/09/2026 16:11] – Fabio Di Matteoprogrammazione:idee:web:spa_websocket [30/09/2026 08:17] (versione attuale) – Fabio Di Matteo
Linea 29: Linea 29:
     const std::string npin =json["pin"].s();     const std::string npin =json["pin"].s();
  
-    disable(conn, "btn_convert");+    disable(conn,pin, "btn_convert");
     if (npin!=pin)     if (npin!=pin)
     {     {
         fmt::println("Invalid pin");         fmt::println("Invalid pin");
-        write_html(conn, "output", "Your pin is wrong!");+        write_html(conn,pin, "output", "Your pin is wrong!");
         return;          return; 
     }     }
Linea 40: Linea 40:
     {     {
         std::string msg = fmt::format("Si richiede di convertire il file {} in {}",file,op);         std::string msg = fmt::format("Si richiede di convertire il file {} in {}",file,op);
-        write_html(conn, "output", msg);+        write_html(conn,pin, "output", msg);
     }else{     }else{
-        write_html(conn,"output","Error! The request operation is wrong!");+        write_html(conn,pin,"output","Error! The request operation is wrong!");
     }     }
-    enable(conn, "btn_convert");+    enable(conn,pin, "btn_convert");
 } }
 +
  
 </code> </code>
Linea 51: Linea 52:
 **Esempio di funzioni helper** **Esempio di funzioni helper**
 <code c++> <code c++>
-void disable(crow::websocket::connection& c ,const std::string& id)+ 
 +void disable(crow::websocket::connection& c, const std::string& pin ,const std::string& id)
 { {
     crow::json::wvalue json;     crow::json::wvalue json;
     json["action"]= "disable";     json["action"]= "disable";
     json["id"]= id;     json["id"]= id;
 +    json["pin"]=pin;
     c.send_text(json.dump());         c.send_text(json.dump());    
 } }
  
-void enable(crow::websocket::connection& c ,const std::string& id)+void enable(crow::websocket::connection& c, const std::string& pin ,const std::string& id)
 { {
     crow::json::wvalue json;     crow::json::wvalue json;
     json["action"]= "enable";     json["action"]= "enable";
     json["id"]= id;     json["id"]= id;
 +    json["pin"]=pin;
     c.send_text(json.dump());         c.send_text(json.dump());    
 } }
  
-void write_html(crow::websocket::connection& c ,const std::string& id, const std::string& html)+void write_html(crow::websocket::connection& c, const std::string& pin ,const std::string& id, const std::string& html)
 { {
     crow::json::wvalue json;     crow::json::wvalue json;
Linea 73: Linea 77:
     json["id"]= id;     json["id"]= id;
     json["html"]=html;     json["html"]=html;
 +    json["pin"]=pin;
     c.send_text(json.dump());         c.send_text(json.dump());    
 } }
 +
  
  
 </code> </code>
 +
 +==== Client ====
 +
 +Il client, anchesso, possiede una funzione che fa da router e alcune funzioni helper per manipolare il DOM. 
 +
 +
 +**router.js**
 +<code javascript>
 +
 +function write_on(id, html) {
 + let t = document.getElementById(id);
 + t.innerHTML = html;
 +}
 +
 +function disable(id){
 +  let t = document.getElementById(id);
 +  t.disabled=true;
 +}
 +
 +function enable(id){
 +  let t = document.getElementById(id);
 +  t.disabled=false;
 +}
 +
 +function add_class(id,cl){
 +  let t = document.getElementById(id);
 +  t.classList.add(cl);
 +}
 +
 +function remove_class(id,cl){
 +  let t = document.getElementById(id);
 +  t.classList.remove(cl);
 +}
 +
 +function append_on(id,html){
 +  let t = document.getElementById(id);
 +  t.insertAdjacentHTML("beforeend",html);
 +}
 +
 +function router(j){
 +  const ob = JSON.parse(j);
 +  // console.log("Action: "+ob.action);
 +  // console.log("Id: "+ob.id);
 +
 +  if (ob.pin!=pin){
 +    write_on("output","Application pin is wrong!");
 +    return;
 +  }
 +  
 +  if (ob.action=="write_html"){
 +    write_on(ob.id, ob.html)
 +  }else if (ob.action=="disable"){
 +    disable(ob.id);
 +  }else if (ob.action=="enable"){
 +    enable(ob.id);
 +  }else if (ob.action=="add_class"){
 +    add_class(ob.id, ob.class);
 +  }else if (ob.action=="remove_class"){
 +    remove_class(ob.id,ob.class)
 +  }else if (ob.action=="append_html"){
 +    append_on(ob.id,ob.html);
 +  }
 +}
 +
 +
 +
 +let ws = new WebSocket("ws://127.0.0.1:8080/ws");
 +
 +ws.addEventListener('open', () => {
 +  console.log("We are connected to server.");
 +});
 +
 +ws.addEventListener('message', (ev) => {
 +    router(ev.data);
 +});
 +
 +ws.addEventListener('error', (ev) => {
 +  console.log(`Error: ${ev.data}`);
 +});
 +
 +ws.addEventListener('close', (ev) => {
 +    console.log(`Connection closed: ${ev.data}`);
 +    ws = null;
 +});
 +
 +
 +</code> 
programmazione/idee/web/spa_websocket.1790172692.txt.gz · Ultima modifica: 23/09/2026 16:11 da Fabio Di Matteo