我正在尝试在两个 IPFS 节点之间发送消息。我正在运行的守护进程基于 go-ipfs,并使用以下标志运行:
ipfs daemon --enable-pubsub-experiment
我编写了两个 .js 文件,一个用于订阅者:
const IPFS = require('ipfs')
const topic = 'topic';
const Buffer = require('buffer').Buffer;
const msg_buffer = Buffer.from('message');
const ipfs = new IPFS({
repo: repo(),
EXPERIMENTAL: {
pubsub: true
},
config: {
Addresses: {
Swarm: [
'/dns4/ws-star.discovery.libp2p.io/tcp/443/wss/p2p-websocket-star'
]
}
}
})
ipfs.once('ready', () => ipfs.id((err, info) => {
if (err) { throw err }
console.log('IPFS node ready with address ' + info.id)
subscribeToTopic()
}))
function repo () {
return 'ipfs-' + Math.random()
}
const receiveMsg = (msg) => {
console.log(msg.data.toString())
}
const subscribeToTopic = () => {
ipfs.pubsub.subscribe(topic, receiveMsg, (err) => {
if (err) {
return console.error(`failed to subscribe to ${topic}`, err)
}
console.log(`subscribed to ${topic}`)
})
}
一个是给出版商的:
const IPFS = require('ipfs');
const topic = 'topic';
const Buffer = require('buffer').Buffer;
const msg_buffer = Buffer.from('message');
const ipfs = new IPFS({
repo: repo(),
EXPERIMENTAL: {
pubsub: true
},
config: {
Addresses: {
Swarm: [
'/dns4/ws-star.discovery.libp2p.io/tcp/443/wss/p2p-websocket-star'
]
}
}
})
ipfs.once('ready', () => ipfs.id((err, info) => {
if (err) { throw err }
console.log('IPFS node ready with address ' + info.id)
publishToTopic()
}))
function repo () {
return 'ipfs-' + Math.random()
}
const publishToTopic = () => {
ipfs.pubsub.publish(topic, msg_buffer, (err) => {
if (err) {
return console.error(`failed to publish to ${topic}`, err)
}
// msg was broadcasted
console.log(`published to ${topic}`)
console.log(msg_buffer.toString())
})
}
我已经运行了 .js 脚本:
node file.js
但是订阅者没有收到订阅者的任何消息,我不知道为什么。
在这种情况下连接两个节点的正确方法是什么?