-
Notifications
You must be signed in to change notification settings - Fork 0
/
Server.hx
71 lines (57 loc) · 1.62 KB
/
Server.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
// define a typed remoting API
class ClientApiImpl extends haxe.remoting.AsyncProxy<ClientApi> {}
// our client class
class ClientData implements ServerApi
{
var api : ClientApiImpl;
var name : String;
public function new( scnx : haxe.remoting.SocketConnection ) {
api = new ClientApiImpl(scnx.client);
(cast scnx).__private = this;
}
public function identify( name : String ) {
if( this.name != null )
throw "You are already identified";
this.name = name;
Server.clients.add(this);
for( c in Server.clients ) {
if( c != this )
c.api.userJoin(name);
api.userJoin(c.name);
}
}
public function say( text : String ) {
for( c in Server.clients )
c.api.userSay(name,text);
}
public function leave() {
if( Server.clients.remove(this) )
for( c in Server.clients )
c.api.userLeave(name);
}
public static function ofConnection( scnx : haxe.remoting.SocketConnection ) : ClientData {
return (cast scnx).__private;
}
}
// server loop
class Server {
public static var clients = new List<ClientData>();
static function initClientApi( scnx : haxe.remoting.SocketConnection, context : haxe.remoting.Context ) {
trace("Client connected");
var c = new ClientData(scnx);
context.addObject("api",c);
}
static function onClientDisconnected( scnx ) {
trace("Client disconnected");
ClientData.ofConnection(scnx).leave();
}
static function main() {
var host = "localhost";
var domains = [host];
var s = new neko.net.ThreadRemotingServer(domains);
s.initClientApi = initClientApi;
s.clientDisconnected = onClientDisconnected;
trace("Starting server...");
s.run(host,1024);
}
}