UDP hole punching service
npm install udpp2p
First, start up server that will try to establish connections between clients:
server = require("udpp2p").createServer({
port: 1338 # listen on port 1338
})
server.start() # lift server
After that, there are two types of topologies between udp clients.
________ client1
/
mainClient(host) ------ client2
\________ client3
While using this type of service, you will need "host" client. everyone will then be connected right to him.
client = require("udpp2p").createClient({
port: 1338
host: "127.0.0.1"
service: {
name: "Service"
type: "single" # when using swarm, use "swarm" instead of single
host: true
}
})
client.on "fail", (request, status, message) ->
console.log "Request[#{request}] failed with status[#{status}] and message[#{message}]"
client.on "connect", (peer) ->
console.log "Peer connected #{peer.host.private.address}:#{peer.host.private.port}"
peer.send { ping: true }
client.on "disconnect", (peer) ->
console.log "Peer disconnected #{peer.host.private.address}:#{peer.host.private.port}"
client.on "data", (peer, data) ->
console.dir data
client.start()
Since host is not enough and you can't have more hosts centric topology, you will need clients:
client = require("udpp2p").createClient({
port: 1338
host: "127.0.0.1"
service: {
name: "Service"
type: "single"
}
})
# use same callbacks as in host
client.start()
In this topology, every client is connected to every client. Because of that, all you need is clients.
client1 ------ client2
\ /
\ /
client3
Example client:
client = require("udpp2p").createClient({
port: 1338
host: "127.0.0.1"
service: {
name: "Service"
type: "swarm" # notice the swarm type of service
}
})
# use same callbacks as in host
client.start()