-
-
Save ralphtheninja/74cf32106429d600bf98 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'] | |
var message = process.argv[2] || 'hello world' | |
var socket = utp() | |
socket.on('connection', onconnection) | |
socket.on('listening', function () { | |
var discovery = dc({ | |
dns: { | |
server: SERVER, | |
socket: socket | |
}, | |
dht: { | |
socket: socket | |
} | |
}) | |
discovery.on('peer', function (id, 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