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...
StreamPeer
繼承: RefCounted < Object
被繼承: StreamPeerBuffer, StreamPeerExtension, StreamPeerGZIP, StreamPeerSocket, StreamPeerTLS
與流互動的抽象基底類別。
說明
StreamPeer 是一種抽象基底類別,常用於流式協議(例如 TCP)。它提供了通過流發送資料的 API,將資料作為原始資料或字串處理。
注意:匯出到安卓時,在匯出專案或使用一鍵部署之前,請務必在安卓匯出預設中,開啟 INTERNET 許可權。否則,任何型別的網路通信都將被 Android 阻止。
屬性
|
方法
get_8() |
|
get_16() |
|
get_32() |
|
get_64() |
|
get_available_bytes() const |
|
get_half() |
|
get_partial_data(bytes: int) |
|
get_string(bytes: int = -1) |
|
get_u8() |
|
get_u16() |
|
get_u32() |
|
get_u64() |
|
get_utf8_string(bytes: int = -1) |
|
void |
|
void |
|
void |
|
void |
|
put_data(data: PackedByteArray) |
|
void |
put_double(value: float) |
void |
|
void |
|
put_partial_data(data: PackedByteArray) |
|
void |
put_string(value: String) |
void |
|
void |
|
void |
|
void |
|
void |
put_utf8_string(value: String) |
void |
屬性說明
為 true 時,該 StreamPeer 進行編解碼時會使用大端格式。
方法說明
從流中獲取有符號位元組。
從流中獲取有符號 16 位元值。
從流中獲取有符號 32 位元值。
從流中獲取有符號 64 位元值。
int get_available_bytes() const 🔗
返回該 StreamPeer 可用的位元組數。
Returns a chunk data with the received bytes, as an Array containing two elements: an Error constant and a PackedByteArray. bytes is the number of bytes to be received. If not enough bytes are available, the function will block until the desired amount is received.
從流中獲取一個雙精度浮點數。
從流中獲取一個單精確度浮點數。
Gets a half-precision float from the stream.
Array get_partial_data(bytes: int) 🔗
Returns a chunk data with the received bytes, as an Array containing two elements: an Error constant and a PackedByteArray. bytes is the number of bytes to be received. If not enough bytes are available, the function will return how many were actually received.
String get_string(bytes: int = -1) 🔗
從流中獲取一個位元組長度為 bytes 的 ASCII 字串。如果 bytes 為負(預設),會按照 put_string() 的逆向操作從流中讀取長度。
從流中獲取一個無符號位元組。
從流中獲取一個無符號 16 位元值。
從流中獲取一個無符號 32 位元值。
從流中獲取一個無符號 64 位元值。
String get_utf8_string(bytes: int = -1) 🔗
從流中獲取一個位元組長度為 bytes 的 UTF-8 字串(將發送的字串解碼為 UTF-8)。如果 bytes 為負(預設),會按照 put_utf8_string() 的逆向操作從流中讀取長度。
Variant get_var(allow_objects: bool = false) 🔗
從流中獲取一個 Variant。如果 allow_objects 為 true,則會允許解碼出物件。
內部實作時,使用的解碼機制與 @GlobalScope.bytes_to_var() 方法相同。
警告:反序列化的物件可能包含會被執行的程式碼。如果序列化的物件來自不可信的來源,請勿使用該選項,以免造成遠端程式碼執行等安全威脅。
向流中放入一個有符號位元組。
向流中放入一個有符號 16 位元值。
向流中放入一個有符號 32 位元值。
向流中放入一個有符號 64 位元值。
Error put_data(data: PackedByteArray) 🔗
通過連接發送塊資料,資料完成發送前會阻塞。該函式返回 Error 錯誤碼。
void put_double(value: float) 🔗
向流中放入一個雙精度浮點數。
void put_float(value: float) 🔗
向流中放入一個單精確度浮點數。
Puts a half-precision float into the stream.
Array put_partial_data(data: PackedByteArray) 🔗
通過連接發送資料。如果資料無法一次性發完,則僅會發送部分資料。該函式返回兩個值,一個 Error 錯誤碼以及一個整數,表示實際發送的資料量。
void put_string(value: String) 🔗
向流中放入一個以零結尾的 ASCII 字串,會前置一個表示其大小的 32 位元無符號整數。
注意:如果要放置 ASCII 字串,而不前置大小,可以使用 put_data():
put_data("Hello world".to_ascii_buffer())
PutData("Hello World".ToAsciiBuffer());
向流中放入一個無符號位元組。
向流中放入一個無符號 16 位元值。
向流中放入一個無符號 32 位元值。
向流中放入一個無符號 64 位元值。
void put_utf8_string(value: String) 🔗
向流中放入一個以零結尾的 UTF-8 字串,前置一個表示其大小的 32 位元無符號整數。
注意:如果要放置 UTF-8 字串,而不前置其大小,可以使用 put_data():
put_data("Hello world".to_utf8_buffer())
PutData("Hello World".ToUtf8Buffer());
void put_var(value: Variant, full_objects: bool = false) 🔗
向流中放入一個 Variant。如果 full_objects 為 true,則會允許將物件編碼(其中可能包含程式碼)。
內部實作時,使用的編碼機制與 @GlobalScope.var_to_bytes() 方法相同。