forked from yupswing/akifox-asynchttp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Main.hx
101 lines (71 loc) · 3.29 KB
/
Main.hx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
package ;
import com.akifox.asynchttp.*;
class Main {
static function main() {
// The output log is going to be pretty chaotic because of multi-threading
// At the beginning of every line there will be an 8 char string that identify
// the request (and so the thread)
runRequests();
#if sys
var input = Sys.stdin().readLine();
trace('Goodbye!');
#end
}
static function runRequests() {
// --------------------------------------------------------------------------------------------------
// Force log to console (default enabled on -debug)
AsyncHttp.logEnabled = true;
// --------------------------------------------------------------------------------------------------
// NOTE:
// An HttpRequest is mutable until sent
// An HttpResponse is immutable
// This is a basic GET example that shows all the exposed variables
var request = new HttpRequest({
url : "http://www.apple.com",
callback : function(response:HttpResponse) {
if (response.isOK) {
trace('DONE (HTTP STATUS ${response.status})');
} else {
trace('ERROR (HTTP STATUS ${response.status})');
}
}
});
request.send();
// --------------------------------------------------------------------------------------------------
// This is a more complex example
// it is specified an host + a port + a path + a querystring
// but the host does not exists, so it will get a status 0
// (the handler is anonymous)
new HttpRequest({
url : "http://thishostdoesnotexists.com:8080/mypage?field=test&field2=test",
callback : function(response:HttpResponse){
// anonymous response handler
trace(response.fingerprint + " EXAMPLE > Failed request because of host (status: " + response.status + " time: " + response.time + "s)");
}
}).send();
// --------------------------------------------------------------------------------------------------
// This is an example of multiple requests with same response handler
// The order of the responses could be not the same as the order of the requests
// Prepare and send (saving the fingerprint)
var request = new HttpRequest({url:"http://en.wikipedia.org/wiki/Haxe",callback:wikipediaPage});
wikipediaHaxeFingerprint = request.fingerprint;
request.send();
// Send directly
new HttpRequest({url:"http://en.wikipedia.org/wiki/OpenFL",callback:wikipediaPage}).send(); // good
new HttpRequest({url:"http://en.wikipedia.org/wiki/Akifox",callback:wikipediaPage}).send(); // no page (yet)
new HttpRequest({url:"http://en.wiKKipedia.org/wiki/Wikipedia",callback:wikipediaPage}).send(); // wrong host
}
static var wikipediaHaxeFingerprint:String = null;
static function wikipediaPage(response:HttpResponse) {
// check the fingerprint to identify a specific request for this handler
if (wikipediaHaxeFingerprint == response.fingerprint) {
trace(response.fingerprint + ' EXAMPLE > HEY, this was the Haxe Wikipedia page request!');
}
trace(response.fingerprint + " EXAMPLE > function wikipediaPage: " + response.fingerprint + " status: " + response.status + " time: " + response.time);
if(response.isOK) {
trace(response.fingerprint + ' EXAMPLE > Wikipedia: done');
} else {
trace(response.fingerprint + ' EXAMPLE > Wikipedia: error');
}
}
}