Post by jimmarlowe on Nov 3, 2015 20:23:22 GMT 1
I was trying to figure out if I needed this kind of service in the Anarchy server, and since there are no samples of it, I made one. It reads the urho3d sourceforge page and gleans some information from it.
// This first example, maintaining tradition, prints a "Hello World" message.
// Furthermore it shows:
// - Using the Sample utility functions as a base for the application
// - Adding a Text element to the graphical user interface
// - Subscribing to and handling of update events
// Example of network.MakeHttpRequest() GET - JM
#include "Scripts/Utilities/Sample.as"
String patchInstructions; // sample app needs this, we dont
String urlstr, message = "Hello Http from Urho3D!"; // our globals
HttpRequest@ http = null;
void Start()
{
SampleStart(); // Execute the common startup for samples
CreateText(); // Create "Hello World" Text
SubscribeToEvents(); // Finally, hook-up this HelloWorld instance to handle update events
}
void CreateText()
{
Text@ helloText = Text("helloUrho3d"); // Construct new Text object
message = "Hello Http from Urho3D!"; // Set String to display
helloText.text = message;
helloText.SetFont(cache.GetResource("Font", "Fonts/Anonymous Pro.ttf"), 12 ); // Set font
helloText.color = Color(0.0f, 1.0f, 0.0f); // Set text color
helloText.horizontalAlignment = HA_CENTER; // Align Text center-screen
helloText.verticalAlignment = VA_CENTER;
ui.root.AddChild(helloText); // Add Text instance to the UI root element
}
void SubscribeToEvents()
{
SubscribeToEvent("Update", "HandleUpdate"); // Subscribe HandleUpdate() function for processing update events
}
void HandleUpdate(StringHash eventType, VariantMap& eventData)
{
if ( http is null ) // create and preform the http request
{
const String url = "http://sourceforge.net/projects/urho3d/", verby = "GET", post = "";
Array <String> headr;
http = network.MakeHttpRequest( url, verby, headr, post );
log.Info ( "MakeHttpRequest url "+ url + " headers = " + ( headr.length> 0 ? headr[0] :"no headers" ) + " post = " + post );
message += "\nMakeHttpRequest " + verby + " " + url
+ ", headers = " + ( headr.length> 0 ? headr[0] :"no headers" ) + ", post = " + post;
}
if ( (http !is null) && http.state == HTTP_OPEN ) // collect url data
{
message += "\nhttp connection is open, reading " + String(http.size) + " bytes."; // fyi, size gives really big result.
Text@ mytext = ui.root.GetChild("helloUrho3d");
mytext.text = message;
do
{
urlstr += http.ReadString();
}
while ( http.availableSize > 0 ); // add to string if it gets buffered
message += "\nread " + String(urlstr.length) + " bytes.";
mytext.text = message;
log.Info ( message );
}
if ( (http !is null) && http.state == HTTP_ERROR ) // someone broke the internet, again.
{
log.Error( http.error + ", for url = " + http.url );
message += "\nError " + http.error + "\nPress escape to exit";
Text@ mytext = ui.root.GetChild("helloUrho3d");
mytext.text = message;
input.SetMouseVisible (true); // give the user control back
UnsubscribeFromEvent("Update"); // we are done with http, stop looping
}
if ( (http !is null) && http.state == HTTP_CLOSED ) // we are so done.
{
message += "\nCompleted reading url " + http.url + "\n\n";
// html is so hard to parse, these are hacks
int dlgt = -1, dllt = -1, dlnum = urlstr.Find ( "call-to-action-stats", 0 );
if ( dlnum > -1 ) // we found the offending key for number of downloads
{
message += "\nThe web page says :";
dlgt = urlstr.Find( ">", dlnum );
dllt = urlstr.Find( "<", dlgt );
String cap1 = urlstr.Substring ( dlgt+1, dllt-dlgt-1 ); // grab the text around stats
message += "\n Urho3d has " + cap1 + " this week.";
}
dlnum = urlstr.Find ( "/projects/urho3d/files/latest/download" );
if ( dlnum > -1 ) // we found the offending key for latest download
{
dlgt = urlstr.Find( "Download", dlnum );
dllt = urlstr.Find( "MB", dlgt );
String cap2 = urlstr.Substring ( dlgt, dllt-dlgt+2 ); // grab more text
message += "\n " + cap2;
}
message += "\n\nPress escape to exit\n";
Text@ mytext = ui.root.GetChild("helloUrho3d");
mytext.text = message;
log.Info ( message );
input.SetMouseVisible (true); // give the user control back
UnsubscribeFromEvent("Update"); // we are done with http, stop looping
}
}