当前位置: 首页 > 工具软件 > Web3.js > 使用案例 >

web3.js的provider

管景天
2023-12-01

web3是所有ethereum交互模块的总包。

web3.setProvider()

  • 参数:
    Object - myProvider: 有效的服务提供者对象。

  • 返回值:
    Boolean

web3.setProvider()方法用来修改指定模块的底层通讯服务提供者。
调用:

web3.setProvider(myProvider)
web3.eth.setProvider(myProvider)
web3.shh.setProvider(myProvider)
web3.bzz.setProvider(myProvider)
...

注意:当在web3上直接调用setProvider()方法时(即调用web3.setProvider(myProvider)),将为所有其他子模块设置服务提供者,例如web3.eth和web3.shh。 但web3.bzz不受影响,因为该子模块始终使用独立的服务提供者。

示例代码:

var web3 = new Web3('http://localhost:8545');
// or
var web3 = new Web3(new Web3.providers.HttpProvider('http://localhost:8545'));

// change provider
web3.setProvider('ws://localhost:8546');
// or
web3.setProvider(new Web3.providers.WebsocketProvider('ws://localhost:8546'));

// Using the IPC provider in node.js
var net = require('net');
var web3 = new Web3('/Users/myuser/Library/Ethereum/geth.ipc', net); // mac os path
// or
var web3 = new Web3(new Web3.providers.IpcProvider('/Users/myuser/Library/Ethereum/geth.ipc', net)); // mac os path
// on windows the path is: "\\\\.\\pipe\\geth.ipc"
// on linux the path is: "/users/myuser/.ethereum/geth.ipc"

web3.providers

返回当前有效的通信服务提供者。

web3.providers
web3.eth.providers
web3.shh.providers
web3.bzz.providers
...

返回值:
Object, 参见以下服务提供者对象:

  • Object - HttpProvider: The HTTP provider is deprecated, as it won’t work for subscriptions.
    HTTP服务提供器已经被弃用,因为它不支持订阅。
  • Object - WebsocketProvider: The Websocket provider is the standard for usage in legacy browsers.
    Websocket服务提供器是用于传统的浏览器中的标准方法。
  • Object - IpcProvider: The IPC provider is used node.js dapps when running a local node. Gives the most secure connection.
    当运行一个本地节点时,IPC服务提供器用于node.js的DApp,该方法提供最安全的连接。

示例代码:

var Web3 = require('web3');
// use the given Provider, e.g in Mist, or instantiate a new websocket provider
var web3 = new Web3(Web3.givenProvider || 'ws://remotenode.com:8546');
// or
var web3 = new Web3(Web3.givenProvider || new Web3.providers.WebsocketProvider('ws://remotenode.com:8546'));

// Using the IPC provider in node.js
var net = require('net');

var web3 = new Web3('/Users/myuser/Library/Ethereum/geth.ipc', net); // mac os path
// or
var web3 = new Web3(new Web3.providers.IpcProvider('/Users/myuser/Library/Ethereum/geth.ipc', net)); // mac os path
// on windows the path is: "\\\\.\\pipe\\geth.ipc"
// on linux the path is: "/users/myuser/.ethereum/geth.ipc"

Configuration:

// ====
// Http
// ====

var Web3HttpProvider = require('web3-providers-http');

var options = {
    keepAlive: true,
    withCredentials: false,
    timeout: 20000, // ms
    headers: [
        {
            name: 'Access-Control-Allow-Origin',
            value: '*'
        },
        {
            ...
        }
    ],
    agent: {
        http: http.Agent(...),
        baseUrl: ''
    }
};

var provider = new Web3HttpProvider('http://localhost:8545', options);



// ==========
// Websockets
// ==========

var Web3WsProvider = require('web3-providers-ws');

var options = {
    timeout: 30000, // ms

    // Useful for credentialed urls, e.g: ws://username:password@localhost:8546
    headers: {
      authorization: 'Basic username:password'
    },

    // Useful if requests result are large
    clientConfig: {
      maxReceivedFrameSize: 100000000,   // bytes - default: 1MiB
      maxReceivedMessageSize: 100000000, // bytes - default: 8MiB
    },

    // Enable auto reconnection
    reconnect: {
        auto: true,
        delay: 5000, // ms
        maxAttempts: 5,
        onTimeout: false
    }
};

var ws = new Web3WsProvider('ws://localhost:8546', options);

相关内容:
https://web3js.readthedocs.io/en/v1.2.9/web3.html#setprovider
http://cw.hubwiz.com/card/c/web3.js-1.0/1/1/4/

web3的provider的正确使用

 类似资料: