Jump to content

Can a lsl script write text to a website using javascript instead of PHP?


Wolfee Yaffle
 Share

You are about to reply to a thread that has been inactive for 4343 days.

Please take a moment to consider if this thread is worth bumping.

Recommended Posts

I've been wanting to create a visitor tracker with a web interface for my land. What I want is a lsl script that will stay inactive until I call on it from the website, then it will send a list of who is on my land to the website. I'm not sure how to script the request button or have the lsl script write info to the website. Also my web hosting plan doesn't support PHP. It's the free hosting plan that comes with your domain if registered from Doteasy. Would I need to upgrade my plan? Or is there a way to do it under javascript? I've been working with a lsl script that reads text from my website, but I'd like one that writes TO it. I don't want the lsl script to remember everyone who came by, just display who is currently there.

Link to comment
Share on other sites

What you would nee is so called HTTP in - a call to the script inworld with the script acting as a webserver. You don't need php for that - javascript can do that . The problem is, that the URL of the server can change - and will under certain cirumstances (read here). So the answer to that part would be: you can, but it's not be a good solution.

What you usually do, is that the in-world server reports it's URL to a php file which in turn stores it. But that would require PHP and some means to store data (either write a file or a database).

 

ETA:

If you're going to update your hosting plan, be aware that having php doesn't mean that you can call your lsl script - most hosting providers block calls to odd ports which LL uses.

Link to comment
Share on other sites

You could use something like this ....

http1 = llHTTPRequest(URL + "&name=" + avname ,[HTTP_METHOD, "POST"],""); // Send the data string to URL

where name is a variable that the script in your external server expects to see, and avname is the value you are passing.

  • Like 1
Link to comment
Share on other sites

The issue Darkie is talking about is that if you want an on demand report from a scripted object in world, you are going to need to know the URL of that scripted object so you can send it the command to send you the report.

As example, lets say you make a button on your website, you press that button.  Then your website sends a command to your prim in world to send you the current residents.  Your prim then sends you the informaiton.

It is the "your website sends a command to your prim" part that is the issue.  To do that, you are going to need to know the URL of the prim in world, and this is where Darkie is warning you of the changing URL of prims.

There was a quick solution that was on the wiki but the author has deleted it.  I will see if I can find it agian.

Anyway, none of this tells you how to actually send the results, which I am sure someone with more LSL http knowledge will chime in with.

edit to add: found the solution on the authors wiki http://was.fm/wiki/Permanent_Primitive_URL

edit to add2: and it looks like while I was typing my reply Rolig gave you pointer on how to do the communication itself :)

 

Link to comment
Share on other sites

In-world objects would always send messages to website and receive messages back in HTTP. On the in-world side the engine assembling HTTP messages to send and processing those received must be written in LSL. On the web page side the processing engine could be written in any language that the web site supports. It could be PHP, it could be C++, it could be Java or it could be Javascript. PHP is used most often only because it is supported by most websites and has convenient methods to talk to databases.

Link to comment
Share on other sites

Well, now you should have about everything you need:

- a 3rd party database so you don't need one as descibed by Talia

- the http_response event to receive requests from your website

To send requests from your website, you can either use a frame that calls your tiny URL (see Talia's posting) that shows the reply on your website or Ajax (see here for an introduction) - the events could be anything like pressing a button on the page, loading the page or a timer. This is standard javascript web programming.

Link to comment
Share on other sites

From the department of diagnostic scripting (an in-joke which you should ignore)

Your javascript needs to make an HTTPRequest to the URL of your SL object (as described by Darkie, et al).  This is the programmed equivalent of a person clicking on a hyperlink to get a web page.  A quick Google-search for "javascript HTTPRequest" finds any number of examples but this is a good illustration:

 

Javascript HTTPRequestfunction httpGet(theUrl){    var xmlHttp = null;    xmlHttp = new XMLHttpRequest();    xmlHttp.open( "GET", theUrl, false );    xmlHttp.send( null );    return xmlHttp.responseText;}

 

In your LSL script you need an http_request() event handler (what to do when someone sends you an HTTPRequest).  The wiki http://wiki.secondlife.com/wiki/Http_request illustrates that nicely.  You need to edit the final 'else' part of the example.  Put there all the processing you want to do (such as firing a sensor to see who's around).  In the sensor() event-handler you assemble the names into a string and return that to your web-site with the llHTTPResponse() function http://wiki.secondlife.com/wiki/LlHTTPResponse.  !! Remember !! If there's no-one around a sensor() event won't be raised, you'll need to handle no_sensor().

 ETA:  Thus, LSL writes TO the web-site in response to a request FROM that web-site.  What you then do with that response and if/how you store it depends on the rest of your site's setup.  The essential difference between PHP and javascript is that PHP runs server-side, while javascript is client-side.  That is, a PHP page is usually an unopened document on the server.  When the server gets a request for that page it opens it, processes it, sends whatever the result is and closes it again.  With javascript you've already opened the page in your browser and are processing it there.  The SL script is the one sitting doing nothing until it gets the request and gives the response.

Link to comment
Share on other sites

Thanks for all the help everyone, but I have absolutely no idea what I'm doing. I don't know any PHP or javascript. I'm trying to learn js but it seems too much work just to send data. I can do the LSL scripting side of things, but the web scripting side is just too confusing. I don't suppose I can request someone to build this for me? Here is what I've done so far. I did use the tinyurl thing btw.

lsl script

default{    state_entry()    {    }    touch_start(integer total_number)    {        llHTTPRequest("http://www.wolfeedarkfang.com/test.html", [HTTP_METHOD,"POST"], "");    }    http_request(key id, string method, string body)    {        llOwnerSay("request - " + body);        //llHTTPResponse(id, 200, body);    }    http_response(key id, integer status, list metadata, string body)    {        llOwnerSay("response - " + body);        //llHTTPResponse(id, 200, body);    }}

You can see I currently have most functions //'d out until i figure out what they do and how it all works. I'm using ownersay so thescript tells me the results. It just spits out the contents of the test.html file though. Nothing else...

 test.html contents

function httpGet(http://tiny.cc/swivs){    var xmlHttp = null;    xmlHttp = new XMLHttpRequest();    xmlHttp.open( "GET", theUrl, false );    xmlHttp.send( null );    return xmlHttp.responseText;}

 I'm sure the web side is done completely wrong, and I have no idea what to do with the info listed. I may just give up.

Link to comment
Share on other sites


Wolfee Yaffle wrote:

I may just give up.

Weeeellll, you aren't going to get it done soon, that's for sure.  On the other hand do you need it enough to pay anyone for it?  If you really need it and don't want to learn programming for any other reason then go ahead - the place to post is in the wanted and/or in-world employment forum.  On the other hand, if you're willing to stick at it and learn you've got two fairly major tasks ahead of you: i) reading all the LSL wiki links I gave you so you can re-do your LSL script, ii) learning about javascript.  You can forget all about PHP, that's completely different to what you're trying to do.

We're here to help with the scripting side.  All I'll say about the javascript is:

Link to comment
Share on other sites

I wouldn't start with Ajax if you know just a little javascript - even more so as cross site Ajax is a little tricky.

But there is a very simple way to do it - just use an embedded frame.

First of all, take the code from the page that Talia posted - make sure you resistered on tiny.cc and generated a key as described there and fill in your username and the key in it.

 //////////////////////////////////////////////////////////// [K] Kira Komarov - 2011, License: GPLv3              //// Please see: http://www.gnu.org/licenses/gpl.html     //// for legal details, rights of fair usage and          //// the disclaimer and warranty conditions.              //////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////                   CONFIGURATION                      ////////////////////////////////////////////////////////////// This is the url extension, ie http://tiny.cc/biose.string CUSTOM_URL = "cschmutz";// Set this to the username you signed up with on // http://tiny.cc.string USER_NAME = "";// Set this to the API key generatred by tiny.cc. To// generate one, visit: http://tiny.cc/api-docs and you// will see Your "API Key:" followed by a key.string API_KEY = "";////////////////////////////////////////////////////////////                     INTERNALS                        //////////////////////////////////////////////////////////// key rlReq = NULL_KEY;key glReq = NULL_KEY;string smURL = ""; default{    state_entry()    {        llSetTimerEvent(5);        llRequestURL();    }    on_rez(integer param) {        llResetScript();    }    timer() {        if(smURL != "") {            rlReq = llHTTPRequest("http://tiny.cc/?c=rest_api&format=json&version=2.0.3&m=shorten&login=" + USER_NAME + "&apiKey=" + API_KEY + "&shortUrl=" + CUSTOM_URL + "&longUrl=" + llEscapeURL(smURL), [HTTP_METHOD, "GET"], "");            llSetTimerEvent(0);            return;        }    }    http_request(key id, string method, string body)    {        if (method == URL_REQUEST_GRANTED) {            smURL = body;            return;        }         if(method == URL_REQUEST_DENIED) {            llResetScript();            return;        }         if(method == "GET") {            llHTTPResponse(id, 200, "This is my response from in-world");            llOwnerSay("sent");            return;        }    }    http_response(key request_id,         integer status,         list metadata,         string body)    {        if (glReq == request_id) {            llHTTPResponse(glReq, 200, "");            return;        }        if(rlReq == request_id) {            if(~llSubStringIndex(body, "1215")) {                glReq = llHTTPRequest("http://tiny.cc/?c=rest_api&format=json&version=2.0.3&m=edit&hash=" + CUSTOM_URL + "&login=" + USER_NAME + "&apiKey=" + API_KEY + "&shortUrl=" + CUSTOM_URL + "&longUrl=" + llEscapeURL(smURL), [HTTP_METHOD, "GET"], "");            }            llHTTPResponse(rlReq, 200, "");            return;        }    }}

 

You just dump that script in your object.

And here is a very simple page that lets you read the response from your object and refresh it with a button - of course you have to put in your tiny url in the href and the src (replace the XXX):

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"       "http://www.w3.org/TR/html4/loose.dtd"><html><head><title>Prim message in an embedded frame</title></head><body><h1>Look through the window</h1><p>The object says:</p><iframe src="http://tiny.cc/XXX" width="90%" height="400" name="MyFrame"></iframe><input type="button" value="Reload" onclick="parent.frames.MyFrame.location.href='http://tiny.cc/XXX';"></body></html>

 

 

Link to comment
Share on other sites

At least this is a reply from SL. It seems, that your inworld script does not update the tiny url - are you sure that you put in the script I posted and followed the steps that are described on the page mentioned? In the script I posted, you have to supply the correct tiny URL and your key from there. I tested it and it works like a charm.

Link to comment
Share on other sites

To be exact - you have to fil in the three values of the script in the config section - I forgot to delete my custom URL there

////////////////////////////////////////////////////////////                   CONFIGURATION                      ////////////////////////////////////////////////////////////// This is the url extension, ie http://tiny.cc/biose.string CUSTOM_URL = "cschmutz";// Set this to the username you signed up with on // http://tiny.cc.string USER_NAME = "";// Set this to the API key generatred by tiny.cc. To// generate one, visit: http://tiny.cc/api-docs and you// will see Your "API Key:" followed by a key.string API_KEY = "";

 

Link to comment
Share on other sites

Hmmm - it's difficult to say where the mistake is. All I can say is, that if you follow the instructions, it works. And the reply you mention shows that you get a connection to SL - just that the URL of the in-world object doesn't get updated properly. I can only assume that there must be something not quite right with your configuration. I can rezz my object and post the page here to show you, if you want - it works.

Link to comment
Share on other sites

The script I am using is the one from here, and I did fill out all 3 things. I have a API key which I added to the right part of the script, I put in my tinycc username, and here is what I put for the url.

string CUSTOM_URL = "swivs";

No matter how many times I reset the script, nothing changes. The script is active. So I have no idea why it isn't working. All I can assume is something broke in the background which I'm unable to fix.

Link to comment
Share on other sites

I decided to put in llOwnerSay("Debug") messages throughout the tinycc script to pinpoint where it stalls at, but I determined it's functioning properly, so I can only assume theres something wrong on either tinycc's side or lindenlabs side. It has to be out of my reach.

Link to comment
Share on other sites

You are about to reply to a thread that has been inactive for 4343 days.

Please take a moment to consider if this thread is worth bumping.

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now
 Share

×
×
  • Create New...