当前位置:首页 > 物联网 > 区块链
[导读] 在本文中,我们将学习如何使用Web3Js自动处理生产环境中的区块链断开。下面描述的方法适用于Web3Js版本1.0.0-beta.35,但是对于稳定的1.2 *版本也适用。 问

在本文中,我们将学习如何使用Web3Js自动处理生产环境中的区块链断开。下面描述的方法适用于Web3Js版本1.0.0-beta.35,但是对于稳定的1.2 *版本也适用。

问题描述

如果您的团队在生产中使用Web3Js,那么您必须意识到在Web3Js中没有内置的重新连接功能来处理区块链断开或重新启动。因此,通常情况下,当连接下降时,需要重新启动NodeJS服务以便再次连接到区块链。这不是一个很实用的方法。

解决方案

让我们看看我们如何优雅地处理NodeJS中区块链断开的情况。在Web3Js库中,程序为我们提供了以下事件

连接——建立连接

错误——程序错误

结束——程序连接结束。

断开连接后,我们可以利用终止事件重新启动一个新的Web3Js连接。让我们来看一个例子来理解这一点:

File connection.js

在这个文件中,我们将处理NodeJS和区块链之间的连接。我们将有一个新区块链连接,它将返回一个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

}

总结

在区块链断开连接时,当提供程序触发“终止”事件时,我们将在超时后触发回调。然后,该事件反过来调用函数来创建一个新的区块链连接。

需要注意的几点:

· 有时,Web3Js为同一个连接点向您发送多个“终止”事件,因此我们必须检查我们是否已经处理过一次断开事件。

· 使用“setProvider”在web3实例对象中设置新的提供程序,而不是创建一个新的web3实例。

· 重置提供程序并删除活动侦听器

· 重新连接的时间间隔必须至少是5秒,区块链重启大约需要5秒。

本站声明: 本文章由作者或相关机构授权发布,目的在于传递更多信息,并不代表本站赞同其观点,本站亦不保证或承诺内容真实性等。需要转载请联系该专栏作者,如若文章内容侵犯您的权益,请及时联系本站删除。
换一批
延伸阅读

特朗普集团近日取消了其新推出的T1智能手机“将在美国制造”的宣传标语,此举源于外界对这款手机能否以当前定价在美国本土生产的质疑。

关键字: 特朗普 苹果 AI

美国总统特朗普在公开场合表示,他已要求苹果公司CEO蒂姆·库克停止在印度建厂,矛头直指该公司生产多元化的计划。

关键字: 特朗普 苹果 AI

4月10日消息,据媒体报道,美国总统特朗普宣布,美国对部分贸易伙伴暂停90天执行新关税政策,同时对中国的关税提高到125%,该消息公布后苹果股价飙升了15%。这次反弹使苹果市值增加了4000多亿美元,目前苹果市值接近3万...

关键字: 特朗普 AI 人工智能 特斯拉

3月25日消息,据报道,当地时间3月20日,美国总统特朗普在社交媒体平台“真实社交”上发文写道:“那些被抓到破坏特斯拉的人,将有很大可能被判入狱长达20年,这包括资助(破坏特斯拉汽车)者,我们正在寻找你。”

关键字: 特朗普 AI 人工智能 特斯拉

1月22日消息,刚刚,新任美国总统特朗普放出重磅消息,将全力支持美国AI发展。

关键字: 特朗普 AI 人工智能

特朗普先生有两件事一定会载入史册,一个是筑墙,一个是挖坑。在美墨边境筑墙的口号确保边境安全,降低因非法移民引起的犯罪率过高问题;在中美科技产业之间挖坑的口号也是安全,美国企业不得使用对美国国家安全构成威胁的电信设备,总统...

关键字: 特朗普 孤立主义 科技产业

据路透社1月17日消息显示,知情人士透露,特朗普已通知英特尔、铠侠在内的几家华为供应商,将要撤销其对华为的出货的部分许可证,同时将拒绝其他数十个向华为供货的申请。据透露,共有4家公司的8份许可被撤销。另外,相关公司收到撤...

关键字: 华为 芯片 特朗普

曾在2018年时被美国总统特朗普称作“世界第八奇迹”的富士康集团在美国威斯康星州投资建设的LCD显示屏工厂项目,如今却因为富士康将项目大幅缩水并拒绝签订新的合同而陷入了僵局。这也导致富士康无法从当地政府那里获得约40亿美...

关键字: 特朗普 富士康

今年5月,因自己发布的推文被贴上“无确凿依据”标签而与推特发生激烈争执后,美国总统特朗普签署了一项行政令,下令要求重审《通信规范法》第230条。

关键字: 谷歌 facebook 特朗普

众所周知,寄往白宫的所有邮件在到达白宫之前都会在他地进行分类和筛选。9月19日,根据美国相关执法官员的通报,本周早些时候,执法人员截获了一个寄给特朗普总统的包裹,该包裹内包含蓖麻毒蛋白。

关键字: 美国 白宫 特朗普
关闭