Attention: Here be dragons
This is the latest
(unstable) version of this documentation, which may document features
not available in or compatible with released stable versions of Godot.
Checking the stable version of the documentation...
StreamPeerTCP¶
继承: StreamPeer < RefCounted < Object
处理 TCP 连接的流对等体。
描述¶
处理 TCP 连接的流对等体。该对象可用于连接 TCP 服务器,也可以由 TCP 服务器返回。
注意:导出到安卓时,在导出项目或使用一键部署之前,请务必在安卓导出预设中,开启 INTERNET
权限。否则,任何类型的网络通信都将被 Android 阻止。
方法¶
connect_to_host ( String host, int port ) |
|
void |
|
get_connected_host ( ) const |
|
get_connected_port ( ) const |
|
get_local_port ( ) const |
|
get_status ( ) const |
|
poll ( ) |
|
void |
set_no_delay ( bool enabled ) |
枚举¶
enum Status:
Status STATUS_NONE = 0
StreamPeerTCP的初始状态。这也是断开连接后的状态。
Status STATUS_CONNECTING = 1
表示连接到主机的 StreamPeerTCP 的状态。
Status STATUS_CONNECTED = 2
表示连接到主机的 StreamPeerTCP 的状态。
Status STATUS_ERROR = 3
表示处于错误状态的 StreamPeerTCP 的状态。
方法说明¶
Error bind ( int port, String host="*" )
打开 TCP 套接字,并将其绑定到指定的本地地址。
通常不需要这个方法,只是用来强制让后续调用 connect_to_host 时使用指定的主机 host
和端口 port
作为源地址。会在部分 NAT 打洞技术中用到,也可用于强制设置源网络接口。
Error connect_to_host ( String host, int port )
连接到指定的 host:port
对。如果使用的是有效主机名,则会进行解析。成功时返回 @GlobalScope.OK。
void disconnect_from_host ( )
与主机断开连接。
String get_connected_host ( ) const
返回该对等体的IP。
int get_connected_port ( ) const
返回该对等体的端口。
int get_local_port ( ) const
返回该对等体绑定到的本地端口。
Status get_status ( ) const
返回连接的状态,见Status。
Error poll ( )
轮询套接字,更新其状态。见 get_status。
void set_no_delay ( bool enabled )
如果 enabled
为 true
,数据包会立即发送。如果 enabled
为 false
(默认值),数据包传输将被延迟,并使用纳格算法合并。
注意:对于发送大数据包或需要传输大量数据的应用程序,建议将本属性保持禁用,因为启用本属性会减少总体可用带宽。