日本黄色一级经典视频|伊人久久精品视频|亚洲黄色色周成人视频九九九|av免费网址黄色小短片|黄色Av无码亚洲成年人|亚洲1区2区3区无码|真人黄片免费观看|无码一级小说欧美日免费三级|日韩中文字幕91在线看|精品久久久无码中文字幕边打电话

當(dāng)前位置:首頁 > 物聯(lián)網(wǎng) > 區(qū)塊鏈
[導(dǎo)讀] 在本文中,我們將學(xué)習(xí)如何使用Web3Js自動處理生產(chǎn)環(huán)境中的區(qū)塊鏈斷開。下面描述的方法適用于Web3Js版本1.0.0-beta.35,但是對于穩(wěn)定的1.2 *版本也適用。 問

在本文中,我們將學(xué)習(xí)如何使用Web3Js自動處理生產(chǎn)環(huán)境中的區(qū)塊鏈斷開。下面描述的方法適用于Web3Js版本1.0.0-beta.35,但是對于穩(wěn)定的1.2 *版本也適用。

問題描述

如果您的團(tuán)隊在生產(chǎn)中使用Web3Js,那么您必須意識到在Web3Js中沒有內(nèi)置的重新連接功能來處理區(qū)塊鏈斷開或重新啟動。因此,通常情況下,當(dāng)連接下降時,需要重新啟動NodeJS服務(wù)以便再次連接到區(qū)塊鏈。這不是一個很實用的方法。

解決方案

讓我們看看我們?nèi)绾蝺?yōu)雅地處理NodeJS中區(qū)塊鏈斷開的情況。在Web3Js庫中,程序為我們提供了以下事件

連接——建立連接

錯誤——程序錯誤

結(jié)束——程序連接結(jié)束。

斷開連接后,我們可以利用終止事件重新啟動一個新的Web3Js連接。讓我們來看一個例子來理解這一點:

File connection.js

在這個文件中,我們將處理NodeJS和區(qū)塊鏈之間的連接。我們將有一個新區(qū)塊鏈連接,它將返回一個Web3活動連接對象。

const web3 = require(“web3”);

let hasProviderEnded = false, web3Instance, reconnecTInterval = 10000;

async funcTIon newBlockchainConnecTIon(webSocketProvider, endCallback) {

// create new provider

const provider = new web3.providers.WebsocketProvider(webSocketProvider);

hasProviderEnded = false;

// connect event fires when the connecTIon established successfully.

provider.on(‘connect’, () =》 console.log(“connected to blockchain”));

// error event fires whenever there is an error response from blockchain and this event also has an error object and message property of error gives us the specific reason for the error

provider.on(‘error’, (err) =》 console.log(err.message));

// end event fires whenever the connection end is detected. So Whenever this event fires we will try to reconnect to blockchain

provider.on(‘end’, async (err) =》 {

// handle multiple event calls sent by Web3JS library

if (hasProviderEnded) return;

// setting hashProviderEnded to true as sometimes the end event is fired multiple times by the provider

hasProviderEnded = true;

// reset the current provider

provider.reset();

// removing all the listeners of provider.

provider.removeAllListeners(“connect”);

provider.removeAllListeners(“error”);

provider.removeAllListeners(“end”);

setTimeout(() =》 {

// emitting the restart event after some time to allow blockchain to complete startup

// we are listening to this event in the other file and this callback will initialize a new connection

endCallback();

}, reconnectInterval);

});

if (web3Instance == undefined) web3Instance = new web3(provider);

else web3Instance.setProvider(provider);

return web3Instance;

}

module.exports = {

newBlockchainConnection

}

File app.js

const connection = require(“connection”);

const web3JSConnection;

const endCallback = async function () {

web3JSConnection = await connection.newBlockchainConnection(‘ws://127.0.0.1:8545’, customEvent);

});

async function getWeb3Connection() {

if (web3JSConnection == undefined) web3JSConnection = await connection.newBlockchainConnection(‘ws://127.0.0.1:8545’, endCallback);

return web3JSConnection;

}

module.exports = {

getWeb3Connection

}

總結(jié)

在區(qū)塊鏈斷開連接時,當(dāng)提供程序觸發(fā)“終止”事件時,我們將在超時后觸發(fā)回調(diào)。然后,該事件反過來調(diào)用函數(shù)來創(chuàng)建一個新的區(qū)塊鏈連接。

需要注意的幾點:

· 有時,Web3Js為同一個連接點向您發(fā)送多個“終止”事件,因此我們必須檢查我們是否已經(jīng)處理過一次斷開事件。

· 使用“setProvider”在web3實例對象中設(shè)置新的提供程序,而不是創(chuàng)建一個新的web3實例。

· 重置提供程序并刪除活動偵聽器

· 重新連接的時間間隔必須至少是5秒,區(qū)塊鏈重啟大約需要5秒。

本站聲明: 本文章由作者或相關(guān)機(jī)構(gòu)授權(quán)發(fā)布,目的在于傳遞更多信息,并不代表本站贊同其觀點,本站亦不保證或承諾內(nèi)容真實性等。需要轉(zhuǎn)載請聯(lián)系該專欄作者,如若文章內(nèi)容侵犯您的權(quán)益,請及時聯(lián)系本站刪除( 郵箱:macysun@21ic.com )。
換一批
延伸閱讀
關(guān)閉