Difference between revisions of "LSL HTTP server/examples/fr"
< LSL HTTP server | examples
Jump to navigation
Jump to search
(traduction) |
m (→Visitor List) |
||
Line 57: | Line 57: | ||
</lsl> | </lsl> | ||
=== | === Liste de visiteurs === | ||
Une liste des visiteurs à portée de détection du serveur. | |||
<br>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; | integer j; | ||
for (j = 0; j < | |||
for (j = 0; j < l; ++j) | |||
{ | { | ||
llHTTPResponse(llList2Key( | llHTTPResponse(llList2Key(requetes, j), 200, corps); | ||
} | } | ||
requetes = []; | |||
} | } | ||
Line 82: | Line 85: | ||
} | } | ||
http_request(key id, string | http_request(key id, string methode, string corps) | ||
{ | { | ||
if ( | if (methode == URL_REQUEST_GRANTED) | ||
{ | { | ||
llSay(0,"URL: " + body); | llSay(0,"URL: " + body); | ||
} | } | ||
else if ( | else if (methode == URL_REQUEST_DENIED) | ||
{ | { | ||
llSay(0, " | llSay(0, "Et flûte. " + body); | ||
} | } | ||
else if (method == "GET") | else if (method == "GET") | ||
{ | { | ||
if (! | if (!detection) | ||
{ | { | ||
llSensor("",NULL_KEY,AGENT,96,PI); | llSensor("", NULL_KEY,AGENT, 96, PI); | ||
detection = TRUE; | |||
} | } | ||
requetes += [ id ]; | |||
} | } | ||
else | else | ||
{ | { | ||
llHTTPResponse(id,405," | llHTTPResponse(id, 405, "Hein ?"); | ||
} | } | ||
} | } | ||
Line 110: | Line 113: | ||
no_sensor() | no_sensor() | ||
{ | { | ||
send_response(" | send_response("C'est désert ici."); | ||
detection = FALSE; | |||
} | } | ||
sensor(integer n) | sensor(integer n) | ||
{ | { | ||
string | string affiche; | ||
integer i; | integer i; | ||
for (i = 0;i<n;++i) | |||
if (n < 16) affiche = "Il y a " + (string) n + " avatars dans le coin:"; | |||
else affiche = "C'est la folie ici, il y a au moins 16 avatars dans le coin."; | |||
for (i = 0; i < n; ++i) | |||
{ | { | ||
affiche += "\n\t" + llDetectedName(i); | |||
} | } | ||
send_response( | send_response(affiche); | ||
detection = FALSE; | |||
} | } | ||
} | } |
Revision as of 03:10, 10 May 2009
LSL Portal | Functions | Events | Types | Operators | Constants | Flow Control | Script Library | Categorized Library | Tutorials |
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: " + body); } else if (methode == URL_REQUEST_DENIED) { llSay(0, "Et flûte. " + body); } else if (method == "GET") { if (!detection) { llSensor("", NULL_KEY,AGENT, 96, PI); detection = TRUE; } requetes += [ id ]; } else { llHTTPResponse(id, 405, "Hein ?"); } } no_sensor() { send_response("C'est désert ici."); detection = FALSE; } sensor(integer n) { string affiche; integer i;
if (n < 16) affiche = "Il y a " + (string) n + " avatars dans le coin:"; else affiche = "C'est la folie ici, il y a au moins 16 avatars dans le coin.";
for (i = 0; i < n; ++i) { affiche += "\n\t" + llDetectedName(i); } send_response(affiche);
detection = FALSE; }
} </lsl>
Url Persistence / Visitor Counter
A more complete 'hello world', always has an url and keeps a visitor counter. <lsl> string url; integer hits;
setup() {
llSetObjectName("HTTP Server"); url = ""; llRequestURL(); hits = (integer)llGetObjectDesc(); llSetText((string)hits + " visitors.",<1,1,0>,1);
}
default {
state_entry() { setup(); } on_rez(integer n) { setup(); } changed(integer c) { if (c & (CHANGED_REGION | CHANGED_REGION_START | CHANGED_TELEPORT) ) { setup(); } } touch_start(integer n) { llSay(0,"My url is: " + url); } http_request(key id, string method, string body) { if (method == URL_REQUEST_GRANTED) { url = body; } else if (method == URL_REQUEST_DENIED) { 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,"Hello! You are visitor " + (string)hits + "."); } else { llHTTPResponse(id,405,"Method unsupported"); } }
} </lsl>