Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
{
"name": "utp-hole-punching",
"dependencies": {
"utp-native": "*",
"discovery-channel": "*"
}
}
var utp = require('utp-native')
var dc = require('discovery-channel')
var SERVER = ['discovery.publicbits.org:5300', 'discovery.publicbits.org', 'discovery2.publicbits.org:5300']
var message = process.argv[2] || 'hello world'
var socket = utp()
socket.on('connection', onconnection)
socket.on('listening', function () {
var discovery = dc({
dns: {
domain: 'dat.local',
server: SERVER,
socket: socket
},
dht: {
socket: socket
}
})
discovery.on('peer', function (id, peer) {
console.log('found peer', peer)
if (peer.port === socket.address().port) return
var connection = socket.connect(peer.port, peer.host)
onconnection(connection)
})
discovery.add('hello-world-test', 0)
})
socket.listen()
function onconnection (conn) {
conn.setTimeout(60 * 1000, conn.destroy)
conn.write(message)
conn.on('error', function () {})
conn.on('data', function (data) {
console.log(data.toString())
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.