# wsock.bt.dhtClient 库模块帮助文档

## wsock.bt 成员列表 <a id="wsock.bt" href="#wsock.bt">&#x23;</a>

### wsock.bt.dhtClient <a id="wsock.bt.dhtClient" href="#wsock.bt.dhtClient">&#x23;</a>
BitTorrent DHT 客户端。  

此客户端可用于访问 process.aria2 对象的 DHT 服务端口（dhtListeningPort 属性）。

### wsock.bt.dhtClient() <a id="wsock.bt.dhtClient" href="#wsock.bt.dhtClient">&#x23;</a>
[返回对象:wsockBtDhtClientObject](#wsockBtDhtClientObject)

### wsock.bt.dhtClient(ports,routingTable) <a id="wsock.bt.dhtClient" href="#wsock.bt.dhtClient">&#x23;</a>
创建 DHT 客户端。  
- 参数 @ports 可用数组或字符串指定端口号，支持 string.intSegments 格式。  
例如："6881-6999"，自动检测可用端口并自动添加 UPnP 端口映射。  
- 参数 @routingTable 必须指定 wsock.bt.kBucket 对象（或兼容的实现）

## wsockBtDhtClientObject 成员列表 <a id="wsockBtDhtClientObject" href="#wsockBtDhtClientObject">&#x23;</a>

### wsockBtDhtClientObject.nodeId <a id="wsockBtDhtClientObject.nodeId" href="#wsockBtDhtClientObject.nodeId">&#x23;</a>
DHT 节点 ID

### wsockBtDhtClientObject.sendFindNode <a id="wsockBtDhtClientObject.sendFindNode" href="#wsockBtDhtClientObject.sendFindNode">&#x23;</a>

```aardio
wsockBtDhtClientObject.sendFindNode(ip,port,tagetNodeId,  
	function(nodes) {  
		/*回调参数 nodes 都是 DHT 端点信息数组，每个元素都有 ip,port,nodeId 字段。  

可选用第 3,4 个回调参数接收发送时的 ip,port 参数。  
第 2 个回调参数为保留参数，第 5 个回调函数总是 null 值。  
这样设计参数顺序是为了可以与 sendGetPeers 方法共用一个回调函数。*/  
	}  
)
```

### wsockBtDhtClientObject.sendGetPeers <a id="wsockBtDhtClientObject.sendGetPeers" href="#wsockBtDhtClientObject.sendGetPeers">&#x23;</a>

```aardio
wsockBtDhtClientObject.sendGetPeers(ip,port,infoHash,  
	function(nodes,peers) {  
		/*如果找到可获取元数据的端点则回调参数 @peers 为 BT 端点（peer）数组，  
否则回调参数 nodes 返回 DHT 节点信息数组。  
peers 是可以使用 BT 协议连接的端点地址，node 可以使用 DHT 协议连接的端点地址。  
nodes,peers 的格式都是对象数组，每个元素都有 ip,port 字段，  
nodes 元素则会多一个 20 字节的 nodeId 字段（二进制字符串）。  

可选用第 3,4,5 个回调参数接收发送时的 ip,port,infoHash 参数。*/  
	}  
)
```

## wsockBtDhtClientObject 事件列表 <a id="wsockBtDhtClientObjectEvent" href="#wsockBtDhtClientObjectEvent">&#x23;</a>

### wsockBtDhtClientObject.onAnnouncePeer(infoHash,ip,port) <a id="wsockBtDhtClientObject.onAnnouncePeer" href="#wsockBtDhtClientObject.onAnnouncePeer">&#x23;</a>

```aardio
wsockBtDhtClientObject.onAnnouncePeer = function(infoHash,ip,port){
	/*收到 announce peers 请求  
回调参数 @infoHash 为发现的 BT 资源 info hash 值。  
可调用 sendGetPeers 方法获取可下载元数据的 peers*/
}
```

### wsockBtDhtClientObject.onError <a id="wsockBtDhtClientObject.onError" href="#wsockBtDhtClientObject.onError">&#x23;</a>

```aardio
onError = function(msg,code){
	print("DHT Client error:",msg,code);
}
```

### wsockBtDhtClientObject.onGetPeers(infoHash,ip,port) <a id="wsockBtDhtClientObject.onGetPeers" href="#wsockBtDhtClientObject.onGetPeers">&#x23;</a>

```aardio
wsockBtDhtClientObject.onGetPeers = function(infoHash,ip,port){
	/*收到 get peers 请求。  
回调参数 @infoHash 为发现的 BT 资源 info hash 值。*/
}
```

### wsockBtDhtClientObject.onSoketError <a id="wsockBtDhtClientObject.onSoketError" href="#wsockBtDhtClientObject.onSoketError">&#x23;</a>

```aardio
onSoketError = function(msg,code){
	print("DHT Client socket error:",msg,code);
}
```

