Skip to content

Instantly share code, notes, and snippets.

@bcomnes
Last active Jan 1, 2020
Embed
What would you like to do?
noise-peer-example
node_modules

This is an example of noise-peer where the end events are properly handled.

const net = require('net')
const noisePeer = require('noise-peer')
const socket = new net.Socket()
socket.connect(8124, () => {
console.log('socket connected')
const peer = noisePeer(socket, true)
peer.on('close', () => {
console.log('noise-peer close')
clearInterval(pinger)
})
peer.on('end', () => {
console.log('noise-peer end')
// peer.end(() => console.log('sending end frame'))
})
peer.on('error', (err) => {
console.error('noise-peer errored')
console.error(err)
})
peer.on('timeout', () => {
console.error('noise-peer timeout')
peer.end(() => console.error('noise-peer ended after timeout'))
})
const pinger = setInterval(() => {
peer.write(Buffer.from('ping'))
}, 1000)
peer.on('data', buf => console.log(buf.toString()))
process.once('SIGINT', quit)
process.once('SIGTERM', quit)
function quit () {
clearInterval(pinger)
peer.end(() => {
console.log('client sent `FINISH` message')
})
}
})
{
"name": "noise-peer-sanity",
"version": "0.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "Bret Comnes <bcomnes@gmail.com> (https://bret.io/)",
"license": "MIT",
"dependencies": {
"noise-peer": "^1.1.0",
"pump": "^3.0.0",
"streamx": "^2.5.0"
},
"devDependencies": {
"standard": "^14.3.1"
}
}
const net = require('net')
const noisePeer = require('noise-peer')
const { Transform } = require('streamx')
const pump = require('pump')
const server = net.createServer((socket) => {
// 'connection' listener.
const client = {
id: Math.random().toString(36).slice(2)
}
console.log(`client ${client.id} connected`)
const peer = noisePeer(socket, false)
peer.on('end', () => {
console.log('noise-peer end')
// peer.end(() => console.log('sending end frame'))
})
peer.on('close', () => {
console.log('noise-peer close')
process.removeListener('SIGINT', endSession)
process.removeListener('SIGTERM', endSession)
})
peer.on('error', (err) => {
console.error('noise-peer errored')
console.error(err)
})
peer.on('timeout', () => {
console.error('noise-peer timeout')
peer.end(console.error('noise-peer ended after timeout'))
})
const upperCaser = new Transform({
transform (buf, cb) {
const str = buf.toString()
console.log(`${client.id}: ${str}`)
cb(null, str.toUpperCase())
}
})
pump(peer, upperCaser, peer)
process.once('SIGINT', endSession)
process.once('SIGTERM', endSession)
function endSession () {
console.error(`ending session for ${client.id}:`)
peer.end(() => console.log(`session for ${client.id} is ended`))
}
})
server.on('error', (err) => {
console.error('server error:')
console.error(err)
})
server.listen(8124, () => {
console.log('server listening on 8124')
})
process.once('SIGINT', quit)
process.once('SIGTERM', quit)
function quit () {
console.log('server is shutting down')
server.close((err) => {
if (err) throw err
console.log('server gracefully shutdown')
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment