-
Notifications
You must be signed in to change notification settings - Fork 3
Networking
This module (and its corresponding manager) provides low-level networking for your game through Discord, allowing you to update routes, open channels, and handle events directly emitted by the SDK.
- Functions as a connection-oriented, TCP-like API, but over UDP
- Supports "reliable" and "unreliable" connections
- Packets with loot in them always get there, but player positioning can be eventually consistent
- Features P2P-like connections, but routed through Discord's high-end server infrastructure
- Has all the benefits of direct connections, without the IP leaks
- Provides encryption
Discord.NetworkManager
Extends Object.
Contains methods for networking.
get_peer_id() -> int
Get the networking peer ID for the current user, allowing other users to send packets to them.
int - The peer ID for the current user.
var discord: Discord.Core
var networking: Discord.NetworkManager
func _ready() -> void:
# ...
var peer_id: int = networking.get_peer_id()
print("Current peer ID: ", peer_id)
flush() -> Discord.Result
Flushes the network. Run this at the end of your game's loop, once you've finished sending all you need to send.
Discord.Result - The result of the operation.
var discord: Discord.Core
var networking: Discord.NetworkManager
func _process(_delta: float) -> void:
if networking:
var result: int = networking.flush()
if result != Discord.Result.OK:
print("Failed to flush network: ", result)
open_peer(peer_id: int, route: String) -> Discord.Result
Opens a network connection to another Discord user.
-
peer_id: int- The peer ID of the user to connect to. -
route: String- The route the user is currently broadcasting.
Discord.Result - The result of the operation.
var discord: Discord.Core
var networking: Discord.NetworkManager
func _ready() -> void:
# ...
var result: int = networking.open_peer(
10198723409184,
"ZyLwh5mAOnJ1HRYrr5LfxtTSIfaNitgE="
)
if result == Discord.Result.OK:
print("Opened peer connection.")
else:
print("Failed to open peer connection: ", result)
update_peer(peer_id: int, route: String) -> Discord.Result
Updates the network connection to another Discord user. You'll want to call this when notified that the route for a user to which you are connected has changed, most likely from a lobby member update event.
-
peer_id: int- The user's peer ID. -
route: String- The new route for the user.
Discord.Result - The result of the operation.
var discord: Discord.Core
var networking: Discord.NetworkManager
func _ready() -> void:
# ...
var result: int = networking.open_peer(
10198723409184,
"ZyLwh5mAOnJ1HRYrr5LfxtTSIfaNitgE="
)
if result != Discord.Result.OK:
print("Failed to open peer connection: ", result)
return
result = networking.update_peer(
10198723409184,
"ZyLwh1dhI821hryRR5lFXTTSIfaNitgE="
)
if result == Discord.Result.OK:
print("Updated peer connection.")
else:
print("Failed to update peer connection: ", result)
close_peer(peer_id: int) -> Discord.Result
Disconnects the network session from another Discord user.
-
peer_id: int- The user's peer ID.
Discord.Result - The result of the operation.
var discord: Discord.Core
var networking: Discord.NetworkManager
func _ready() -> void:
# ...
var result: int = networking.open_peer(
10198723409184,
"ZyLwh5mAOnJ1HRYrr5LfxtTSIfaNitgE="
)
if result != Discord.Result.OK:
print("Failed to open peer connection: ", result)
return
result = networking.close_peer(10198723409184)
if result == Discord.Result.OK:
print("Closed peer connection.")
else:
print("Failed to close peer connection: ", result)
open_channel(peer_id: int, channel_id: int, reliable: bool) -> Discord.Result
Opens a channel to a user with their given peer ID on the given channel number.
Requires the connection to have been created with open_peer.
-
peer_id: int- The peer ID of the user to connect to. -
channel_id: int- The channel ID to bind to, an integer in the range of 0-127 (7-bits). -
reliable: bool- Whether the channel should be unreliable or reliable.
Discord.Result - The result of the operation.
var discord: Discord.Core
var networking: Discord.NetworkManager
func _ready() -> void:
# ...
var result: int = networking.open_peer(
10198723409184,
"ZyLwh5mAOnJ1HRYrr5LfxtTSIfaNitgE="
)
if result != Discord.Result.OK:
print("Failed to open peer connection: ", result)
return
result = networking.open_channel(10198723409184, 0, true)
if result != Discord.Result.OK:
print("Failed to open channel: ", result)
else:
print("Opened channel successfully!")
close_channel(peer_id: int, channel_id: int) -> Discord.Result
Close the connection to a given user on the given channel.
-
peer_id: int- The peer ID of the user to connect to. -
channel_id: int- The channel ID to bind to, an integer in the range of 0-127 (7-bits).
Discord.Result - The result of the operation.
var discord: Discord.Core
var networking: Discord.NetworkManager
func _ready() -> void:
# ...
var result: int = networking.open_peer(
10198723409184,
"ZyLwh5mAOnJ1HRYrr5LfxtTSIfaNitgE="
)
if result != Discord.Result.OK:
print("Failed to open peer connection: ", result)
return
result = networking.open_channel(10198723409184, 0, true)
if result != Discord.Result.OK:
print("Failed to open channel: ", result)
return
result = networking.close_channel(10198723409184, 0)
if result != Discord.Result.OK:
print("Failed to close channel: ", result)
else:
print("Closed channel successfully!")
send_message(peer_id: int, channel_id: int, data: PoolByteArray) -> Discord.Result
Sends data to a given peer ID through the given channel.
Requires the channel to be open with open_channel.
-
peer_id: int- The peer ID of the user to send a message to. -
channel_id: int- The channel ID to send a message through. -
data: PoolByteArray- The data to send to the user.
Discord.Result - The result of the operation.
var discord: Discord.Core
var networking: Discord.NetworkManager
func _ready() -> void:
# ...
var result: int = networking.open_peer(
10198723409184,
"ZyLwh5mAOnJ1HRYrr5LfxtTSIfaNitgE="
)
if result != Discord.Result.OK:
print("Failed to open peer connection: ", result)
return
result = networking.open_channel(10198723409184, 0, true)
if result != Discord.Result.OK:
print("Failed to open channel: ", result)
return
var message: PoolByteArray = "Hello, World!".to_utf8()
result = networking.send_message(10198723409184, 0, message)
if result != Discord.Result.OK:
print("Failed to send message: ", result)
else:
print("Sent message successfully!")
func _process(_delta: float) -> void:
# ...
var result: int = networking.flush()
if result != Discord.Result.OK:
print("Failed to flush network: ", result)
message(peer_id: int, channel_id: int, data: PoolByteArray)
Fires when a network message is received from another user. This callback will only fire if you already have an open channel with the user sending you data.
-
peer_id: int- The peer ID of the sender. -
channel_id: int- The channel ID the message was sent through. -
data: PoolByteArray- The message data that was sent.
var discord: Discord.Core
var networking: Discord.NetworkManager
func _ready() -> void:
# ...
networking.connect(
"message",
self, "_on_message"
)
func _on_message(peer_id: int, channel_id: int, data: PoolByteArray) -> void:
print(
"Received message from ", peer_id,
" on ", channel_id, ": ", data.get_string_from_utf8()
)
route_update(route: String)
Fires when your networking route has changed. You should broadcast to other users to whom you are connected that this has changed, probably by updating your lobby member metadata for others to receive.
-
route: String- The new route to the user.
var discord: Discord.Core
var networking: Discord.NetworkManager
func _ready() -> void:
# ...
networking.connect(
"route_update",
self, "_on_route_update"
)
func _on_route_update(route: String) -> void:
print("Route update: ", route)