Figuring out your online status in Real World

Second Life has numerous nifty features, and despite some bugs, the urge to create all kinds of stuff has made me eagerly test for each and every one of them.

I wanted to get an “online status” on my web page, and this means using XML-RPC, a standard, open-source interface to inter-application communication. Linden Labs has provided a way for the “outside world” to communicate with scripts in Second Life and even has provided a 2-page description on how this could be accomplished. First, credit where credit’s due. I wouldn’t manage to know how to call XML-RPC from PHP (LL uses a Perl example, but my website runs from inside a PHP content management system) if I didn’t read this thread from Eddy Stryker, which uses a library provided by Keith Devens. You just have to put it somewhere in your web server and call it from there. (Note: You need to make sure that your php.ini configuration file has allow_call_time_pass_reference = On; newer versions of PHP, namely PHP5, have it off by default) Let us set up the object in Second Life. Just use any object at all and put the following script into it:

key avatar;
string online_status; // preserve it

default
{
  state_entry()
  {
    // don't exaggerate, one check per minute
    // is more than enough!
    llSetTimerEvent(60.0);
    llSetTouchText("XML/RPC");
    avatar = llGetOwner();
  }

  timer()
  {
    // now request agent data for online status
    avatar = llGetOwner();
    llRequestAgentData(avatar, DATA_ONLINE);
  }

  dataserver(key queryid, string data)
  {
    // this is the reply you get inside SL
    if (data == "1")
      online_status = "online";
    else
      online_status = "offline";

    // hover the text over your object
    llSetText(llKey2Name(avatar) + " is " + online_status, ZERO_VECTOR, 1.0);
  }

  touch_start(integer total_number)
  {
    // reset XML-RPC communications. Only
    // the owner can do this, and will
    // get a new channel key each time

    if (llGetOwner() == llDetectedKey(0))
    {
      llWhisper(0, "Registering with RPC server...");
      llOpenRemoteDataChannel();
    }
    else llWhisper(0, "Sorry, only" + llKey2Name(avatar)+ " may set up XML/RPC handler on object");
  }

  remote_data(integer event_type, key channel, key message_id, string sender, integer idata, string sdata)
  {
    // this is called from the Outside World
    llWhisper(0, "Incoming information request...");
    if (event_type == REMOTE_DATA_CHANNEL)
    {
      // as channels change, it's good to
      // get it once in a while...
      llWhisper(0, "Channel id: "+(string)channel);

      // send it to you each time it changes
      llEmail("[email protected]", "New channel id for Online Status handler", "New channel id is "+(string)channel);
    }
    if (event_type == REMOTE_DATA_REQUEST)
    {
      llRemoteDataReply(channel, channel, online_status, idata);
    }
  }
}

Notice that the way things work in Second Life, you need to know the channel id to communicate to your object (and this id changes over time, as objects are given away, scripts are reset, etc.). So the best thing is to send the channel id by email!

| | | Next → |
%d bloggers like this: