-
Notifications
You must be signed in to change notification settings - Fork 23
/
ThreadedSocketRequest.hx
61 lines (51 loc) · 1.17 KB
/
ThreadedSocketRequest.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
package googleAnalytics;
#if cpp
import cpp.vm.Thread;
#elseif neko
import neko.vm.Thread;
#end
class ThreadedSocketRequest {
#if ( cpp || neko )
private static var thread:Thread;
private static var initted:Bool=false;
public static function init() {
if(initted) return;
initted=true;
thread = Thread.create(onThreadMessage);
}
private static function onThreadMessage(){
var s:sys.net.Socket = null;
var msg:String = null;
while(true){
try {
msg = Thread.readMessage(true);
if ( msg == null ) continue;
var t1:Float = Sys.time();
s = new sys.net.Socket();
s.setTimeout(2);
s.connect(new sys.net.Host("www.google-analytics.com"),80);
s.write(msg);
s.input.readLine();
var t2:Float = Sys.time();
// trace(Math.round((t2-t1)*1000)+"ms ");
} catch(e:Dynamic) {
// trace("Exception: "+e);
}
try {
if(s!=null){
s.close();
s=null;
}
} catch(e:Dynamic) {
// trace("Closing Exception: "+e);
}
}
}
#end
public static function request(url:String, userAgent:String){
#if ( cpp || neko )
init();
thread.sendMessage("GET "+url+" HTTP/1.1\nUser-Agent: "+userAgent+"\n\n");
#end
}
}