Skip to content
This repository was archived by the owner on May 31, 2024. It is now read-only.

Networking

LennyPhoenix edited this page Jun 2, 2021 · 7 revisions

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

Contents

Network Manager

Discord.NetworkManager

Extends Object.

Description

Contains methods for networking.

Methods

Get Peer ID

get_peer_id() -> int

Get the networking peer ID for the current user, allowing other users to send packets to them.

Returns

int - The peer ID for the current user.

Example

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

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.

Returns

Discord.Result - The result of the operation.

Example

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

open_peer(peer_id: int, route: String) -> Discord.Result

Opens a network connection to another Discord user.

Arguments

  • peer_id: int - The peer ID of the user to connect to.

  • route: String - The route the user is currently broadcasting.

Returns

Discord.Result - The result of the operation.

Example

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

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.

Arguments

  • peer_id: int - The user's peer ID.

  • route: String - The new route for the user.

Returns

Discord.Result - The result of the operation.

Example

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

close_peer(peer_id: int) -> Discord.Result

Disconnects the network session from another Discord user.

Arguments

  • peer_id: int - The user's peer ID.

Returns

Discord.Result - The result of the operation.

Example

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

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.

Arguments

  • 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.

Returns

Discord.Result - The result of the operation.

Example

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

close_channel(peer_id: int, channel_id: int) -> Discord.Result

Close the connection to a given user on the given channel.

Arguments

  • 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).

Returns

Discord.Result - The result of the operation.

Example

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

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.

Arguments

  • 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.

Returns

Discord.Result - The result of the operation.

Example

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)

Signals

Message

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.

Arguments

  • 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.

Example

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_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.

Arguments

  • route: String - The new route to the user.

Example

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)

Clone this wiki locally