Browser/Object RPC

De DigiWiki.

This "Getting Started" will allow you to talk XML-RPC to Second Life just by using your web browser. A server is not necessary.

In a single HTML page, I provide you with (read Instructions below for accessing it):

  • A ready-to-run, XML-RPC capable SL script that you can attach to any SL object.
  • An HTML page that sends RPC requests to and receives responses from the SL script.
  • Important Tidbits section about XML-RPC in SL (limitations, etc.) to get you going.

I limited the functionality so that you can easily tweak the code until you're on your own two feet.

IMPORTANT: This works only in Internet Explorer right now. An ActiveX object is used to communicate via XML-RPC: you must unblock it if your IE browser blocks it (the default). I'll change it to work with other browsers when I have the time.

Instructions

Simply copy the contents of the html code below into a file in your computer, save it (with an .html extension, of course), and open it in Internet Explorer.

I will update the code and the Important Tidbits section as things change in the SL protocol/limitations.

<html>
<!-- Getting Started Second Life XML-RPC (version 1) -- Ruben Kleiman 11/14/06
     Sample Code
-->
<head>
<title>Getting Started Second Life XML-RPC (ver. 1) -- Ruben Kleiman 11/14/06</title>
<style>
.code {
    background: #EEEEEE;
    border: solid #888888 2px;
    font-family: "Courier New", monospace;
    color: black;
    font-size: 10pt;
    width: 96%;
    max-height: 200px;
    overflow: auto;
    padding: 3px;
    margin: 2px;
    margin-left: 1em;
}
 
.style1 {
	font-size: x-small;
	font-weight: bold;
}
</style>
<script>
// Request object
var request = null;
 
// callRPCScript -- make RPC to SL script
function callRPCScript() {
  try {
 
  var key = document.getElementById('key');
  var stringMessage = document.getElementById('stringMessage');
  var intMessage = document.getElementById('intMessage');
 
  request = getXMLHttpRequest(); 
  if (request == null) {
  	alert("Sorry, you must use Internet Explorer.");
	return;
  }
 
  var xml = '<?xml version=\"1.0\"?><methodCall><methodName>llRemoteData</methodName><params><param><value><struct><member><name>Channel</name><value><string>'+key.value+'</string></value></member><member><name>IntValue</name><value><int>'+intMessage.value+'</int></value></member><member><name>StringValue</name><value><string>'+stringMessage.value+'</string></value></member></struct></value></param></params></methodCall>';
  alert("Sending: "+xml);
 
  request.open('POST', 'http://xmlrpc.secondlife.com/cgi-bin/xmlrpc.cgi', true);
  request.onreadystatechange = getHttpResponse;
  request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
  request.setRequestHeader('Cache-Control', 'no-cache');
  request.send(xml);
  } catch (err) {
    alert("XML-RPC Error: "+err.message);
    return;
  }
}
 
// getHttpResponse -- get RPC response from SL script
function getHttpResponse() {
  if (request.readyState == 4) {
  	alert("Response Status: "+request.status+" "+request.statusText);
  	alert("Response Text:"+request.responseText);
  }
}
 
// getXMLHttpRequest -- create a standard Http connection object
function getXMLHttpRequest() {
	if (request != null) return request; // re-use
	if (typeof XMLHttpRequest != 'undefined')
		{ return new XMLHttpRequest(); } 
	try { return new ActiveXObject("Msxml2.XMLHTTP"); } 
	catch (err1) { 
		try { return new ActiveXObject("Microsoft.XMLHTTP");
		} catch (err2) {
			// ignore
		}
	}
	return false;
}
</script>
</head>
<body>
<p><b>Getting Started With XML-RPC In Second Life</b></p>
<p class="style1">Ruben Kleiman (SomeScripter) 11/14/06</p>
<p>This page will help you to quickly get started sending and receiving XML requests in Second Life.</p>
<p>Copy the HTML contents of this page into a file in your own computer and run it from there.</p>
<p><em><strong>Important</strong>: </em>This works only in Internet Explorer. Why? For all other parental-guidance-required browsers, I would need to use digital signatures or gruesome hacks that defeat it--which, unfortunately, is a waste of time for me. </p>
<p>The way it works is as follows (detailed instructions <a href="#details">here</a>):</p>
<ul>
  <li>You create an object in Second Life with a script provided here.</li>
<li>This HTML page  sends a request to that object.</li>
<li>The Second Life object replies to the request.</li>
<li>This HTML page displays the data in the object's reply. </li>
</ul>
<p><b><a name="details"></a>Detailed Instructions</b></p>
<p>First, in Second Life, do the following:</p>
<ol>
<li>Create some object (any type or size will do).</li>
<li>For the sake of reference in what follows, name the object MyRCPObject. The name doesn't really matter.</li>
<li>Create a script for the object.</li>
<li>Replace the default script in MyRCPObject with  this script:</li>
<div class="code"><pre><span style="color: #FF7700;">
string EXPECTED_REQUEST_STRING_MESSAGE = "hello there";
string BAD_RESPONSE_STRING_MESSAGE = "I do not understand what you said";
string GOOD_RESPONSE_STRING_MESSAGE = "Hi.";
key myChannel;
 
default {
  state_entry() {
    llOwnerSay("Touch me to start listening for XML-RPC requests.");
  }
 
  touch_start(integer n) {
    llOpenRemoteDataChannel();
    llOwnerSay("Waiting for channel to open");
  }
 
  remote_data(integer type, key channel, key uid, string from, integer integerValue, string stringValue) {
    if (type == REMOTE_DATA_CHANNEL) {
      myChannel = channel;
      llOwnerSay("Channel Open. Channel Key="+(string)channel);
      state waiting;
    }
  }
}
 
state waiting {
 
  state_entry() {
    llOwnerSay("Waiting for XML-RPC Requests...");
  }
 
  remote_data(integer type, key channel, key uid, string from, integer integerValue, string stringValue) {
    if (type == REMOTE_DATA_REQUEST) {
      if (stringValue == EXPECTED_REQUEST_STRING_MESSAGE) {
    // uid is the identifier for the request message.
    llRemoteDataReply(channel, uid, GOOD_RESPONSE_STRING_MESSAGE, 1);
      } else {
    llRemoteDataReply(channel, uid, BAD_RESPONSE_STRING_MESSAGE, 0);
      }
    } else {
      // Code would go here to respond to a new open channel, etc...
      // Check the options for 'type' in the LSL Wiki.
    }
  }
 
  touch_start(integer n) {
    state default;
  }
 
  state_exit() {
    llCloseRemoteDataChannel(myChannel);
    llOwnerSay("Channel closed. Touch again to start listening on channel.");
  }
}
</span></pre></div>
<li>What does this script do? It listens for an XML-RPC request and checks whether its string message is 'hello there'. If it is, it sends a response to the browser with a string message 'Hi.' Otherwise, it sends a response with a string message 'I do not understand what you said'.</li>
<li>Save the script and follow its instructions.</li>
</ol>
<p>Finally, fill-in the <a href="#form">form below</a>:</p>
<ol>
<li>Enter the key of MyRCPObject's channel. This key would have been printed by MyRCPObject on the chat line after you touch that object. You can use copy/paste from the chat history. You can click on the 'Talk With...' button now and see what happens; else, read the following steps. </li>
<li>Leave the String Message and Integer Message as they are.</li>
<li>Click on the 'Talk With XML-RPC Script' button.</li>
<li>After you click on the button, an XML-RPC request will be sent to MyRCPObject, but, before sending it, an alert box will show you the contents of the XML request. Click OK on the alert box to allow the request to be sent.</li>
<li>The browser will wait for a response from MyRCPObject. When it arrives, an alert box will show a status code: if is equal to 200, then the request has succeeded (it has found the server and the response has been answered by MyRCPObject. Click OK on the alert box.</li>
<li>Finally, another alert box will show you the XML document returned in the response by MyRCPObject. If you look hard at the XML document, you'll notice that the string "Hi." has been returned in the response.</li>
</ol>
<p>For more details, read this page's HTML code; in particular, the callRPCScript() script, which sends the message and receives the response. </p>
<p><strong>Important Tidbits (valid 11/14/06):</strong></p>
<ul>
  <li>You can't initiate an XML-RPC request to a server from Second Life. You can only respond to requests.</li>
<li>You can communicate with a Second Life object when the owner is logged off.</li>
<li>Once you create an object, the channel key string appears to persist for the object between channel deactivation and activation.</li>
<li>The string value in an RPC response is limited to about 255 characters.</li>
<li>There's a minimum response latency from SL servers for RPC calls of about 3 seconds.</li>
</ul>
<a name="form"></a>
<hr/>
<table style='background-color: #00CCFF;' width='400'>
<tr><td>Channel's Key: </td><td><input type='text' id='key' size='40' maxlength='50' value='b46c0c9b-9824-a1bb-bc2a-2853b49648f2' /></td></tr>
<tr><td>String Message: </td><td><input type='text' name='stringMessage' id='stringMessage' size='40' maxlength='255' value='hello there' /></td></tr>
<tr><td>Integer Message: </td><td><input type='text' name='intMessage' id='intMessage' size='40' maxlength='10' value='32' /></td></tr>
<tr><td colspan='2' align='center'><input onclick='callRPCScript()' type='button' value="Talk With XML-RPC Script" /></td></tr>
</table>
</body>
</html>
Outils personnels
  • Cette page a été consultée 725 fois.
donate
Google Ads