微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

使用 Pubnub 设置发布/订阅实现的问题

如何解决使用 Pubnub 设置发布/订阅实现的问题

首先让我先说我是一名网络开发人员,对区块链领域非常陌生,所以如果我遗漏了一些明显的东西,我提前道歉。话虽如此,我在将广播交易方法实施到我正在从事的加密货币项目中​​时遇到了问题。每次我能够成功地向我的 API 发出事务请求并且我能够在主开发网络上看到正确的事务池时,但是我的对等网络在我重新启动它之前看不到任何新事务。我知道这意味着我的广播事务实现缺少一些东西,但我不确定我需要修复它。

请参考以下图片

Dev port(left) Dev-peer(Right)

GET Request to API returns transaction

GET Request to dev-peer port does NOT return transaction

Dev-Peer port updates the transaction when the sync method is called after restarting the node

这是我使用 PUBNUB 实现 pubsub 的代码片段

const CHANNELS = {
    TEST: "TEST",BLOCKCHAIN: "BLOCKCHAIN",TRANSACTION: "TRANSACTION"
};

class PubSub {
    constructor({blockchain,transactionPool}) {
        this.blockchain = blockchain;
        this.transactionPool = transactionPool;
        this.pubnub = new PubNub(credentials); // defined above but omitted for obvIoUs reasons
        this.pubnub.subscribe({channels: Object.values(CHANNELS)}); // defined in channels object
        this.pubnub.addListener(this.listener());
    };

    listener() {
        return {
            message: messageObject => {
                const { channel,message } = messageObject;

                console.log(`Message received. Channel: ${channel}. Message: ${message}`);

                const parsedMessage = JSON.parse(message);

                switch(channel) {
                    case CHANNELS.BLOCKCHAIN:
                        this.blockchain.replaceChain(parsedMessage);
                        break;
                    case CHANNELS.TRANSACTION: 
                        this.transactionPool.setTransaction(parsedMessage);
                        break;
                    default: 
                        return;
                }
            }
        };
    }

    publish({channel,message}) {
        this.pubnub.unsubscribe(channel,() => {
            this.pubnub.publish(channel,message,() => {
                this.pubnub.subscribe(channel);
            });
        });
 
    }

    broadcastChain() {
        this.publish({
            channel: CHANNELS.BLOCKCHAIN,message: JSON.stringify(this.blockchain.chain)
        })
    }

    broadcastTransaction(transaction) {
        this.publish({
            channel: CHANNELS.TRANSACTION,message: JSON.stringify(transaction)
        });
    }
    };

这里是调用 broadcastTransaction 方法代码片段

const pubsub = new PubSub({blockchain,transactionPool});

    let transaction = transactionPool.existingTransaction({inputAddress: wallet.publicKey}); // Creates global binding

// Sends transaction to the network
app.post("/api/transact",(req,res) => {
    const {amount,recipient} = req.body;


    try {
        if(transaction) {
            transaction.update({senderWallet: wallet,recipient,amount });
        } else {
            transaction = wallet.createTransaction({recipient,amount});
        }
    } catch(error) {
        return res.status(400).json({type: "error",message: error.message});
    };

    transactionPool.setTransaction(transaction);

    pubsub.broadcastTransaction(transaction); // Calls broadcastTransaction from pubsub class (does not work for peers)

    res.json({type: "success",transaction});
});

我试图尽可能具体,但如果我错过了什么,请告诉我。提前致谢!

解决方法

感谢您的投入。日志记录帮助我意识到在启动节点时我的开发和对等端口没有被订阅。我在 PubSub 类中添加了以下方法,并且能够同步开发端口和对等端口

subscribeToChannels() {
    this.pubnub.subscribe({
      channels: [Object.values(CHANNELS)]
    });
  }

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。