Skip to content

Commit

Permalink
feat: add observe example using californium server
Browse files Browse the repository at this point in the history
  • Loading branch information
JKRhb committed Apr 24, 2022
1 parent 3d83839 commit 6f2a541
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions examples/observe_eclipse.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
const coap = require('../') // or coap

const statusRequest = coap.request({
method: 'GET',
host: 'californium.eclipseprojects.io',
pathname: 'obs',
observe: true,
confirmable: true
})

let responseCounter = 0

statusRequest.on('response', res => {
res.on('error', err => {
console.error('Error by receiving: ' + err)
this.emit('error', 'Error by receiving: ' + err)
res.close()
})

res.on('data', chunk => {
console.log(`Server time: ${chunk.toString()}`)
responseCounter++
if (responseCounter >= 5) {
console.log('Successfully received five responses. Closing the ObserveStream.')
res.close()
}
})
})
statusRequest.end()

0 comments on commit 6f2a541

Please sign in to comment.