web3是所有ethereum交互模块的总包。
参数:
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.eth.providers
web3.shh.providers
web3.bzz.providers
...
返回值:
Object, 参见以下服务提供者对象:
Object
- HttpProvider
: The HTTP provider is deprecated, as it won’t work for subscriptions.Object
- WebsocketProvider
: The Websocket provider is the standard for usage in legacy browsers.Object
- IpcProvider
: The IPC provider is used node.js dapps when running a local node. Gives the most secure connection.示例代码:
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/