-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.js
58 lines (48 loc) · 2.08 KB
/
client.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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
var mqtt = require('mqtt')
var readline = require('readline')
var client = mqtt.connect('mqtt://localhost:1883')
var nacl = require('tweetnacl');
var R = require('ramda')
var encrypt = require('./identity.js').encrypt
var decrypt = require('./identity.js').decrypt
nacl.util = require('tweetnacl-util');
const clientIdentity = {
secretKey:"6e3856435a3374507a72317236616f4f6234614c74686d54346c654e4d7150714b7a33564473475a4d626b3d",
publicKey:"526d4b73516b614357533843344f7a342b51785a6847365867666c4e494830514e4c5334377430493856453d"
}
// list of device client can talk to
const devicePublicKey = "653172766e53504738505a51625a7646453337355345394d4367644b54646e486a6733313845444c7858413d"
var rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
let sendingTime = new Date().getTime()
let receivingTime = new Date().getTime()
client.on('connect', function () {
client.subscribe(`${clientIdentity.publicKey}`, function (err) {
if(!err) console.log(`Client listening on ${clientIdentity.publicKey}`)
rl.on('line', (input) => {
console.log(`sending ${input} to device`)
const message = encrypt(input, clientIdentity, devicePublicKey)
// Send messages to the device via it's public key
console.log(`sending message = ${JSON.stringify(message)}`)
client.publish(devicePublicKey, JSON.stringify(message))
sendingTime = new Date().getTime()
})
})
})
client.on('message', function (topic, message) {
console.log(`Received a message on the topic: ${topic}`)
// message is Buffer
const payload = R.compose(JSON.parse, R.map(x => x.toString()))(message)
console.log(`Received payload ${JSON.stringify(payload)} from device`)
if (payload.publicKey !== devicePublicKey){
console.log('Unauthorized message received.');
return
}
// if box is not properly signed, this will throw
const unencrypted = decrypt(payload, clientIdentity.secretKey)
receivingTime = new Date().getTime()
console.log(`Device sent me a message!: ${unencrypted}`)
console.log(`It took ${receivingTime - sendingTime} ms\n`)
})