首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用shh.subscribe

如何使用shh.subscribe
EN

Ethereum用户
提问于 2019-10-03 08:18:22
回答 1查看 221关注 0票数 0

我想使用web3-shh功能。我使用了来自readthedocs(web3-shh)的例子。除了订阅操作之外,一切都正常工作。它会引发以下错误:

代码语言:javascript
复制
UnhandledPromiseRejectionWarning: Error: Subscriptions are not supported with the HttpProvider.

知道怎么处理这事吗?我使用的测试程序是

代码语言:javascript
复制
import Web3            from 'web3';
const uri = process.env.ETH_NODE_URI || 'http://localhost:8545';
var web3 = new Web3(new Web3.providers.HttpProvider(uri));

var identities = [];

Promise.all([
web3.shh.newSymKey().then((id) => {identities.push(id);}),
web3.shh.newKeyPair().then((id) => {identities.push(id);})
]).then(() => {
var subscription = web3.shh.subscribe('messages', {
    symKeyID: identities[0],
    topics: ['0xffaadd11']
}).on('data', console.log);
}).then(() => {
web3.shh.post({
    symKeyID: identities[0], // encrypts using the sym key ID                                                  
    sig: identities[1], // signs the message using the keyPair ID                                              
    topic: '0xffaadd11',
    payload: '0xffffffdddddd1122'
}).then(h => console.log(`Message with hash ${h} was successfuly sent`))
.catch(err => console.log("Error: ", err));
});

我希望就如何处理这个问题提出任何建议。文档中的完整示例也是一个好主意。

EN

回答 1

Ethereum用户

回答已采纳

发布于 2019-10-03 11:39:10

正如Sanjay上面所指出的,您需要websocket才能执行其实时通知传入消息的功能。

下面是一个如何创建Websocket (使用ethjs,但web3.js类似) https://github.com/invisible-college/democracy/blob/master/packages/whisper/src/whisper.js#L23的示例

如果您运行您自己的语音节点,如geth或状态-go实现(我鼓励您这样做,因为其他人的私语节点将能够窥探您的键盘),您应该使用--wsorigins="mychat"标志启动它,只要您在客户端中创建websocket提供程序时选择相同的值,"mychat“就可以是任何内容。

票数 0
EN
页面原文内容由Ethereum提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://ethereum.stackexchange.com/questions/76454

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档