web3.shh.filter
调用web3.ssh.filter()
方法来监听接收的whisper消息。
调用方法:
web3.shh.filter(options[,callback])
参数:
options
:Object,过滤器选项:- topics: 主题字符串数组,用来过滤消息,可以如下组合:
- ['topic1', 'topic2'] == 'topic1' && 'topic2'
- ['topic1', ['topic2', 'topic3']] == 'topic1' && ('topic2' || 'topic3')
- [null, 'topic1', 'topic2'] == ANYTHING && 'topic1' && 'topic2' -> null works as a wildcard
- to: Filter by identity of receiver of the message. If provided and the node has this identity, it will decrypt incoming encrypted messages.
- topics: 主题字符串数组,用来过滤消息,可以如下组合:
callback
:Function ,可选的回调函数,当设置此参数后将采用异步http请求节点旳API
回调函数的返回值:
Object
- 接收到的消息:
- from: String, 60字节,消息发送者
- to: String, 60字节,消息接受者
- expiry: Number,消息过期时间,秒为单位
- ttl: Number,消息存活时间,秒为单位
- sent: Number ,消息发送时间,unix时间戳格式
- topics: Array of String,消息包含的主题字符串数组
- payload: String,消息的载荷内容
- workProved: Number ,消息发送前需要完成的任务
示例代码:
var filter = web3.shh.filter(options)
// watch for changes
filter.watch(function(error, result){
if (!error)
console.log(result);
});