Rank: Administration
Groups: Translators, Members, Administrators Joined: 1/11/2018(UTC) Posts: 1,359 Location: Tampa, FL Thanks: 28 times Was thanked: 419 time(s) in 356 post(s)
|
Use this in your Global Actions > Load/Unload > Load script tab to create a listener which awaits POST requests and responds. NOTE: In my example below, I'm using the host name for my computer, so change accordingly! Also see comments about granting URL access and opening the ports. Code:function CreateHTTPListener(prefix) {
// Create a listener.
sp.StoreObject("HTTPListener", new clr.System.Net.HttpListener());
var listener = sp.GetStoredObject("HTTPListener");
// Add the prefix, e.g. "Http://localhost:8080/" (must end with /)
listener.Prefixes.Add(prefix);
// Start listening
listener.Start();
// Note: The GetContext method blocks while waiting for a request.
var context = listener.GetContext();
// Handle when S+ reloaded, to prevent error message when listener stopped
try{
// Here is after a request is received
var request = context.Request;
// Create stream to receive request body
var body = host.newVar(clr.System.IO.Stream);
body = request.InputStream;
var data = new clr.System.IO.StreamReader(body, request.ContentEncoding)
// Obtain a response object.
var response = context.Response;
var jsonin = JSON.parse(data.ReadToEnd());
sp.MessageBox(jsonin.Message, jsonin.Title);
// Construct a response, below is just echoing the entire body of the POST request
var responseString = '{ "Status": "OK" }';
var buffer = clr.System.Text.Encoding.UTF8.GetBytes(responseString);
response.ContentLength64 = buffer.Length;
response.Close(buffer, true);
// Seemed to need a delay before closing the connection, research further
// Would result in random valid response, or connection reset without sleep
sp.Sleep(200);
listener.Stop();
}
catch(err)
{
sp.MessageBox(err.message, "err");
}
// Create a new timer to wait for the next request
// To allow the ability to bind to the prefix below, I ran this in an admin CMD prompt:
// netsh http add urlacl url="http://rob-sager:8081/" user=everyone
// You can use localhost without needing to do this
// Use Windows Firewall to allow connections to port 8081 from other computers
sp.CreateTimer("HTTPListenerTimer", 0, -1, `CreateHTTPListener('${prefix}');`);
}
if(__spEngineWrapper.Engine.Name == sp.EngineList().Last().Engine.Name)
{
//Only create one instance of an HTTP Listener, close and restart on reload
if(sp.GetStoredObject("HTTPListener").GetType().ToString() == "System.Net.HttpListener")
{
try {
sp.DeleteTimer("HTTPListenerTimer");
sp.GetStoredObject("HTTPListener").Stop();
sp.GetStoredObject("HTTPListener").Close();
sp.DeleteStoredObject("HTTPListener")
}
catch {}
}
// Create time thread which waits for a request
sp.CreateTimer("HTTPListenerTimer", 0, -1, "CreateHTTPListener('http://rob-sager:8081/');");
}
I used Postman to test, sending the JSON below as the Raw Body content via POST: Code:{
"Title": "Title Text",
"Message": "Your message here"
}
Which resulted in the response below (after showing a message box): Edited by user Saturday, April 10, 2021 9:54:01 PM(UTC)
| Reason: Not specified
|