Figuring out your online status, revisited

// be retrieved by external XML-RPC calls// Global Variableskey avatar;string avatarName;string onlineStatus = "status unknown"; // preserve itkey onlineStatusRequest;key webResponse; // we send the RPC channel ID to the webserverdefault{ state_entry() { llSetTimerEvent(60.0); // call a timer event // every 60 seconds. avatar = llGetOwner(); avatarName = llKey2Name(avatar); llOwnerSay("Registering with RPC server and retrieving user data from database server..."); llOpenRemoteDataChannel(); // this sets the object up to accept external XML-RPC calls } on_rez(integer startParam) { llResetScript(); } timer() { // Every 60 seconds, retrieve the online status from the SL database server onlineStatusRequest = llRequestAgentData(avatar, DATA_ONLINE); llSetText(avatarName + " is " + onlineStatus, ZERO_VECTOR, 1.0); // set the hover text } dataserver(key queryid, string data) { // this is what SL returns to us when we call llRequestAgentData() if (queryid == onlineStatusRequest) // check if it's the correct request { if (data == "1") // online status is just 0 or 1 onlineStatus = "online"; else onlineStatus = "offline"; } } remote_data(integer event_type, key channel, key message_id, string sender, integer idata, string sdata) { if (event_type == REMOTE_DATA_CHANNEL) { // SL has set up a channel for the XML-RPC calls llOwnerSay("Channel id "+(string)channel); llOwnerSay("Sending channel id to web server..."); // We'll save it, by sending it to our server webResponse = llHTTPRequest("http://insert.your.domain.here.tld/save-channel.php", [HTTP_METHOD, "POST", HTTP_MIMETYPE, "application/x-www-form-urlencoded"], "channel=" + (string)channel); } if (event_type == REMOTE_DATA_REQUEST) { // This is a call made from our web site/blog asking us for the online status llRemoteDataReply(channel, message_id, onlineStatus, idata); } } http_response(key request_id, integer status, list metadata, string body) { if (request_id == webResponse) { if (status == 200) // we'll get a 200 OK status if the web server stored the XML-RPC channel correctly llOwnerSay("Channel id successfully saved"); else llOwnerSay("Webserver error " + (string)status + ": " + body); } }}

Now it’s time to take a look at the web-side code. The first bit is the handler for the new channel. I’ve done a quick-and-dirty thingy that simply writes to a local file. Since this is meant to be used inside things like your own hosted blogs/websites/forums, just pick a local directory that is writable and use it. For WordPress, I’m using the uploads directory and the filename I’ve picked was channels.inc. Just copy and paste the following code into a file called save-channel.php where your webserver can find it (remember to place the PHP tags before and after this code, I cannot display them inside WordPress):

if ($_REQUEST['channel'])
{
	include('./wp-config.php');
	$filename = ABSPATH . 'wp-content/uploads/channel.inc';

	$fd = fopen($filename, 'w');
	if (fwrite($fd, $_REQUEST['channel']) === FALSE)
	{
		header("HTTP/1.0 503 Service Unavailable");
		echo "File $filename not writeable - channel not saved";
	}
	else
	{
		fclose($fd);
		header("Content-type: text/plain; charset=utf-8");
		echo "Channel " . $_REQUEST['channel'] . " saved.";
	}
}
else
{
	header("HTTP/1.0 405 Method Not Allowed");
	echo "No channel specified";
}

And finally comes the last bit — adding the relevant code on your site/blog/forum to check if you’re in-world or not. This requires including class-IXR.php, which is available from Incutio. However, the WordPress guys have continued to update this XML-RPC library and have a more recent version.

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