之前在学习 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: upgrade22:03:42.447 WebSocketConnectReadThread-20 握手信息key: Date ,value: Tue, 07 Nov 2023 14:03:42 GMT22:03:42.447 WebSocketConnectReadThread-20 握手信息key: Sec-WebSocket-Accept ,value: dia6CeCsPpnqTtBXZsLc58pxWmk=22:03:42.448 WebSocketConnectReadThread-20 握手信息key: Upgrade ,value: websocket22: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"}
       复制代码
  
评论