织梦CMS - 轻松建站从此开始!

我的网站

当前位置: 主页 > 区块链行情

我们如何处理web3js中的区块链重新连接

时间:2020-03-02 18:30来源:金库链 作者:admin 点击:
在本文中,我们将学习如何使用Web3Js自动处理生产环境中的区块链断开。下面描述的方法适用于Web3Js版本1.0.0-beta.35,但是对于稳定的1.2 *版本也适用。...... 在本文中,我们将学习如何使
在本文中,我们将学习如何使用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秒。

(责任编辑:admin)
织梦二维码生成器
顶一下
(0)
0%
踩一下
(0)
0%
------分隔线----------------------------
发表评论
请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
评价:
表情:
用户名: 验证码:点击我更换图片
栏目列表
推荐内容