LSL HTTP server/examples: Difference between revisions
Jump to navigation
Jump to search
Kelly Linden (talk | contribs) |
Kelly Linden (talk | contribs) No edit summary |
||
| Line 6: | Line 6: | ||
state_entry() | state_entry() | ||
{ | { | ||
llRequestURL(); | |||
} | } | ||
| Line 28: | Line 28: | ||
state_entry() | state_entry() | ||
{ | { | ||
llRequestURL(); | |||
} | } | ||
| Line 64: | Line 64: | ||
state_entry() | state_entry() | ||
{ | { | ||
llRequestURL(); | |||
} | } | ||
| Line 117: | Line 117: | ||
</lsl> | </lsl> | ||
=== Url Persistence === | === Url Persistence / Visitor Counter === | ||
A more complete 'hello world', always has an url and keeps a visitor counter. | A more complete 'hello world', always has an url and keeps a visitor counter. | ||
<lsl> | <lsl> | ||
| Line 127: | Line 127: | ||
llSetObjectName("HTTP Server"); | llSetObjectName("HTTP Server"); | ||
url = ""; | url = ""; | ||
llRequestURL(); | |||
hits = (integer)llGetObjectDesc(); | hits = (integer)llGetObjectDesc(); | ||
llSetText((string)hits + " visitors.",<1,1,0>,1); | llSetText((string)hits + " visitors.",<1,1,0>,1); | ||
Revision as of 11:02, 2 September 2008
Hello World!
Classic example, the smallest http_request script possible. <lsl> default {
state_entry()
{
llRequestURL();
}
http_request(key id, string method, string body)
{
if (method == URL_REQUEST_GRANTED)
{
llSay(0,"URL: " + body);
}
else if (method == "GET")
{
llHTTPResponse(id,200,"Hello World!");
}
}
} </lsl> A slightly more robust version: <lsl> default {
state_entry()
{
llRequestURL();
}
http_request(key id, string method, string body)
{
if (method == URL_REQUEST_GRANTED)
{
llSay(0,"URL: " + body);
}
else if (method == URL_REQUEST_DENIED)
{
llSay(0, "Something went wrong, no url. " + body);
}
else if (method == "GET")
{
llHTTPResponse(id,200,"Hello World!");
}
else
{
llHTTPResponse(id,405,"Unsupported Method");
}
}
} </lsl>
Visitor List
A list of residents within sensor range of the server.
Notes:
- It is cheesy and only handles one request at a time. This was just so the script didn't fill out with list management.
<lsl> key request_id = NULL_KEY;
default {
state_entry()
{
llRequestURL();
}
http_request(key id, string method, string body)
{
if (method == URL_REQUEST_GRANTED)
{
llSay(0,"URL: " + body);
}
else if (method == URL_REQUEST_DENIED)
{
llSay(0, "Something went wrong, no url. " + body);
}
else if (method == "GET")
{
if (request_id != NULL_KEY)
{
// This is cheesy.
// Handling multiple requests means list management though.
llHTTPResponse(id,503,"Sorry, busy right now.\n\tTry again soon.");
}
else
{
request_id = id;
llSensor("",NULL_KEY,AGENT,96,PI);
}
}
else
{
llHTTPResponse(id,405,"Unsupported method.");
}
}
no_sensor()
{
llHTTPResponse(request_id, 200, "There is no one here.");
request_id = NULL_KEY;
}
sensor(integer n)
{
string output = "There are " + (string)n + " avatars nearby:";
integer i;
for (i = 0;i<n;++i)
{
output += "\n\t" + llDetectedName(i);
}
llHTTPResponse(request_id,200,output);
request_id = NULL_KEY;
}
} </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_RESTART | 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>