mirror of
https://github.com/WolfangT/pixel-crusade.git
synced 2026-02-16 15:21:15 -05:00
initial files
This commit is contained in:
117
src/networking/multiplayer_client.gd
Normal file
117
src/networking/multiplayer_client.gd
Normal file
@@ -0,0 +1,117 @@
|
||||
extends "ws_webrtc_client.gd"
|
||||
|
||||
var rtc_mp := WebRTCMultiplayerPeer.new()
|
||||
var sealed := false
|
||||
|
||||
signal finished_connection()
|
||||
|
||||
func _init() -> void:
|
||||
connected.connect(_connected)
|
||||
disconnected.connect(_disconnected)
|
||||
|
||||
offer_received.connect(_offer_received)
|
||||
answer_received.connect(_answer_received)
|
||||
candidate_received.connect(_candidate_received)
|
||||
|
||||
lobby_joined.connect(_lobby_joined)
|
||||
lobby_sealed.connect(_lobby_sealed)
|
||||
peer_connected.connect(_peer_connected)
|
||||
peer_disconnected.connect(_peer_disconnected)
|
||||
|
||||
|
||||
func start(url: String, _lobby: String = "", _mesh: bool = true) -> void:
|
||||
stop()
|
||||
sealed = false
|
||||
mesh = _mesh
|
||||
lobby = _lobby
|
||||
connect_to_url(url)
|
||||
|
||||
|
||||
func stop() -> void:
|
||||
multiplayer.multiplayer_peer = null
|
||||
rtc_mp.close()
|
||||
close()
|
||||
|
||||
|
||||
func _create_peer(id: int) -> WebRTCPeerConnection:
|
||||
var peer: WebRTCPeerConnection = WebRTCPeerConnection.new()
|
||||
# Use a public STUN server for moderate NAT traversal.
|
||||
# Note that STUN cannot punch through strict NATs (such as most mobile connections),
|
||||
# in which case TURN is required. TURN generally does not have public servers available,
|
||||
# as it requires much greater resources to host (all traffic goes through
|
||||
# the TURN server, instead of only performing the initial connection).
|
||||
peer.initialize({
|
||||
"iceServers": [ {"urls": ["stun:stun.l.google.com:19302"]}]
|
||||
})
|
||||
peer.session_description_created.connect(_offer_created.bind(id))
|
||||
peer.ice_candidate_created.connect(_new_ice_candidate.bind(id))
|
||||
rtc_mp.add_peer(peer, id)
|
||||
if id < rtc_mp.get_unique_id(): # So lobby creator never creates offers.
|
||||
peer.create_offer()
|
||||
return peer
|
||||
|
||||
|
||||
func _new_ice_candidate(mid_name: String, index_name: int, sdp_name: String, id: int) -> void:
|
||||
send_candidate(id, mid_name, index_name, sdp_name)
|
||||
|
||||
|
||||
func _offer_created(type: String, data: String, id: int) -> void:
|
||||
if not rtc_mp.has_peer(id):
|
||||
return
|
||||
print("created", type)
|
||||
rtc_mp.get_peer(id).connection.set_local_description(type, data)
|
||||
if type == "offer": send_offer(id, data)
|
||||
else: send_answer(id, data)
|
||||
|
||||
|
||||
func _connected(id: int, use_mesh: bool) -> void:
|
||||
print("Connected %d, mesh: %s" % [id, use_mesh])
|
||||
if use_mesh:
|
||||
rtc_mp.create_mesh(id)
|
||||
elif id == 1:
|
||||
rtc_mp.create_server()
|
||||
else:
|
||||
rtc_mp.create_client(id)
|
||||
multiplayer.multiplayer_peer = rtc_mp
|
||||
finished_connection.emit()
|
||||
|
||||
|
||||
func _lobby_joined(_lobby: String) -> void:
|
||||
lobby = _lobby
|
||||
|
||||
|
||||
func _lobby_sealed() -> void:
|
||||
sealed = true
|
||||
|
||||
|
||||
func _disconnected() -> void:
|
||||
print("Disconnected: %d: %s" % [code, reason])
|
||||
if not sealed:
|
||||
stop() # Unexpected disconnect
|
||||
|
||||
|
||||
func _peer_connected(id: int) -> void:
|
||||
print("Peer connected: %d" % id)
|
||||
_create_peer(id)
|
||||
|
||||
|
||||
func _peer_disconnected(id: int) -> void:
|
||||
if rtc_mp.has_peer(id):
|
||||
rtc_mp.remove_peer(id)
|
||||
|
||||
|
||||
func _offer_received(id: int, offer: String) -> void:
|
||||
print("Got offer: %d" % id)
|
||||
if rtc_mp.has_peer(id):
|
||||
rtc_mp.get_peer(id).connection.set_remote_description("offer", offer)
|
||||
|
||||
|
||||
func _answer_received(id: int, answer: String) -> void:
|
||||
print("Got answer: %d" % id)
|
||||
if rtc_mp.has_peer(id):
|
||||
rtc_mp.get_peer(id).connection.set_remote_description("answer", answer)
|
||||
|
||||
|
||||
func _candidate_received(id: int, mid: String, index: int, sdp: String) -> void:
|
||||
if rtc_mp.has_peer(id):
|
||||
rtc_mp.get_peer(id).connection.add_ice_candidate(mid, index, sdp)
|
||||
1
src/networking/multiplayer_client.gd.uid
Normal file
1
src/networking/multiplayer_client.gd.uid
Normal file
@@ -0,0 +1 @@
|
||||
uid://cs4vx0irsgyv4
|
||||
128
src/networking/ws_webrtc_client.gd
Normal file
128
src/networking/ws_webrtc_client.gd
Normal file
@@ -0,0 +1,128 @@
|
||||
extends Node
|
||||
|
||||
enum Message {
|
||||
JOIN,
|
||||
ID,
|
||||
PEER_CONNECT,
|
||||
PEER_DISCONNECT,
|
||||
OFFER,
|
||||
ANSWER,
|
||||
CANDIDATE,
|
||||
SEAL,
|
||||
}
|
||||
|
||||
@export var autojoin := true
|
||||
@export var lobby := "" # Will create a new lobby if empty.
|
||||
@export var mesh := true # Will use the lobby host as relay otherwise.
|
||||
|
||||
var ws := WebSocketPeer.new()
|
||||
var code := 1000
|
||||
var reason := "Unknown"
|
||||
var old_state := WebSocketPeer.STATE_CLOSED
|
||||
|
||||
signal lobby_joined(lobby: String)
|
||||
signal connected(id: int, use_mesh: bool)
|
||||
signal disconnected()
|
||||
signal peer_connected(id: int)
|
||||
signal peer_disconnected(id: int)
|
||||
signal offer_received(id: int, offer: String)
|
||||
signal answer_received(id: int, answer: String)
|
||||
signal candidate_received(id: int, mid: String, index: int, sdp: String)
|
||||
signal lobby_sealed()
|
||||
|
||||
|
||||
func connect_to_url(url: String) -> void:
|
||||
close()
|
||||
code = 1000
|
||||
reason = "Unknown"
|
||||
ws.connect_to_url(url, TLSOptions.client_unsafe())
|
||||
|
||||
|
||||
func close() -> void:
|
||||
ws.close()
|
||||
|
||||
|
||||
func _process(_delta: float) -> void:
|
||||
ws.poll()
|
||||
var state := ws.get_ready_state()
|
||||
if state != old_state and state == WebSocketPeer.STATE_OPEN and autojoin:
|
||||
join_lobby(lobby)
|
||||
while state == WebSocketPeer.STATE_OPEN and ws.get_available_packet_count():
|
||||
if not _parse_msg():
|
||||
print("Error parsing message from server.")
|
||||
if state != old_state and state == WebSocketPeer.STATE_CLOSED:
|
||||
code = ws.get_close_code()
|
||||
reason = ws.get_close_reason()
|
||||
disconnected.emit()
|
||||
old_state = state
|
||||
|
||||
|
||||
func _parse_msg() -> bool:
|
||||
var parsed: Dictionary = JSON.parse_string(ws.get_packet().get_string_from_utf8())
|
||||
if typeof(parsed) != TYPE_DICTIONARY or not parsed.has("type") or not parsed.has("id") or \
|
||||
typeof(parsed.get("data")) != TYPE_STRING:
|
||||
return false
|
||||
|
||||
var msg := parsed as Dictionary
|
||||
|
||||
var type := int(msg.type)
|
||||
var src_id := int(msg.id)
|
||||
|
||||
if type == Message.ID:
|
||||
connected.emit(src_id, msg.data == "true")
|
||||
elif type == Message.JOIN:
|
||||
lobby_joined.emit(msg.data)
|
||||
elif type == Message.SEAL:
|
||||
lobby_sealed.emit()
|
||||
elif type == Message.PEER_CONNECT:
|
||||
# Client connected.
|
||||
peer_connected.emit(src_id)
|
||||
elif type == Message.PEER_DISCONNECT:
|
||||
# Client connected.
|
||||
peer_disconnected.emit(src_id)
|
||||
elif type == Message.OFFER:
|
||||
# Offer received.
|
||||
offer_received.emit(src_id, msg.data)
|
||||
elif type == Message.ANSWER:
|
||||
# Answer received.
|
||||
answer_received.emit(src_id, msg.data)
|
||||
elif type == Message.CANDIDATE:
|
||||
# Candidate received.
|
||||
var candidate: PackedStringArray = msg.data.split("\n", false)
|
||||
if candidate.size() != 3:
|
||||
return false
|
||||
if not candidate[1].is_valid_int():
|
||||
return false
|
||||
candidate_received.emit(src_id, candidate[0], candidate[1].to_int(), candidate[2])
|
||||
else:
|
||||
return false
|
||||
|
||||
return true # Parsed.
|
||||
|
||||
|
||||
func join_lobby(_lobby: String) -> Error:
|
||||
return _send_msg(Message.JOIN, 0 if mesh else 1, _lobby)
|
||||
|
||||
|
||||
func seal_lobby() -> Error:
|
||||
return _send_msg(Message.SEAL, 0)
|
||||
|
||||
|
||||
func send_candidate(id: int, mid: String, index: int, sdp: String) -> Error:
|
||||
return _send_msg(Message.CANDIDATE, id, "\n%s\n%d\n%s" % [mid, index, sdp])
|
||||
|
||||
|
||||
func send_offer(id: int, offer: String) -> Error:
|
||||
return _send_msg(Message.OFFER, id, offer)
|
||||
|
||||
|
||||
func send_answer(id: int, answer: String) -> Error:
|
||||
return _send_msg(Message.ANSWER, id, answer)
|
||||
|
||||
|
||||
func _send_msg(type: int, id: int, data: String = "") -> Error:
|
||||
return ws.send_text(JSON.stringify({
|
||||
"type": type,
|
||||
"id": id,
|
||||
"data": data,
|
||||
}))
|
||||
1
src/networking/ws_webrtc_client.gd.uid
Normal file
1
src/networking/ws_webrtc_client.gd.uid
Normal file
@@ -0,0 +1 @@
|
||||
uid://bhsrou3v8bum
|
||||
Reference in New Issue
Block a user