Ethereum WebSocket接口实践

之前在学习Ethereum的infura API时候看到有WebSocket,但是翻了很久都没找到很完整的接口文档和实践文档。当时比较迷,没太在意,以为是区块链接口的性能还用不上WebSocket,HTTP完全支持没问题。

后面在更深入学习了jsonrpc协议之后,我突然悟道了可能不需要WebSocket的接口文档也能发起请求。

经过一些失败的尝试,基本摸清楚了这里面的弯弯道道。其实就是把Web3j源码翻看一下,基本也能猜个八九不离十。下面举个最简单的例子。

Web3j源码

我们找了获取网络版本的API:net_version 。下面是尝试找到的Web3j的源码内容:

    public Request<?, NetVersion> netVersion() {
        return new Request("net_version", Collections.emptyList(), this.web3jService, NetVersion.class);
    }

这里我们看到 org.web3j.protocol.core.Request 构造方法,下面是内容:

public Request(String method, List<S> params, Web3jService web3jService, Class<T> type) {  
    this.method = method;  
    this.params = params;  
    this.id = nextId.getAndIncrement();  
    this.web3jService = web3jService;  
    this.responseType = type;  
}

到这里,我们就基本了解了如何构造请求的初步逻辑,下面我们看一下WebSocket的如何发送请求信息的:

client.send("{\"jsonrpc\":\"2.0\",\"method\":\"net_version\",\"params\":[],\"id\":1333333}")

是不是有点熟悉,刚好跟 org.web3j.protocol.core.Request 属性对应。到这里相比大家是不是都差不多明白了。

再补充一个信息,就是 org.web3j.protocol.core.Request 的属性定义部分代码。

private static AtomicLong nextId = new AtomicLong(0L);  
private String jsonrpc = "2.0";  
private String method;  
private List<S> params;  
private long id;  
private Web3jService web3jService;  
private Class<T> responseType;

这里用到了之前分享过的 java.util.concurrent.atomic.AtomicLong ,用来作为全局的唯一ID非常合适。

WebSocket API实践

这里我用到了我自己封装的WebSocket的客户端 com.funtester.socket.WebSocketFunClient ,用的是 goerli 测试网络的WebSocket地址。话不多说,上代码:

static final String host = "wss://goerli.infura.io/ws/v3/apikey"  
  
static void main(String[] args) {  
    def client = new WebSocketFunClient(host, "infura ethereum")  
    client.connect()  
    client.send("{\"jsonrpc\":\"2.0\",\"method\":\"eth_accounts\",\"params\":[],\"id\":1}")  
    client.send("{\"jsonrpc\":\"2.0\",\"method\":\"net_version\",\"params\":[],\"id\":1333333}")  
  
}

控制台输出:

22:03:41.023 main infura ethereum 开始连接...
22:03:42.447 WebSocketConnectReadThread-20 infura ethereum 正在建立socket连接...
22:03:42.447 WebSocketConnectReadThread-20 握手信息key: Connection ,value: upgrade
22:03:42.447 WebSocketConnectReadThread-20 握手信息key: Date ,value: Tue, 07 Nov 2023 14:03:42 GMT
22:03:42.447 WebSocketConnectReadThread-20 握手信息key: Sec-WebSocket-Accept ,value: dia6CeCsPpnqTtBXZsLc58pxWmk=
22:03:42.448 WebSocketConnectReadThread-20 握手信息key: Upgrade ,value: websocket
22:03:44.028 main infura ethereum 连接成功!
22:03:44.299 WebSocketConnectReadThread-20 infura ethereum收到: {"jsonrpc":"2.0","id":1,"result":[]}
22:03:44.544 WebSocketConnectReadThread-20 infura ethereum收到: {"jsonrpc":"2.0","id":1333333,"result":"5"}

posted @ 2023-11-17 22:04  FunTester  阅读(23)  评论(0编辑  收藏  举报