http://www.7klian.com

我们如那里理惩罚web3js中的区块链从头毗连

newBlockchainConnection  
        else web3Instance.setProvider(provider);  
总结

在这个文件中,我们将处理惩罚NodeJS和区块链之间的毗连。我们将有一个新区块链毗连,它将返回一个Web3勾当毗连工具。

async function getWeb3Connection() {  
 
module.exports = {  
办理方案
让我们看看我们
如何优雅地处理惩罚NodeJS中区块链断开的环境。在Web3Js库中,措施为我们提供了以下事件
在区块链断开毗连时,当提供措施触发“终止”事件时,我们将在超时后触发回调。然后,该事件反过来挪用函数来建设一个新的区块链毗连。
            endCallback(); 

        if (hasProviderEnded) return;  
    // create new provider 
const web3JSConnection;  

const web3 = require(“web3”); 
        }, reconnectInterval);  
module.exports = {  
provider.on(‘connect’, () => console.log(“connected to blockchain”)); 
        if (web3Instance == undefined) web3Instance = new web3(provider);  
   web3JSConnection = await connection.newBlockchainConnection(‘ws://127.0.0.1:8545’, customEvent);  
 
async function newBlockchainConnection(webSocketProvider, endCallback) {  
        provider.reset();  
 
provider.on(‘end’, async (err) => {  
}  
   if (web3JSConnection == undefined) web3JSConnection = await connection.newBlockchainConnection(‘ws://127.0.0.1:8545’, endCallback);  
错误——措施错误
断开毗连后,,我们可以操作终止事件
从头启动一个新的Web3Js毗连。让我们来看一个例子来领略这一点:
竣事——措施毗连竣事。
 
const endCallback  = async function () {  
需要留意的几点:
File connection.js

});  
        // handle multiple event calls sent by Web3JS library  
   getWeb3Connection  
        });  
return web3JSConnection;  
 
        return web3Instance;  
        provider.removeAllListeners(“error”);  

const connection = require(“connection”); 
·
从头毗连的时距离断必需至少是5秒,区块链重启约莫需要5秒。

                       // emitting the restart event after some time to allow blockchain to complete startup 
 
// connect event fires when the connection established successfully. 
        setTimeout(() => {  
// 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 
            // removing all the listeners of provider. 
 
· 有时,Web3Js为同一个毗连点向您发送多个“终止”事件,因此我们必需查抄我们是否已经处理惩罚过一次断开事件。
        // end event fires whenever the connection end is detected. So Whenever this event fires we will try to reconnect to blockchain 
        hasProviderEnded = true;  

问题描写
 
         provider.removeAllListeners(“end”);  
假如您的团队在出产中利用Web3Js,那么您必需意识到在Web3Js中没有内置的从头毗连成果来处理惩罚区块链断开或从头启动。因此,凡是环境下,当毗连下降时,需要从头启动NodeJS处事以便再次毗连到区块链。这不是一个很实用的要领。
                       // we are listening to this event in the other file and this callback will initialize a new connection 
 
}

}
         provider.removeAllListeners(“connect”);  
provider.on(‘error’, (err) => console.log(err.message)); 
· 重置提供措施并删除勾当侦听器
 
        // reset the current provider  
· 利用“setProvider”在web3实例工具中配置新的提供措施,而不是建设一个新的web3实例。
 
 
let hasProviderEnded = false, web3Instance, reconnectInterval = 10000;  
const provider = new web3.providers.WebsocketProvider(webSocketProvider);  

在本文中,我们将进修如何利用Web3Js自动处理惩罚出产情况中的断开。下面描写的要领合用于Web3Js版本1.0.0-beta.35,可是对付不变的1.2 *版本也合用。

}  
hasProviderEnded = false; 
毗连——成立毗连
 

File app.js

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

郑重声明:本文版权归原作者所有,转载文章仅为传播更多信息之目的,如作者信息标记有误,请第一时间联系我们修改或删除,多谢。

相关文章阅读