example nat
This commit is contained in:
29
examples/nat.js
Normal file
29
examples/nat.js
Normal file
@@ -0,0 +1,29 @@
|
||||
// NAT Punchthrough Server
|
||||
// This server helps two chess clients find each other through NAT
|
||||
// The server coordinates the punchthrough by having both clients
|
||||
// connect to each other simultaneously
|
||||
|
||||
var json = use('json');
|
||||
var waiting_client = null;
|
||||
var match_id = 0;
|
||||
|
||||
$_.portal(e => {
|
||||
console.log("NAT server: received connection request");
|
||||
|
||||
if (!is_actor(e.actor))
|
||||
send(e, {reason: "Must provide the actor you want to connect."});
|
||||
|
||||
if (waiting_client) {
|
||||
console.log(`sending out messages! to ${json.encode(e.actor)} and ${json.encode(waiting_client.actor)}`)
|
||||
send(waiting_client, e.actor)
|
||||
send(e, waiting_client.actor)
|
||||
|
||||
waiting_client = undefined
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
waiting_client = e
|
||||
|
||||
console.log(`actor ${json.encode(e.actor)} is waiting ...`)
|
||||
}, 4000);
|
||||
22
examples/nat_client.js
Normal file
22
examples/nat_client.js
Normal file
@@ -0,0 +1,22 @@
|
||||
console.log(`nat client starting`)
|
||||
|
||||
$_.contact((actor, reason) => {
|
||||
if (actor) {
|
||||
console.log(`trying to message ${json.encode(actor)}`)
|
||||
send(actor, {type:"greet"})
|
||||
} else {
|
||||
console.log(json.encode(reason))
|
||||
}
|
||||
}, {
|
||||
address: "108.210.60.32", // NAT server's public IP
|
||||
port: 4000,
|
||||
actor: $_
|
||||
})
|
||||
|
||||
$_.receiver(e => {
|
||||
switch(e.type) {
|
||||
case 'greet':
|
||||
console.log(`hello!`)
|
||||
break
|
||||
}
|
||||
})
|
||||
Reference in New Issue
Block a user