SeeMySites.net Forum Index
FAQFAQ    SearchSearch
SeeMySites.net
Discussion and support for our stuff.
 
SeeMySites Home
Update

 
Post new topic   Reply to topic    SeeMySites.net Forum Index -> SendReceive
View previous topic :: View next topic  
Author Message
jeremy2
Site Admin


Joined: 04 Aug 2006
Posts: 48

Post subject: Update     PostPosted: Tue Oct 03, 2006 3:05 pm  Reply with quote

The SendReceive file in the (included in LightLoader) has received a shot in the arm. Let me explain the basic use for this file.

I designed SendReceive to simplify the process of using JSON to send/receive information (which is a lot easier and less resource intensive than XML). Basically there is a "request" object and a "reply" object sent. The request object would contain parameters and data in the form of variables, like so:

//JavaScript snippet

var sender = new SendReceive();
var request = {}; // Shorthand for creating a new object.
request.get = '/index.html';


//End JavaScript snippet

In this simplified example, the script that the object is being passed to could look for the variable "get" and then send back the text from the page www.seemysites.net.

In order to send a request, you make a SendReceive object and a request object (see example above).


There are two ways to send data: synchronous and asynchronous. Synchronous means that the script has to wait for a reply, while asynchronous means that a function will be called when the request is received and other things can be done by the script. Usually people only want asynchronous callbacks, but occassionaly a synchronous call is necessary (such as when a certain order has to be followed).

To change from being asynchronous (the default) to synchronous, change the "async" variable on your sender object to false (e.g., sender.async = false;). Please note at this time that if you define a "receive" function, that the function will not be called (I will update it later), but the object returned will be available at sender.response.

There are two ways to define a function that will receive the response object. You can define one like regular:
function getResponse(response) {
...
}
And then pass it as an argument when you send the request, like so:
sender.sendRequest('/forum/somefile.html', request, getResponse);

The other way is to assign the "receiver" attribute of the sender object like so:
sender.receiver = function(response) {
...
};

So that is basically it. You call the remote script by using:
sender.sendRequest(URL of script, request object, receiver function (possibly optional), quiet (optional)) and then receive an object back from the remote script at your defined function. The quiet objective set to true will make it so that an alert is not displayed, but will pass a variable named "status" back the function that will be something other than "ok". You can check for this by checking response.status in the example function above. The err_msg variable will contain a text error message.
Back to top
View user's profile Send private message Visit poster's website
jeremy2
Site Admin


Joined: 04 Aug 2006
Posts: 48

Post subject:     PostPosted: Tue Oct 03, 2006 3:19 pm  Reply with quote

Now for the fun part. Also included in SendReceive is the ability to send a request every n milliseconds. This is uses many of the same mechanisms as the SendReceive object. You define this object as follows:

repeater = RepeatGetAction(URL, request object, interval (in milliseconds), success function, fail function, stop on fail (boolean));

This is pretty basic. The success function will be called when the request goes through successfully and the script does not return a status parameter of "error". The fail function will be called when the status parameter is "error." If there is an error with the request itself, a message is displayed to the user and the requests are stopped. You can stop the requests yourself by calling repeater.stop(). The stop on fail parameter has not been implemented yet. You can also call the stop() function within the success or fail functions by calling this.stop(). This little object is the JavaScript basis for the LightLoader file upload progress script.

I would suggest reading more about JSON at http://www.json.org to learn how you can implement it in your programming.
Back to top
View user's profile Send private message Visit poster's website
Display posts from previous:   
Post new topic   Reply to topic    SeeMySites.net Forum Index -> SendReceive All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum

Powered by phpBB © 2001, 2005 phpBB Group