Difference between revisions of "LSL HTTP server/examples/fr"

From Second Life Wiki
Jump to navigation Jump to search
(traduction)
 
 
(12 intermediate revisions by one other user not shown)
Line 15: Line 15:
     {
     {
         if (methode == URL_REQUEST_GRANTED)
         if (methode == URL_REQUEST_GRANTED)
        {
             llSay(0, "URL : " + corps);
             llSay(0, "URL : " + corps);
        }
         else if (methode == "GET")
         else if (methode == "GET")
        {
             llHTTPResponse(id, 200, "Bonjour à tous !");
             llHTTPResponse(id, 200, "Bonjour à tous !");
        }
     }
     }
}
}
Line 38: Line 34:
     {
     {
         if (methode == URL_REQUEST_GRANTED)
         if (methode == URL_REQUEST_GRANTED)
        {
             llSay(0, "URL : " + corps);
             llSay(0, "URL : " + corps);
        }
         else if (methode == URL_REQUEST_DENIED)
         else if (methode == URL_REQUEST_DENIED)
        {
             llSay(0, "Quelque chose s'est mal passé, pas d'URL. " + corps);
             llSay(0, "Quelque chose s'est mal passé, pas d'URL. " + corps);
        }
         else if (methode == "GET")
         else if (methode == "GET")
        {
             llHTTPResponse(id, 200, "Bonjour à tous !");
             llHTTPResponse(id, 200, "Bonjour à tous !");
        }
         else
         else
        {
             llHTTPResponse(id, 405, "Méthode pas encore prise en charge.");
             llHTTPResponse(id, 405, "Méthode pas encore prise en charge.");
        }
     }
     }
}
}
</lsl>
</lsl>


=== Visitor List ===
=== Liste de visiteurs ===
A list of residents within sensor range of the server.
 
<br>Notes:
Une liste des visiteurs à portée de détection du serveur.
* This includes a method for handling multiple requests while waiting for asynchronous data requests to come back.
<br>Notes :
<lsl>integer scanning = FALSE;
* Comporte une méthode pour gérer des requêtes multiples alors que l'on attend le retour de requêtes asynchrones.
<lsl>integer detection = FALSE;


list requests;
list requetes;


send_response(string body)
envoie_reponse(string corps)
{
{
    integer l = llGetListLength(requetes);
     integer j;
     integer j;
     for (j = 0; j < llGetListLength(requests); ++j)
 
    {
     for (j = 0; j < l; ++j)
         llHTTPResponse(llList2Key(requests,j), 200, body);
         llHTTPResponse(llList2Key(requetes, j), 200, corps);
    }
 
     requests = [];
     requetes = [];
}
}


Line 82: Line 72:
     }
     }
   
   
     http_request(key id, string method, string body)
     http_request(key id, string methode, string corps)
     {
     {
         if (method == URL_REQUEST_GRANTED)
         if (methode == URL_REQUEST_GRANTED)
        {
             llSay(0, "URL: " + corps);
             llSay(0,"URL: " + body);
         else if (methode == URL_REQUEST_DENIED)
         }
            llSay(0, "Et flûte. " + corps);
         else if (method == URL_REQUEST_DENIED)
         else if (methode == "GET")
         {
         {
            llSay(0, "Something went wrong, no url. " + body);
             if (!detection)
        }
        else if (method == "GET")
        {
             if (!scanning)
             {
             {
                 llSensor("",NULL_KEY,AGENT,96,PI);
                 llSensor("", NULL_KEY,AGENT, 96, PI);
                 scanning = TRUE;
                 detection = TRUE;
             }
             }
           
 
             requests += [id];
             requetes += [ id ];
         }
         }
         else
         else
        {
             llHTTPResponse(id, 405, "Hein ?");
             llHTTPResponse(id,405,"Unsupported method.");
        }
     }
     }
   
   
     no_sensor()
     no_sensor()
     {
     {
         send_response("There is no one here.");
         envoie_reponse("C'est désert ici.");
         scanning = FALSE;
         detection = FALSE;
     }
     }
   
   
     sensor(integer n)
     sensor(integer n)
     {
     {
         string output;
         string rapport;
        if (n < 16) output = "There are " + (string)n + " avatars nearby:";
        else output = "There are at least 16 avatars nearby:";
       
         integer i;
         integer i;
         for (i = 0;i<n;++i)
 
        {
        if (n < 16) rapport = "Il y a " + (string) n + " avatars dans le coin:";
             output += "\n\t" + llDetectedName(i);
        else rapport = "C'est la folie ici, il y a au moins 16 avatars dans le coin.";
        }
 
         for (i = 0; i < n; ++i)
             rapport += "\n\t" + llDetectedName(i);
          
          
         send_response(output);
         envoie_reponse(rapport);
 
         detection = FALSE;
         scanning = FALSE;
     }
     }
}
}
</lsl>
</lsl>


=== Url Persistence / Visitor Counter ===
=== Presistance de l'URL - compteur de visiteurs ===
A more complete 'hello world', always has an url and keeps a visitor counter.
Un "hello world" plus complet, a une URL en permanence et tient à jour un compteur de visiteurs.
<lsl>
<lsl>
string url;
string url;
integer hits;
integer visites;


setup()
initialisation()
{
{
     llSetObjectName("HTTP Server");
     llSetObjectName("Serveur HTTP");
     url = "";
     url = "";
     llRequestURL();
     llRequestURL();
     hits = (integer)llGetObjectDesc();
     visites = (integer)llGetObjectDesc();
     llSetText((string)hits + " visitors.",<1,1,0>,1);
     llSetText((string) visites + " visiteurs.", <1,1,0>, 1);
}
}


default
default
{
{
     state_entry() { setup(); }
     state_entry() { initialisation(); }
     on_rez(integer n) { setup(); }
 
     on_rez(integer n) { initialisation(); }
      
      
     changed(integer c)
     changed(integer c)
     {
     {
         if (c & (CHANGED_REGION | CHANGED_REGION_START | CHANGED_TELEPORT) )
         if (c & (CHANGED_REGION | CHANGED_REGION_START | CHANGED_TELEPORT) )
        {
             initialisation();
             setup();
        }
     }
     }
      
      
     touch_start(integer n)
     touch_start(integer n)
     {
     {
         llSay(0,"My url is: " + url);
         llSay(0, "Mon URL est : " + url);
     }
     }
   
   
     http_request(key id, string method, string body)
     http_request(key id, string methode, string corps)
     {
     {
         if (method == URL_REQUEST_GRANTED)
         if (methode == URL_REQUEST_GRANTED)
            url = corps;
        else if (methode == URL_REQUEST_DENIED)
            llSay(0, "Souci, pas d'URL. " + corps);
        else if (methode == "GET")
         {
         {
            url = body;
             ++visites;
        }
             llSetObjectDesc((string) visites);
        else if (method == URL_REQUEST_DENIED)
             llSetText((string) visites + " visiteurs.", <1,1,0>, 1);
        {
             llHTTPResponse(id, 200, "Bonjour Vous êtes le visiteur numéro " + (string) visites + ".");
            llSay(0, "Something went wrong, no url. " + body);
        }
        else if (method == "GET")
        {
             ++hits;
             llSetObjectDesc((string)hits);
             llSetText((string)hits + " visitors.",<1,1,0>,1);
             llHTTPResponse(id,200,"HelloYou are visitor " + (string)hits + ".");
         }
         }
         else
         else
        {
             llHTTPResponse(id, 405, "Méthode inconnue.");
             llHTTPResponse(id,405,"Method unsupported");
        }
     }
     }
}
}
</lsl>
</lsl>


{{LSLC|HTTP}}{{LSLC|Examples}}
{{LSLC/fr|HTTP}}{{LSLC/fr|Examples}}

Latest revision as of 12:45, 5 November 2009

Hello World!

Exemple classique, le plus petit script à base de http_request possible. <lsl> default {

   state_entry()
   {
       llRequestURL();
   }
   http_request(key id, string methode, string corps)
   {
       if (methode == URL_REQUEST_GRANTED)
           llSay(0, "URL : " + corps);
       else if (methode == "GET")
           llHTTPResponse(id, 200, "Bonjour à tous !");
   }

} </lsl>

Une version légèrement plus robuste : <lsl> default {

   state_entry()
   {
       llRequestURL();
   }
   http_request(key id, string methode, string corps)
   {
       if (methode == URL_REQUEST_GRANTED)
           llSay(0, "URL : " + corps);
       else if (methode == URL_REQUEST_DENIED)
           llSay(0, "Quelque chose s'est mal passé, pas d'URL. " + corps);
       else if (methode == "GET")
           llHTTPResponse(id, 200, "Bonjour à tous !");
       else
           llHTTPResponse(id, 405, "Méthode pas encore prise en charge.");
   }

} </lsl>

Liste de visiteurs

Une liste des visiteurs à portée de détection du serveur.
Notes :

  • Comporte une méthode pour gérer des requêtes multiples alors que l'on attend le retour de requêtes asynchrones.

<lsl>integer detection = FALSE;

list requetes;

envoie_reponse(string corps) {

   integer l = llGetListLength(requetes);
   integer j;
   for (j = 0; j < l; ++j)
       llHTTPResponse(llList2Key(requetes, j), 200, corps);
   requetes = [];

}

default {

   state_entry()
   {
       llRequestURL();
   }

   http_request(key id, string methode, string corps)
   {
       if (methode == URL_REQUEST_GRANTED)
           llSay(0, "URL: " + corps);
       else if (methode == URL_REQUEST_DENIED)
           llSay(0, "Et flûte. " + corps);
       else if (methode == "GET")
       {
           if (!detection)
           {
               llSensor("", NULL_KEY,AGENT, 96, PI);
               detection = TRUE;
           }
           requetes += [ id ];
       }
       else
           llHTTPResponse(id, 405, "Hein ?");
   }

   no_sensor()
   {
       envoie_reponse("C'est désert ici.");
       detection = FALSE;
   }

   sensor(integer n)
   {
       string rapport;
       integer i;
       if (n < 16) rapport = "Il y a " + (string) n + " avatars dans le coin:";
       else rapport = "C'est la folie ici, il y a au moins 16 avatars dans le coin.";
       for (i = 0; i < n; ++i)
           rapport += "\n\t" + llDetectedName(i);
       
       envoie_reponse(rapport);
       detection = FALSE;
   }

} </lsl>

Presistance de l'URL - compteur de visiteurs

Un "hello world" plus complet, a une URL en permanence et tient à jour un compteur de visiteurs. <lsl> string url; integer visites;

initialisation() {

   llSetObjectName("Serveur HTTP");
   url = "";
   llRequestURL();
   visites = (integer)llGetObjectDesc();
   llSetText((string) visites + " visiteurs.", <1,1,0>, 1);

}

default {

   state_entry() { initialisation(); }
   on_rez(integer n) { initialisation(); }
   
   changed(integer c)
   {
       if (c & (CHANGED_REGION | CHANGED_REGION_START | CHANGED_TELEPORT) )
           initialisation();
   }
   
   touch_start(integer n)
   {
       llSay(0, "Mon URL est : " + url);
   }

   http_request(key id, string methode, string corps)
   {
       if (methode == URL_REQUEST_GRANTED)
           url = corps;
       else if (methode == URL_REQUEST_DENIED)
           llSay(0, "Souci, pas d'URL. " + corps);
       else if (methode == "GET")
       {
           ++visites;
           llSetObjectDesc((string) visites);
           llSetText((string) visites + " visiteurs.", <1,1,0>, 1);
           llHTTPResponse(id, 200, "Bonjour !  Vous êtes le visiteur numéro " + (string) visites + ".");
       }
       else
           llHTTPResponse(id, 405, "Méthode inconnue.");
   }

} </lsl>