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.

StreamPeerTCP

繼承: StreamPeerSocket < StreamPeer < RefCounted < Object

處理 TCP 連接的流對等體。

說明

處理 TCP 連接的流對等體。該物件可用於連接 TCP 伺服器,也可以由 TCP 伺服器返回。

注意:匯出到安卓時,在匯出專案或使用一鍵部署之前,請務必在安卓匯出預設中,開啟 INTERNET 許可權。否則,任何型別的網路通信都將被 Android 阻止。

方法

Error

bind(port: int, host: String = "*")

Error

connect_to_host(host: String, port: int)

String

get_connected_host() const

int

get_connected_port() const

int

get_local_port() const

void

set_no_delay(enabled: bool)


方法說明

Error bind(port: int, host: String = "*") 🔗

打開 TCP 通訊端,並將其綁定到指定的本地地址。

通常不需要這個方法,只是用來強制讓後續呼叫 connect_to_host() 時使用指定的主機 host 和埠 port 作為源位址。會在部分 NAT 打洞技術中用到,也可用於強制設定源網路介面。


Error connect_to_host(host: String, port: int) 🔗

連接到指定的 host:port 對。如果使用的是有效主機名稱,則會進行解析。成功時返回 @GlobalScope.OK


String get_connected_host() const 🔗

返回該對等體的IP。


int get_connected_port() const 🔗

返回該對等體的埠。


int get_local_port() const 🔗

返回該對等體綁定到的本地埠。


void set_no_delay(enabled: bool) 🔗

如果 enabledtrue,封包會立即發送。如果 enabledfalse(預設值),封包傳輸將被延遲,並使用納格演算法合併。

注意:對於發送大封包或需要傳輸大量資料的套用程式,建議將本屬性保持禁用,因為啟用本屬性會減少總體可用頻寬。