wiki.sine.space | sinespace

Scripting/SWebService

From wiki.sine.space
Revision as of 04:48, 17 February 2017 by Ashasekayi (Talk | contribs)

Jump to: navigation, search

The SWebService class allows you to contact servers you control via HTTP[S] and GET or POST data strings.

Whitelisting

To prevent Space users from unintentionally becoming part of a DDoS botnet, the Space client will first check for a file on a domain before allowing communications with the domain. If this file cannot be found, it will no longer communicate with that domain until the user logs.

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.

Members

Get

void Get (string url, Action<SWebResponse> onComplete);

Performs a HTTP[S] GET against URL and returns the contents as a SWebResponse

local value1 = "SomeValue1"

local value2 = "SomeValue2"
local sentData = "http://www.someplace.net/someScript.php?variable1=" .. value1 .. "&variable2=" .. value2

local response = function(data)
  Space.Log(data.Response)
end

webService.Get(sentData, response)

-- NOTE: variable1 and variable2 would match Get variables in someScript.php.


Post

void Post (string url, string data, Action<SWebResponse> onComplete);

Performs a HTTP[S] POST against URL using data as a post string and returns the contents as a SWebResponse

local value1 = "SomeValue1"

local value2 = "SomeValue2"
local url = "http://www.someplace.net/someScript.php"
local sentData = "variable1=" .. value1 .. "&variable2=" .. value2

local response = function(data)
  Space.Log(data.Response)
end

webService.Post(url, sentData, response)

-- NOTE: variable1 and variable2 would match Post variables in someScript.php.


GetImage

SResource GetImage (string url);

Returns a valid SResource for a image on a remote domain that can be used via e.g. Scripting/SMaterial. While the image loads, it will be a white pixel that will be substituted with the real image once loaded.

local webService = Space.WebServices


-- PresenterCube is a cube object attached to the script in Object References
local presenter = Space.Host.GetReference("PresenterCube")
local meshRenderer = presenter.MeshRenderer

local webImage = webService.GetImage("http://someplace.net/someImage.png")
meshRenderer.material.SetTexture("_MainTex", webImage)

-- Displays webImage on the cube's albedo (_MainTex) slot