【问题标题】:How to subscribe to `newBlockHeaders` on local RSK node over websockets?如何通过 websockets 在本地 RSK 节点上订阅“newBlockHeaders”?
【发布时间】:2021-08-06 14:38:33
【问题描述】:

我正在使用以下端点连接到 RSKj:

ws://localhost:4444/

...但是,我无法连接。

请注意,等效的 HTTP 端点 http://localhost:4444/ 为我工作,所以我知道我的 RSKj 节点运行正常。

我需要监听newBlockHeaders,所以我更喜欢使用WebSockets(而不是HTTP)。

我该怎么做?

【问题讨论】:

    标签: websocket web3 rsk


    【解决方案1】:

    RSKj 默认使用4444 作为 HTTP 传输的端口; 和4445 作为Websockets 传输的端口。 另请注意,websockets 端点不在/, 而是websocket。 因此使用ws://localhost:4445/websocket 作为您的端点。

    如果您使用的是 web3.js, 您可以创建一个通过 Websockets 连接的 web3 实例 使用以下内容:

    const Web3 = require('web3');
    const wsEndpoint = 'ws://localhost:4445/websocket';
    const wsProvider =
      new Web3.providers.WebsocketProvider(wsEndpoint);
    const web3 = new Web3(wsProvider);
    
    

    你问题的第二部分可以做 在newBlockHeaders 上使用eth_subscribe。 像这样使用上面的web3 实例:

    // eth_subscribe newBlockHeaders
    web3.eth.subscribe('newBlockHeaders', function(error, blockHeader) {
      if (!error) {
        // TODO something with blockHeader
      } else {
        // TODO something with error
      }
    });
    
    

    【讨论】:

      猜你喜欢
      • 2021-06-03
      • 2021-10-29
      • 2020-06-28
      • 1970-01-01
      • 2019-03-30
      • 2016-04-09
      • 1970-01-01
      • 1970-01-01
      • 2017-08-12
      相关资源
      最近更新 更多