Last active
February 21, 2016 23:35
-
-
Save mafintosh/731993b5a44103c8bf0c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "utp-hole-punching", | |
"dependencies": { | |
"utp-native": "*", | |
"discovery-channel": "*" | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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