FetchRemote: Difference between revisions
Jump to navigation
Jump to search
(Created page with "__NOTOC__ {{Server function}} This function allows you to post and receive data from HTTP servers. The calls are asynchronous so you do not get an immediate result from the call...") |
No edit summary |
||
Line 7: | Line 7: | ||
==Syntax== | ==Syntax== | ||
<syntaxhighlight lang="lua"> | <syntaxhighlight lang="lua"> | ||
bool fetchRemote ( string URL, callback callbackFunction, [ string postData = "", bool postIsBinary = false ] ) | bool fetchRemote ( string URL, callback callbackFunction, [ string postData = "", bool postIsBinary = false, [ arguments... ] ] ) | ||
</syntaxhighlight> | </syntaxhighlight> | ||
===Required Arguments=== | ===Required Arguments=== | ||
*'''URL:''' A full URL in the format ''<nowiki>http://hostname/path/file.ext</nowiki>''. A port can be specified with a colon followed by a port number appended to the hostname. | *'''URL:''' A full URL in the format ''<nowiki>http://hostname/path/file.ext</nowiki>''. A port can be specified with a colon followed by a port number appended to the hostname. | ||
*'''callbackFunction:''' This is the function that should receive the data returned from the remote server. The argument list should | *'''callbackFunction:''' This is the function that should receive the data returned from the remote server. The callback argument list should be: | ||
**'''''responseData''''' - A sting containing the remote response or "ERROR" if there was a problem | |||
**'''''errno''''' - A number containing the error number or zero if there was no error. A list of error codes can be found on the [http://curl.haxx.se/libcurl/c/libcurl-errors.html cURL website]. | |||
**'''''arguments...''''' - The arguments that were passed into fetchRemote | |||
===Optional Arguments=== | ===Optional Arguments=== | ||
*'''postData:''' A string specifying any | *'''postData:''' A string specifying any data you want to send to the remote HTTP server. | ||
*'''postIsBinary :''' A boolean specifying if the | *'''postIsBinary :''' A boolean specifying if the data is text, or binary. | ||
*'''arguments:''' Any arguments you may want to pass to the callback. | |||
===Returns=== | ===Returns=== | ||
Line 22: | Line 26: | ||
==Example== | ==Example== | ||
This example shows you how you can fetch an image from a web page, and transfer it to | This example shows you how you can fetch an image from a web page, and transfer it to a particulate client: | ||
<section name="Server" class="server" show="true"> | <section name="Server" class="server" show="true"> | ||
<syntaxhighlight lang="lua"> | <syntaxhighlight lang="lua"> | ||
function startImageDownload() | function startImageDownload( playerToReceive ) | ||
fetchRemote ( "http://www.example.com/image.jpg", | fetchRemote ( "http://www.example.com/image.jpg", myCallback, "", false, playerToReceive ) | ||
end | end | ||
function | function myCallback( responseData, errno, playerToReceive ) | ||
if | if errno == 0 then | ||
triggerClientEvent("onClientGotImage", resourceRoot, | triggerClientEvent( playerToReceive, "onClientGotImage", resourceRoot, responseData ) | ||
end | end | ||
end | end |
Revision as of 06:35, 25 January 2012
This function allows you to post and receive data from HTTP servers. The calls are asynchronous so you do not get an immediate result from the call, instead a callback function you specify is called when the download completes.
In the case when the call fails, a string containing "ERROR" followed by an integer containing the error reason will be passed to the callback function. The reason for failure will be similar to errors found with websites - file not found, server not found and timeouts.
Syntax
bool fetchRemote ( string URL, callback callbackFunction, [ string postData = "", bool postIsBinary = false, [ arguments... ] ] )
Required Arguments
- URL: A full URL in the format http://hostname/path/file.ext. A port can be specified with a colon followed by a port number appended to the hostname.
- callbackFunction: This is the function that should receive the data returned from the remote server. The callback argument list should be:
- responseData - A sting containing the remote response or "ERROR" if there was a problem
- errno - A number containing the error number or zero if there was no error. A list of error codes can be found on the cURL website.
- arguments... - The arguments that were passed into fetchRemote
Optional Arguments
- postData: A string specifying any data you want to send to the remote HTTP server.
- postIsBinary : A boolean specifying if the data is text, or binary.
- arguments: Any arguments you may want to pass to the callback.
Returns
Returns true if the arguments are correct, false otherwise.
Example
This example shows you how you can fetch an image from a web page, and transfer it to a particulate client:
Click to collapse [-]
Serverfunction startImageDownload( playerToReceive ) fetchRemote ( "http://www.example.com/image.jpg", myCallback, "", false, playerToReceive ) end function myCallback( responseData, errno, playerToReceive ) if errno == 0 then triggerClientEvent( playerToReceive, "onClientGotImage", resourceRoot, responseData ) end end
Click to collapse [-]
ClientaddEvent( "onClientGotImage", true ) addEventHandler( "onClientGotImage", resourceRoot, function( pixels ) myTexture = dxCreateTexture( pixels ) end ) addEventHandler("onClientRender", root, function() if myTexture then local w,h = dxGetMaterialSize( myTexture ) dxDrawImage( 200, 100, w, h, myTexture ) end end )
Requirements
This template will be deleted.
See Also
- abortRemoteRequest
- call
- fetchRemote
- getResourceConfig
- getResourceDynamicElementRoot
- getResourceExportedFunctions
- getResourceFromName
- getResourceName
- getResourceRootElement
- getResourceState
- getThisResource
- getRemoteRequests
- getRemoteRequestInfo