- 我无法将我的 node.js 客户端连接到测试网络以在我的 arduino nano 上显示当前的块头。对于订阅,我使用 infura 作为服务提供商。
- 在我使用 websocket 连接之前,但我不知道如何使用 websocket 来请求当前的块头。
- 如果您能向我解释 HttpRequest 订阅和 WebSocket 订阅之间的区别,我将非常高兴。
- 最后,我想知道连接到以太坊节点时 web3 的主要任务是什么。
谢谢您的帮助!
当前的错误信息如下。
代码:
var Web3 = require('web3');
var Five = require("johnny-five");
var OledJs = require('oled-js');
var Font = require('oled-font-5x7');
//connect to blockchain client
//based on infura
var web3ws = new Web3(new Web3.providers.HttpProvider('https://ropsten.infura.io/...mySocket'))
//load board ressources
//Initialize the board ressources
var board = new Five.Board();
var oled;
board.on("ready", function() {
//oled parameters
const opts = {width: 128, height: 64, adress: 0x3C};
//initialize display
oled = new OledJs(board, Five, opts);
oled.clearDisplay(true);
oled.update();
//wait for blockheader
listenToBlockchain();
})
//listen for blockchain events (only works on web socket connections)
function listenToBlockchain() {
//event listener
web3ws.eth.subscribe('newBlockHeaders')
.on("data", function(blockHeader){
//an dieser Stelle wird
console.log("Block: "+blockHeader.number);
output(blockHeader);
})
.on("error", function(e){
console.log("FEHLER: "+e);
})
//output function
function output(block) {
oled.setCursor(1,1);
oled.writeString(Font, 1, ''+blockHeader.number, 1, false, 2);
oled.setCursor(1,15);
oled.writeString(Font, 1, ''+blockHeader.hash,1,true,2);
}
}
错误信息:
C:\Users\andra\Desktop\ArduinoIOTblockchainsession\node_modules\web3-core-subscriptions\src\subscription.js:208
this.callback(err2, null, this);
^
TypeError: this.callback is not a function
at Subscription.subscribe (C:\Users\andra\Desktop\ArduinoIOTblockchainsession\node_modules\web3-core-subscriptions\src\subscription.js:208:14)
at Eth.subscribe (C:\Users\andra\Desktop\ArduinoIOTblockchainsession\node_modules\web3-core-subscriptions\src\index.js:68:39)
at listenToBlockchain (C:\Users\andra\Desktop\ArduinoIOTblockchainsession\infura_test.js:38:13)
at Board.<anonymous> (C:\Users\andra\Desktop\ArduinoIOTblockchainsession\infura_test.js:31:2)
at emitNone (events.js:111:20)
at Board.emit (events.js:208:7)
at _combinedTickCallback (internal/process/next_tick.js:131:7)
at process._tickDomainCallback (internal/process/next_tick.js:218:9)