-
Notifications
You must be signed in to change notification settings - Fork 13
/
utp.js
36 lines (28 loc) · 947 Bytes
/
utp.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
var utp = require('utp-native')
var pump = require('pump')
var peer = require('..')
// This example uses UTP as the transport protocol, which is a popular choise
// for data heavy applications and p2p. Otherwise this example is equivalent to
// `tcp.js`
// This is the most basic handshake pattern, yielding a "perfect forward secure"
// connection, but with no authentication (in the cryptographic sense) of either
// peer end.
var server = utp.createServer(function onconnection (rawStream) {
var sec = peer(rawStream, false)
pump(sec, sec, function (err) {
if (err) throw err
})
})
server.listen(function () {
var port = server.address().port
var clientRawStream = utp.connect(port)
var clientSec = peer(clientRawStream, true)
clientSec.on('data', function (data) {
console.log(data.toString())
})
clientSec.on('end', function () {
server.close()
})
clientSec.write('Hello world')
clientSec.end()
})