-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTest.hx
135 lines (102 loc) · 2.9 KB
/
Test.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
class Cursor implements hxbit.NetworkSerializable {
@:s var color : Int;
@:s public var uid : Int;
@:s public var x(default, set) : Float;
@:s public var y(default, set) : Float;
var net : Test;
public function new( color, uid=0 ) {
this.color = color;
this.uid = uid;
init();
x = 0;
y = 0;
}
public function networkGetOwner() {
return this;
}
function set_x( v : Float ) {
if( v == x ) return v;
return this.x = v;
}
function set_y( v : Float ) {
return this.y = v;
}
public function toString() {
return "Cursor " + StringTools.hex(color, 6)+(enableReplication?":ALIVE":"");
}
function init() {
net = Test.inst;
enableReplication = true;
net.log("Init "+this);
}
@:rpc function blink( s : Float ) {
}
public function alive() {
init();
// refresh bmp
this.x = x;
this.y = y;
if( uid == net.uid ) {
net.cursor = this;
net.host.self.ownerObject = this;
}
net.log("alive "+this);
}
public function networkAllow(mode:hxbit.NetworkSerializable.Operation, prop:Int, client:hxbit.NetworkSerializable) {
return true;
}
}
//PARAM=-lib hxbit
class Test {
static var HOST = "127.0.0.1";
static var PORT = 6676;
public var host : SocketHost;
//public var event : hxd.WaitEvent;
public var uid : Int;
public var cursor : Cursor;
public function init() {
//event = new hxd.WaitEvent();
host = new SocketHost();
host.setLogger(function(msg) log(msg));
#if server
host.wait(HOST, PORT, function(c) {
log("Client Connected");
});
host.onMessage = function(c,uid:Int) {
log("Client identified ("+uid+")");
var cursorClient = new Cursor(0x0000FF, uid);
c.ownerObject = cursorClient;
c.sync();
};
log("Server Started");
start();
#else
// we could not start the server
log("Connecting");
uid = 1 + Std.random(1000);
host.connect(HOST, PORT, function(b) {
if( !b ) {
log("Failed to connect to server");
return;
}
log("Connected to server");
host.sendMessage(uid);
});
#end
}
public function log( s : String, ?pos : haxe.PosInfos ) {
pos.fileName = (host.isAuth ? "[S]" : "[C]") + " " + pos.fileName;
haxe.Log.trace(s, pos);
}
function start() {
cursor = new Cursor(0xFF0000);
log("Live");
host.makeAlive();
}
public static var inst : Test;
static function main() {
inst = new Test();
inst.init();
}
public function new() {}
}