@5ire/rpc-provider 中文文档教程
@polkadot/rpc-provider
通用传输提供程序,用于处理与其交互的应用程序与 Polkadot 客户端之间的方法调用传输。 它提供了一个进行 RPC 调用的接口,通常,除非您在低级别操作并负责参数/结果的编码和解码,否则不会直接使用它,而只会传递给更高级别的接口.
Provider Selection
提供了两种类型的提供程序,一种允许使用 HTTP 作为传输机制,另一种使用 WebSockets。 通常建议使用 [[WsProvider]],因为除了标准调用之外,它还允许订阅,其中所有状态更改都可以从节点推送到客户端。
在基于浏览器的环境和 Node.js 环境中,这两个提供程序都是可用的(API 也是如此)。 不支持功能的 Polyfill 会根据功能检测自动应用。
Usage
安装
yarn add @polkadot/rpc-provider
-WebSocket初始化
import { WsProvider } from '@5ire/rpc-provider/ws';
// this is the actual default endpoint
const provider = new WsProvider('ws://127.0.0.1:9944');
const version = await provider.send('client_version', []);
console.log('client version', version);
-HTTP初始化-
import { HttpProvider } from '@5ire/rpc-provider';
// this is the actual default endpoint
const provider = new HttpProvider('http://127.0.0.1:9933');
const version = await provider.send('chain_getBlockHash', []);
console.log('latest block Hash', hash);
@polkadot/rpc-provider
Generic transport providers to handle the transport of method calls to and from Polkadot clients from applications interacting with it. It provides an interface to making RPC calls and is generally, unless you are operating at a low-level and taking care of encoding and decoding of parameters/results, it won't be directly used, rather only passed to a higher-level interface.
Provider Selection
There are two flavours of the providers provided, one allowing for using HTTP as a transport mechanism, the other using WebSockets. It is generally recommended to use the [[WsProvider]] since in addition to standard calls, it allows for subscriptions where all changes to state can be pushed from the node to the client.
Both providers are usable (as is the API), in both browser-based and Node.js environments. Polyfills for unsupported functionality are automatically applied based on feature-detection.
Usage
Installation -
yarn add @polkadot/rpc-provider
WebSocket Initialization -
import { WsProvider } from '@5ire/rpc-provider/ws';
// this is the actual default endpoint
const provider = new WsProvider('ws://127.0.0.1:9944');
const version = await provider.send('client_version', []);
console.log('client version', version);
HTTP Initialization -
import { HttpProvider } from '@5ire/rpc-provider';
// this is the actual default endpoint
const provider = new HttpProvider('http://127.0.0.1:9933');
const version = await provider.send('chain_getBlockHash', []);
console.log('latest block Hash', hash);