-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
172 additions
and
105 deletions.
There are no files selected for viewing
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
const {PubSub} = require('..')//.debug() // call debug() if you want debugging messages printed to the cli | ||
const simulator = require('hyperspace/simulator') | ||
const sodium = require('sodium-universal') | ||
const os = require('os') | ||
|
||
console.log('# HyperPubSub private message example') | ||
console.log('# Usage: start this script on one device, copy the public key to the other and start the script on the other device with this key as argument') | ||
|
||
simulator().then(async ({client}) => { | ||
const pubsub = new PubSub(client.network, {application: 'example app', onError: console.error}) | ||
|
||
const publicKey = Buffer.alloc(sodium.crypto_box_PUBLICKEYBYTES) | ||
const secretKey = Buffer.alloc(sodium.crypto_box_SECRETKEYBYTES) | ||
sodium.crypto_box_keypair(publicKey, secretKey) | ||
console.log('Public Key is: ' + publicKey.toString('hex')) | ||
|
||
pubsub.subPrivateMsg(publicKey, secretKey, (msgBuf, app, peer) => { | ||
console.log('received "' + msgBuf.toString('utf-8') + '" from ' + peer.remoteAddress) | ||
}, true) | ||
|
||
if(process.argv.length > 2) { | ||
const otherPubKey = Buffer.from(process.argv[2], 'hex') | ||
const msg = Buffer.from('hello from ' + os.userInfo().username + ' @ ' + os.hostname, 'utf-8') | ||
await pubsub.joinPublicKey(otherPubKey) | ||
|
||
send() | ||
setInterval(send, 1000) | ||
|
||
function send() { | ||
console.log('sending message: "' + msg.toString('utf-8')+'"') | ||
pubsub.pubPrivateMsg(otherPubKey, msg) | ||
} | ||
} | ||
}) |
Oops, something went wrong.