FetchRemote

From Multi Theft Auto: Wiki
Revision as of 05:33, 25 January 2012 by Ccw (talk | contribs) (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...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

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 ] )

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 argument list should match the format of the data returned. The callback function will be passed a string containing "ERROR" followed by an integer indicating the error code when an error occurs calling the function. A list of error codes can be found on the cURL website.

Optional Arguments

  • postData: A string specifying any post data you want to send to the remote HTTP server.
  • postIsBinary : A boolean specifying if the post data is text, or binary.

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 the client:

Click to collapse [-]
Server

function startImageDownload()
    fetchRemote ( "http://www.example.com/image.jpg", gotImage )
end

function gotImage( data, errno )
    if not errno then
        triggerClientEvent("onClientGotImage", resourceRoot, data )
    end
end
Click to collapse [-]
Client
addEvent( "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

Minimum server version 1.3.0-9.03739
Minimum client version n/a

Note: Using this feature requires the resource to have the above minimum version declared in the meta.xml <min_mta_version> section. e.g. <min_mta_version server="1.3.0-9.03739" />

See Also