WebSocketServer Class |
This class implements a WebSocket server that can also support the high-level multiplayer API.
After starting the server (Listen(Int32, String, Boolean)), you will need to Poll it at regular intervals (e.g. inside _Process(Single)). When clients connect, disconnect, or send data, you will receive the appropriate signal.
Note: Not available in HTML5 exports.
Namespace: Godot
public class WebSocketServer : WebSocketMultiplayerPeer
The WebSocketServer type exposes the following members.
Name | Description | |
---|---|---|
WebSocketServer | Initializes a new instance of the WebSocketServer class |
Name | Description | |
---|---|---|
AllowObjectDecoding | Deprecated. Use get_var and put_var parameters instead. If true, the PacketPeer will allow encoding and decoding of object via GetVar(Boolean) and PutVar(Object, Boolean). Warning: Deserialized objects can contain code which gets executed. Do not use this option if the serialized object comes from untrusted sources to avoid potential security threats such as remote code execution. | |
BindIp | When not set to * will restrict incoming connections to the specified IP address. Setting bind_ip to 127.0.0.1 will cause the server to listen only to the local host. | |
CaChain | When using SSL (see PrivateKey and SslCertificate), you can set this to a valid X509Certificate to be provided as additional CA chain information during the SSL handshake. | |
DynamicObject |
Gets a new DynamicGodotObject associated with this instance.
(Inherited from Object.) | |
EncodeBufferMaxSize | Maximum buffer size allowed when encoding Variants. Raise this value to support heavier memory allocations. The PutVar(Object, Boolean) method allocates memory on the stack, and the buffer used will grow automatically to the closest power of two to match the size of the Variant. If the Variant is bigger than encode_buffer_max_size, the method will error out with . | |
NativeInstance | (Inherited from Object.) | |
PrivateKey | When set to a valid CryptoKey (along with SslCertificate) will cause the server to require SSL instead of regular TCP (i.e. the wss:// protocol). | |
RefuseNewConnections | If true, this NetworkedMultiplayerPeer refuses new connections. | |
SslCertificate | When set to a valid X509Certificate (along with PrivateKey) will cause the server to require SSL instead of regular TCP (i.e. the wss:// protocol). | |
TransferMode | The manner in which to send packets to the target_peer. See NetworkedMultiplayerPeerTransferModeEnum. |
Name | Description | |
---|---|---|
_Get | Virtual method which can be overridden to customize the return value of Get(String). Returns the given property. Returns null if the property does not exist. | |
_GetPropertyList | Virtual method which can be overridden to customize the return value of GetPropertyList. Returns the object's property list as an Array of dictionaries. Each property's Dictionary must contain at least name: String and type: int (see VariantType) entries. Optionally, it can also include hint: int (see PropertyHint), hint_string: String, and usage: int (see PropertyUsageFlags). | |
_Notification | Called whenever the object receives a notification, which is identified in what by a constant. The base Object has two constants and , but subclasses such as Node define a lot more notifications which are also received by this method. | |
_Set | Virtual method which can be overridden to customize the return value of Set(String, Object). Sets a property. Returns true if the property exists. | |
AddUserSignal | Adds a user-defined signal. Arguments are optional, but can be added as an Array of dictionaries, each containing name: String and type: int (see VariantType) entries. | |
Call | Calls the method on the object and returns the result. This method supports a variable number of arguments, so parameters are passed as a comma separated list. Example: call("set", "position", Vector2(42.0, 0.0)) Note: In C#, the method name must be specified as snake_case if it is defined by a built-in Godot node. This doesn't apply to user-defined methods where you should use the same convention as in the C# source (typically PascalCase). | |
CallDeferred | Calls the method on the object during idle time. This method supports a variable number of arguments, so parameters are passed as a comma separated list. Example: call_deferred("set", "position", Vector2(42.0, 0.0)) Note: In C#, the method name must be specified as snake_case if it is defined by a built-in Godot node. This doesn't apply to user-defined methods where you should use the same convention as in the C# source (typically PascalCase). | |
Callv | Calls the method on the object and returns the result. Contrarily to Call(String, Object), this method does not support a variable number of arguments but expects all parameters to be via a single Array. callv("set", [ "position", Vector2(42.0, 0.0) ]) | |
CanTranslateMessages | Returns true if the object can translate strings. See SetMessageTranslation(Boolean) and Tr(String). | |
Connect | Connects a signal to a method on a target object. Pass optional binds to the call as an Array of parameters. These parameters will be passed to the method after any parameter used in the call to EmitSignal(String, Object). Use flags to set deferred or one-shot connections. See ObjectConnectFlags constants. A signal can only be connected once to a method. It will throw an error if already connected, unless the signal was connected with . To avoid this, first, use IsConnected(String, Object, String) to check for existing connections. If the target is destroyed in the game's lifecycle, the connection will be lost. Examples: connect("pressed", self, "_on_Button_pressed") # BaseButton signal connect("text_entered", self, "_on_LineEdit_text_entered") # LineEdit signal connect("hit", self, "_on_Player_hit", [ weapon_type, damage ]) # User-defined signal An example of the relationship between binds passed to Connect(String, Object, String, Array, UInt32) and parameters used when calling EmitSignal(String, Object): connect("hit", self, "_on_Player_hit", [ weapon_type, damage ]) # weapon_type and damage are passed last emit_signal("hit", "Dark lord", 5) # "Dark lord" and 5 are passed first func _on_Player_hit(hit_by, level, weapon_type, damage): print("Hit by %s (lvl %d) with weapon %s for %d damage" % [hit_by, level, weapon_type, damage]) | |
Disconnect | Disconnects a signal from a method on the given target. If you try to disconnect a connection that does not exist, the method will throw an error. Use IsConnected(String, Object, String) to ensure that the connection exists. | |
DisconnectPeer | Disconnects the peer identified by id from the server. See Close(Int32, String) for more information. | |
Dispose | (Inherited from Object.) | |
Dispose(Boolean) | (Inherited from Object.) | |
EmitSignal | Emits the given signal. The signal must exist, so it should be a built-in signal of this class or one of its parent classes, or a user-defined signal. This method supports a variable number of arguments, so parameters are passed as a comma separated list. Example: emit_signal("hit", weapon_type, damage) emit_signal("game_over") | |
Equals | (Inherited from Object.) | |
Finalize | (Inherited from Object.) | |
Free | Deletes the object from memory immediately. For Nodes, you may want to use QueueFree to queue the node for safe deletion at the end of the current frame. Important: If you have a variable pointing to an object, it will not be assigned to null once the object is freed. Instead, it will point to a previously freed instance and you should validate it with @GDScript.is_instance_valid before attempting to call its methods or access its properties. | |
Get | Returns the Variant value of the given property. If the property doesn't exist, this will return null. Note: In C#, the property name must be specified as snake_case if it is defined by a built-in Godot node. This doesn't apply to user-defined properties where you should use the same convention as in the C# source (typically PascalCase). | |
GetAvailablePacketCount | Returns the number of packets currently available in the ring-buffer. | |
GetBindIp | Obsolete. | |
GetCaChain | Obsolete. | |
GetClass | Returns the object's class as a String. | |
GetConnectionStatus | Returns the current state of the connection. See NetworkedMultiplayerPeerConnectionStatus. | |
GetEncodeBufferMaxSize | Obsolete. (Inherited from PacketPeer.) | |
GetHashCode | (Inherited from Object.) | |
GetIncomingConnections | Returns an Array of dictionaries with information about signals that are connected to the object. Each Dictionary contains three String entries: - source is a reference to the signal emitter. - signal_name is the name of the connected signal. - method_name is the name of the method to which the signal is connected. | |
GetIndexed | Gets the object's property indexed by the given NodePath. The node path should be relative to the current object and can use the colon character (:) to access nested properties. Examples: "position:x" or "material:next_pass:blend_mode". | |
GetInstanceId | Returns the object's unique instance ID. This ID can be saved in EncodedObjectAsID, and can be used to retrieve the object instance with @GDScript.instance_from_id. | |
GetMeta | Returns the object's metadata entry for the given name. | |
GetMetaList | Returns the object's metadata as a String. | |
GetMethodList | Returns the object's methods and their signatures as an Array. | |
GetPacket | Gets a raw packet. | |
GetPacketError | Returns the error state of the last packet received (via GetPacket and GetVar(Boolean)). | |
GetPacketPeer | Returns the ID of the NetworkedMultiplayerPeer who sent the most recent packet. | |
GetPeer | Returns the WebSocketPeer associated to the given peer_id. | |
GetPeerAddress | Returns the IP address of the given peer. | |
GetPeerPort | Returns the remote port of the given peer. | |
GetPrivateKey | Obsolete. | |
GetPropertyList | Returns the object's property list as an Array of dictionaries. Each property's Dictionary contain at least name: String and type: int (see VariantType) entries. Optionally, it can also include hint: int (see PropertyHint), hint_string: String, and usage: int (see PropertyUsageFlags). | |
GetScript | Returns the object's Script instance, or null if none is assigned. | |
GetSignalConnectionList | Returns an Array of connections for the given signal. | |
GetSignalList | Returns the list of signals as an Array of dictionaries. | |
GetSslCertificate | Obsolete. | |
GetTransferMode | Obsolete. (Inherited from NetworkedMultiplayerPeer.) | |
GetType | (Inherited from Object.) | |
GetUniqueId | Returns the ID of this NetworkedMultiplayerPeer. | |
GetVar | Gets a Variant. If allow_objects (or AllowObjectDecoding) is true, decoding objects is allowed. Warning: Deserialized objects can contain code which gets executed. Do not use this option if the serialized object comes from untrusted sources to avoid potential security threats such as remote code execution. | |
HasMeta | Returns true if a metadata entry is found with the given name. | |
HasMethod | Returns true if the object contains the given method. | |
HasPeer | Returns true if a peer with the given ID is connected. | |
HasSignal | Returns true if the given signal exists. | |
HasUserSignal | Returns true if the given user-defined signal exists. Only signals added using AddUserSignal(String, Array) are taken into account. | |
InitRef | Initializes the internal reference counter. Use this only if you really know what you are doing. Returns whether the initialization was successful. | |
IsBlockingSignals | Returns true if signal emission blocking is enabled. | |
IsClass | Returns true if the object inherits from the given class. | |
IsConnected | Returns true if a connection exists for a given signal, target, and method. | |
IsListening | Returns true if the server is actively listening on a port. | |
IsObjectDecodingAllowed | Obsolete. (Inherited from PacketPeer.) | |
IsQueuedForDeletion | Returns true if the QueueFree method was called for the object. | |
IsRefusingNewConnections | Obsolete. (Inherited from NetworkedMultiplayerPeer.) | |
Listen | Starts listening on the given port. You can specify the desired subprotocols via the "protocols" array. If the list empty (default), no sub-protocol will be requested. If true is passed as gd_mp_api, the server will behave like a network peer for the MultiplayerAPI, connections from non-Godot clients will not work, and data_received will not be emitted. If false is passed instead (default), you must call PacketPeer functions (put_packet, get_packet, etc.), on the WebSocketPeer returned via get_peer(id) to communicate with the peer with given id (e.g. get_peer(id).get_available_packet_count). | |
MemberwiseClone | (Inherited from Object.) | |
Notification | Send a given notification to the object, which will also trigger a call to the _Notification(Int32) method of all classes that the object inherits from. If reversed is true, _Notification(Int32) is called first on the object's own class, and then up to its successive parent classes. If reversed is false, _Notification(Int32) is called first on the highest ancestor (Object itself), and then down to its successive inheriting classes. | |
Poll | Waits up to 1 second to receive a new network event. | |
PropertyListChangedNotify | Notify the editor that the property list has changed, so that editor plugins can take the new values into account. Does nothing on export builds. | |
PutPacket | Sends a raw packet. | |
PutVar | Sends a Variant as a packet. If full_objects (or AllowObjectDecoding) is true, encoding objects is allowed (and can potentially include code). | |
Reference_ | Increments the internal reference counter. Use this only if you really know what you are doing. Returns true if the increment was successful, false otherwise. | |
RemoveMeta | Removes a given entry from the object's metadata. See also SetMeta(String, Object). | |
Set | Assigns a new value to the given property. If the property does not exist, nothing will happen. Note: In C#, the property name must be specified as snake_case if it is defined by a built-in Godot node. This doesn't apply to user-defined properties where you should use the same convention as in the C# source (typically PascalCase). | |
SetAllowObjectDecoding | Obsolete. (Inherited from PacketPeer.) | |
SetBindIp | Obsolete. | |
SetBlockSignals | If set to true, signal emission is blocked. | |
SetBuffers | Configures the buffer sizes for this WebSocket peer. Default values can be specified in the Project Settings under network/limits. For server, values are meant per connected peer. The first two parameters define the size and queued packets limits of the input buffer, the last two of the output buffer. Buffer sizes are expressed in KiB, so 4 = 2^12 = 4096 bytes. All parameters will be rounded up to the nearest power of two. Note: HTML5 exports only use the input buffer since the output one is managed by browsers. | |
SetCaChain | Obsolete. | |
SetDeferred | Assigns a new value to the given property, after the current frame's physics step. This is equivalent to calling Set(String, Object) via CallDeferred(String, Object), i.e. call_deferred("set", property, value). Note: In C#, the property name must be specified as snake_case if it is defined by a built-in Godot node. This doesn't apply to user-defined properties where you should use the same convention as in the C# source (typically PascalCase). | |
SetEncodeBufferMaxSize | Obsolete. (Inherited from PacketPeer.) | |
SetIndexed | Assigns a new value to the property identified by the NodePath. The node path should be relative to the current object and can use the colon character (:) to access nested properties. Example: set_indexed("position", Vector2(42, 0)) set_indexed("position:y", -10) print(position) # (42, -10) | |
SetMessageTranslation | Defines whether the object can translate strings (with calls to Tr(String)). Enabled by default. | |
SetMeta | Adds, changes or removes a given entry in the object's metadata. Metadata are serialized and can take any Variant value. To remove a given entry from the object's metadata, use RemoveMeta(String). Metadata is also removed if its value is set to null. This means you can also use set_meta("name", null) to remove metadata for "name". | |
SetPrivateKey | Obsolete. | |
SetRefuseNewConnections | Obsolete. (Inherited from NetworkedMultiplayerPeer.) | |
SetScript | Assigns a script to the object. Each object can have a single script assigned to it, which are used to extend its functionality. If the object already had a script, the previous script instance will be freed and its variables and state will be lost. The new script's method will be called. | |
SetSslCertificate | Obsolete. | |
SetTargetPeer | Sets the peer to which packets will be sent. The id can be one of: to send to all connected peers, to send to the peer acting as server, a valid peer ID to send to that specific peer, a negative peer ID to send to all peers except that one. By default, the target peer is . | |
SetTransferMode | Obsolete. (Inherited from NetworkedMultiplayerPeer.) | |
Stop | Stops the server and clear its state. | |
ToSignal |
Returns a new SignalAwaiter awaiter configured to complete when the instance
source emits the signal specified by the signal parameter.
(Inherited from Object.) | |
ToString | (Inherited from Object.) | |
Tr | Translates a message using translation catalogs configured in the Project Settings. Only works if message translation is enabled (which it is by default), otherwise it returns the message unchanged. See SetMessageTranslation(Boolean). | |
Unreference | Decrements the internal reference counter. Use this only if you really know what you are doing. Returns true if the decrement was successful, false otherwise. |