Skip to content

Instantly share code, notes, and snippets.

@mafintosh
Last active February 21, 2016 23:35
Show Gist options
  • Save mafintosh/731993b5a44103c8bf0c to your computer and use it in GitHub Desktop.
Save mafintosh/731993b5a44103c8bf0c to your computer and use it in GitHub Desktop.
{
"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