SWebService
Set up your web service
To setup your server for communication with space, in the root of your domain, on the port you are using, place a file named 'sinewave.space.scripting.txt' containing 'SPACE_OK'. E.g. http://somewhere.com/sinewave.space.scripting.txt - if this file is not present, you will be unable to use scripting to communicate with the domain. Note: you should use HTTPS for all API calls if you want these to work reliably in WebGL. You may also need to implement a CORS policy in your webserver headers.
Index
Functions Index
Functions
Get
void Get (string url, Closure onComplete, Table headers=null, float timeout=0)
Performs a HTTP[S] GET against URL and returns the contents as a SWebResponse
function OnResponseFunction(responseData)
--
end
Space.WebServices.Get('A URL', OnResponseFunction)
-- Example: Retrieve user-specific data from your integration endpoint
-- Typically retrieved dynamically, e.g., from Space.Scene.CurrentUser
local userID = "User123"
-- Identifier for the integrated service or application
local serviceID = "CRMApp"
-- Build the request URL with query parameters
local sentData = "http://www.yourdomain.net/getIntegrationData.php?id=" .. userID .. "&service=" .. serviceID
-- Callback function to handle the API response
local response = function(data)
if data.Error == nil then
-- Log the successful response
Space.Log(data.Response)
else
-- Log any error returned by the API
Space.Log(data.Error)
end
end
-- Make a GET request to your integration API
Space.WebServices.Get(sentData, response)
-- You can parse the response to extract and use relevant integration data
Post
void Post (string url, string data, Closure onComplete, Table headers=null, float timeout=0)
Performs a HTTP[S] POST against URL using data as a post string and returns the contents as a SWebResponse
function OnResponseFunction(responseData)
--
end
Space.WebServices.Post('A URL', OnResponseFunction)
-- Example: Send user-specific data to your integration endpoint via POST request
-- User identifier, typically retrieved dynamically
local userID = "User123"
-- Identifier for the integrated service or product
local serviceID = "CRMApp"
-- Target URL for the integration API
local url = "http://www.yourdomain.net/postIntegrationData.php"
-- Construct the payload for the POST request
local sentData = "id=" .. userID .. "&service=" .. serviceID
-- Define a callback to handle the API response
local response = function(data)
if data.Error == nil then
-- Log the successful response
Space.Log(data.Response)
else
-- Log any error returned by the API
Space.Log(data.Error)
end
end
-- Set the appropriate content-type for the POST request
local tableHeader = {["Content-Type"] = "text/plain"}
-- Send a POST request to your integration API
Space.WebServices.Post(url, sentData, response, tableHeader)
GetImage
SResource GetImage (string url, Closure onComplete=null, Table header=null, float timeout=0)
Returns a valid SResource for a image on a remote domain that can be used via e.g. SMaterial. While the image loads, it will be a white pixel that will be substituted with the real image once loaded.
resourceImage = Space.WebServices.GetImage('An Image URL')
-- Example: Load and apply a remote image from your integration media server
-- Image filename to retrieve from the media service
local imageFileName = "user_banner_1024.jpg"
-- Base URL of the content delivery server
local mediaServerUrl = "https://cdn.acme-integrations.com/assets/"
-- Reference to the 3D object where the image will be displayed
local displayObject = Space.Host.GetReference("profileDisplay")
-- Fetch the image from the server
local imageResource = Space.WebServices.GetImage(mediaServerUrl .. imageFileName)
-- Apply the image as a texture to the object's material
displayObject.Renderer.Material.SetTexture("_MainTex", imageResource)
Last updated
Was this helpful?