-
-
Save frozeman/fc2a46f861f7995ff8b905e81e9a2902 to your computer and use it in GitHub Desktop.
whisper chat demo
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
var chat = { | |
username: "<not set>", | |
topic: "0xfeedbabe", | |
key: "", | |
identity: "", | |
pollInterval: null, | |
filter: null, | |
setUsername: function(name) { | |
this.username = name; | |
return true; | |
}, | |
join: function(password) { | |
// create key from shared secret | |
this.key = shh.generateSymKeyFromPassword(password); | |
// create pub/priv key for identity | |
this.identity = shh.newKeyPair(); | |
pubKey = shh.publicKey(this.identity); | |
console.log("identity", pubKey.substr(2, 8)); | |
// create message filter (console doesn't support subscriptions) | |
this.filter = shh.newMessageFilter({ | |
symKeyID: this.key, | |
topics: [this.topic], | |
}); | |
// poll for new messages | |
me = this; | |
this.pollInterval = setInterval(function() { | |
me.pollMessages() | |
}, 1000); | |
this.say("joined"); | |
return true; | |
}, | |
pollMessages: function() { | |
messages = shh.getFilterMessages(this.filter); | |
for (i = 0; i < messages.length; i++) { | |
printMessage(messages[i], this.username); | |
} | |
} | |
say: function(text) { | |
message = { | |
symKeyId: this.key, | |
topic: this.topic, | |
payload: web3.toHex(this.username + ": " + text), | |
powTime: 5, | |
powTarget: shh.info.minPow, | |
sig: this.identity | |
}; | |
return shh.post(message); | |
} | |
leave: function() { | |
shh.deleteMessageFilter(this.filter); | |
clearInterval(this.pollInterval); | |
this.say("left...") | |
console.log("chat room left"); | |
return true; | |
}, | |
} | |
// start helper functions | |
function time(message) { | |
date = new Date(message.timestamp * 1000); | |
hours = date.getHours(); | |
minutes = "0" + date.getMinutes(); | |
seconds = "0" + date.getSeconds(); | |
return hours + ':' + minutes.substr(-2) + ':' + seconds.substr(-2); | |
} | |
function sender(message) { | |
return message.sig.substr(2, 8) | |
} | |
function printMessage(message, myUsername) { | |
line = web3.toAscii(message.payload); | |
parts = line.split(":"); | |
name = parts[0] | |
if (name !== myUsername) { | |
console.log("[" + time(message) + " " + name + "/" + sender(message) + "]" + parts.slice(1).join(":")); | |
} | |
} | |
// end helper functions | |
console.log("Supported functions:"); | |
console.log("* chat.setUsername(<name:string>)"); | |
console.log("* chat.join(<password:string>)"); | |
console.log("* chat.leave()"); | |
console.log("* chat.say(<message:string>)"); | |
console.log(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
frozeman/chat.jsforked from bas-vk/chat
whisper chat demo
Raw
chat.js
var chat = { username: "", topic: "0xfeedbabe", key: "", identity: "", pollInterval: null, filter: null, setUsername: function(name) { this.username = name; return true; }, join: function(password) { // create key from shared secret this.key = shh.generateSymKeyFromPassword(password); // create pub/priv key for identity this.identity = shh.newKeyPair(); pubKey = shh.publicKey(this.identity); console.log("identity", pubKey.substr(2, 8)); // create message filter (console doesn't support subscriptions) this.filter = shh.newMessageFilter({ symKeyID: this.key, topics: [this.topic], }); // poll for new messages me = this; this.pollInterval = setInterval(function() { me.pollMessages() }, 1000); this.say("joined"); return true; }, pollMessages: function() { messages = shh.getFilterMessages(this.filter); for (i = 0; i < messages.length; i++) { printMessage(messages[i], this.username); } } say: function(text) { message = { symKeyId: this.key, topic: this.topic, payload: web3.toHex(this.username + ": " + text), powTime: 5, powTarget: shh.info.minPow, sig: this.identity }; return shh.post(message); } leave: function() { shh.deleteMessageFilter(this.filter); clearInterval(this.pollInterval); this.say("left...") console.log("chat room left"); return true; },} // start helper functionsfunction time(message) { date = new Date(message.timestamp * 1000); hours = date.getHours(); minutes = "0" + date.getMinutes(); seconds = "0" + date.getSeconds(); return hours + ':' + minutes.substr(-2) + ':' + seconds.substr(-2);} function sender(message) { return message.sig.substr(2, 8)} function printMessage(message, myUsername) { line = web3.toAscii(message.payload); parts = line.split(":"); name = parts[0] if (name !== myUsername) { console.log("[" + time(message) + " " + name + "/" + sender(message) + "]" + parts.slice(1).join(":")); }}// end helper functions console.log("Supported functions:");console.log("* chat.setUsername(name:string)");console.log("* chat.join(password:string)");console.log("* chat.leave()");console.log("* chat.say(message:string)");console.log();
Copyright © 2018-2045 QDRBTCPSJBDC MICROCHIP SMART CONTRACT.™
All Rights Reserved.