wiki.sine.space | sinespace

Difference between revisions of "Scripting/SWebService"

From wiki.sine.space
Jump to: navigation, search
(10 intermediate revisions by 3 users not shown)
Line 7: Line 7:
  
 
==Members==
 
==Members==
{{ScriptFunction|void|Get|(string url, Action<SWebResponse> onComplete);|Performs a HTTP[S] GET against URL and returns the contents as a SWebResponse|5 = local value1 = "SomeValue1"<br>
+
 
local value2 = "SomeValue2"<br>
+
It should be noted that the callback function to handle the WebResponse MUST BE DEFINED PRIOR TO CALLING or a script crash will occur. Thanks to Serp Serpton for this highly relevant detail.
local sentData = "http://www.someplace.net/someScript.php?variable1=" .. value1 .. "&variable2=" .. value2<br>
+
 
 +
{{ScriptFunction|void|Get|(string url, Action<SWebResponse> onComplete);|Performs a HTTP[S] GET against URL and returns the contents as a SWebResponse|
 +
5 = ''--For example, you want to get a game data from your domain''
 +
local userID = "AvatarID" ''--You can using Space.Scene.PlayerAvatar to get the current player's info''<br>
 +
local gameID = "GameID" ''--Just an example, like you have more than one game hosted''<br>
 +
local sentData = "http://www.yourdomain.net/getGameData.php?id=" .. userID .. "&game=" .. gameID<br>
 
<br>
 
<br>
 
local response = function(data)<br>
 
local response = function(data)<br>
&nbsp;&nbsp;Space.Log(data.Response)<br>
+
&nbsp;&nbsp;if data.Error == nil then<br>
 +
&nbsp;&nbsp;&nbsp;&nbsp;Space.Log(data.Response)<br>
 +
&nbsp;&nbsp;end<br>
 
end<br>
 
end<br>
 
<br>
 
<br>
webService.Get(sentData, response)<br>
+
'''Space.WebService.Get'''(sentData, response)<br>
-- NOTE: variable1 and variable2 would match Get variables in someScript.php.}}
+
''--Phase the response to get what you need''}}
  
{{ScriptFunction|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|5 = local value1 = "SomeValue1"<br>
+
{{ScriptFunction|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 value2 = "SomeValue2"<br>
+
5 =local userID = "AvatarID"<br>
local url = "http://www.someplace.net/someScript.php"<br>
+
local gameID = "GameID"<br>
local sentData = "variable1=" .. value1 .. "&variable2=" .. value2<br>
+
local url = "http://www.yourdomain.net/getGameData.php"<br>
 +
local sentData = "id=" .. userID .. "&game=" .. gameID<br>
 
<br>
 
<br>
 
local response = function(data)<br>
 
local response = function(data)<br>
&nbsp;&nbsp;Space.Log(data.Response)<br>
+
&nbsp;&nbsp;if data.Error == nil then<br>
 +
&nbsp;&nbsp;&nbsp;&nbsp;Space.Log(data.Response)<br>
 +
&nbsp;&nbsp;end<br>
 
end<br>
 
end<br>
 
<br>
 
<br>
webService.Post(url, sentData, response)<br>
+
'''Space.WebService.Post'''(url, sentData, response)<br>
-- NOTE: variable1 and variable2 would match Post variables in someScript.php.}}
+
''-- Like the example above, just using http(s) post method.''}}
  
{{ScriptFunction|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.}}
+
{{ScriptFunction|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.|5 = function hithere()
 +
  image = "mrlee.jpg"
 +
  server = "https://middleware.systems/"
 +
  obj = Space.Host.GetReference("dispobj")
 +
  resrc = Space.WebServices.GetImage(server .. "mrlee.jpg")
 +
  obj.Renderer.Material.SetTexture("_MainTex", resrc)
 +
end}}
  
 
{{Scripting Navbox}}
 
{{Scripting Navbox}}

Revision as of 02:33, 24 December 2018

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

It should be noted that the callback function to handle the WebResponse MUST BE DEFINED PRIOR TO CALLING or a script crash will occur. Thanks to Serp Serpton for this highly relevant detail.

Get

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

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

--For example, you want to get a game data from your domain

local userID = "AvatarID" --You can using Space.Scene.PlayerAvatar to get the current player's info
local gameID = "GameID" --Just an example, like you have more than one game hosted
local sentData = "http://www.yourdomain.net/getGameData.php?id=" .. userID .. "&game=" .. gameID

local response = function(data)
  if data.Error == nil then
    Space.Log(data.Response)
  end
end

Space.WebService.Get(sentData, response)

--Phase the response to get what you need


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 userID = "AvatarID"

local gameID = "GameID"
local url = "http://www.yourdomain.net/getGameData.php"
local sentData = "id=" .. userID .. "&game=" .. gameID

local response = function(data)
  if data.Error == nil then
    Space.Log(data.Response)
  end
end

Space.WebService.Post(url, sentData, response)

-- Like the example above, just using http(s) post method.


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.

function hithere()
 image = "mrlee.jpg"
 server = "https://middleware.systems/"
 obj = Space.Host.GetReference("dispobj")
 resrc = Space.WebServices.GetImage(server .. "mrlee.jpg")
 obj.Renderer.Material.SetTexture("_MainTex", resrc)
end