Don't touch anything. It works.
This commit is contained in:
parent
292bf5ba5c
commit
ed3e07cf4b
3 changed files with 29 additions and 19 deletions
|
@ -23,7 +23,7 @@ func _ready():
|
|||
signaller.connect("answer_received", self, "answer_received")
|
||||
signaller.connect("candidate_received", self, "candidate_received")
|
||||
|
||||
signaller.connect("lobby_joined", self, "_lobby_joined")
|
||||
signaller.connect("peer_id_set", self, "_peer_id_set")
|
||||
# connect("lobby_sealed", self, "lobby_sealed")
|
||||
signaller.connect("peer_joined", self, "_peer_joined")
|
||||
# connect("peer_disconnected", self, "peer_disconnected")
|
||||
|
@ -38,10 +38,9 @@ func connect_to_signaller():
|
|||
signaller.connect_to_websocket_signaller(multiplayer_url)
|
||||
|
||||
func _create_peer(peer_id):
|
||||
print("_create_peer: %d" % peer_id)
|
||||
if peer_id == signaller.peer_id: return
|
||||
# if peer_id == signaller.peer_id: return
|
||||
var peer: WebRTCPeerConnection = WebRTCPeerConnection.new()
|
||||
print("New Net Peer %s with peer_id %d" % [peer, peer_id])
|
||||
print("New Net Peer with peer_id %d" % [peer_id])
|
||||
var _peer_init_results = peer.initialize({"iceServers": webrtc_ice_servers})
|
||||
peer.connect("session_description_created", self, "_offer_created", [peer_id])
|
||||
peer.connect("ice_candidate_created", self, "_new_ice_candidate", [peer_id])
|
||||
|
@ -68,8 +67,8 @@ func _offer_created(type, data, peer_id):
|
|||
else:
|
||||
signaller.send_answer(peer_id, data)
|
||||
|
||||
func _lobby_joined(id, peer_id):
|
||||
print("Connected to lobby %s as peer %d" % [id, peer_id])
|
||||
func _peer_id_set(peer_id):
|
||||
print("Peer ID received %s - setting up to peer..." % [peer_id])
|
||||
mp.initialize(peer_id, true)
|
||||
get_tree().network_peer = mp
|
||||
|
||||
|
@ -89,6 +88,8 @@ func offer_received(data):
|
|||
if mp.has_peer(id):
|
||||
print("Setting offer remote description: %s" % JSON.print(data))
|
||||
mp.get_peer(id).connection.set_remote_description("offer", data["offer"])
|
||||
else:
|
||||
print("Received an offer for a peer with ID %s that hasn't been added" % id)
|
||||
|
||||
func answer_received(data):
|
||||
var id = data["peerId"]
|
||||
|
|
16
server.ts
16
server.ts
|
@ -185,7 +185,7 @@ class Lobby {
|
|||
}),
|
||||
);
|
||||
this.clients.forEach((c) =>
|
||||
c.send(buildMessage("peer_joined", {
|
||||
client.send(buildMessage("peer_joined", {
|
||||
id: c.id,
|
||||
name: c.name,
|
||||
peerId: c.peerId,
|
||||
|
@ -217,7 +217,6 @@ function onMessage(client: Client, ev: MessageEvent) {
|
|||
if (ev.data === "lobby_create") client.lobbyCreate();
|
||||
if (ev.data === "lobby_leave") client.lobbyLeave();
|
||||
if (ev.data === "request_lobby_list") client.lobbyList();
|
||||
if (ev.data === "request_peer_list") client.clientList();
|
||||
if (ev.data.startsWith("lobby_join:")) {
|
||||
const id = ev.data.substr(11);
|
||||
const lobby = allLobbies.get(id);
|
||||
|
@ -233,15 +232,16 @@ function onMessage(client: Client, ev: MessageEvent) {
|
|||
if (typeof data.data === "object") {
|
||||
const subdata = data.data;
|
||||
if (typeof subdata["peerId"] === "number") {
|
||||
const peerId: number = subdata["peerId"];
|
||||
// const payload = Object.assign({}, data);
|
||||
// payload.data.peerId = client.peerId;
|
||||
const destPeerId: number = subdata["peerId"];
|
||||
for (const iClient of client.lobby?.clients.values() || []) {
|
||||
if (iClient.peerId == peerId) {
|
||||
if (iClient.peerId == destPeerId) {
|
||||
const payload = Object.assign({}, data);
|
||||
const srcPeerId = client.peerId;
|
||||
payload.data.peerId = srcPeerId;
|
||||
console.log(
|
||||
`Forwarding WebRTC Negotiation Message from peer ${client.peerId} to peer ${peerId}...`,
|
||||
`Forwarding WebRTC Negotiation Message from peer ${srcPeerId} to peer ${destPeerId}...`,
|
||||
);
|
||||
iClient.send(ev.data);
|
||||
iClient.send(payload);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -15,6 +15,8 @@ signal peer_left(id)
|
|||
signal candidate_received(data)
|
||||
signal offer_received(data)
|
||||
signal answer_received(data)
|
||||
signal client_id_set(client_id)
|
||||
signal peer_id_set(peer_id)
|
||||
signal websocket_connected
|
||||
signal websocket_disconnected
|
||||
|
||||
|
@ -74,7 +76,6 @@ func _send(s: String):
|
|||
|
||||
func _parse_msg():
|
||||
var msg: String = ws.get_peer(1).get_packet().get_string_from_utf8()
|
||||
print("Signaller received message: %s" % msg)
|
||||
if msg.begins_with("json:"):
|
||||
var data = JSON.parse(msg.substr(5))
|
||||
if data.error == OK:
|
||||
|
@ -84,10 +85,17 @@ func _parse_msg():
|
|||
|
||||
func handle_message(data: Dictionary):
|
||||
match data["type"]:
|
||||
"your_id": client_id = data["data"]
|
||||
"your_peer_id": peer_id = data["data"]
|
||||
"your_id":
|
||||
client_id = data["data"]
|
||||
emit_signal("client_id_set", client_id)
|
||||
"your_peer_id":
|
||||
peer_id = int(data["data"])
|
||||
emit_signal("peer_id_set", peer_id)
|
||||
|
||||
"lobby_list": emit_signal("lobby_new", data["data"])
|
||||
"lobby_new": emit_signal("lobby_new", [{"id": data["id"], "name": data["name"]}])
|
||||
"lobby_delete": emit_signal("lobby_delete", data["id"])
|
||||
|
||||
"lobby_joined":
|
||||
lobby_id = data["id"]
|
||||
peer_id = data["peerId"]
|
||||
|
@ -95,10 +103,10 @@ func handle_message(data: Dictionary):
|
|||
"lobby_left":
|
||||
lobby_id = null
|
||||
emit_signal("lobby_left", data["id"])
|
||||
"lobby_list": emit_signal("lobby_new", data["data"])
|
||||
"peer_list": emit_signal("peer_joined", data["data"])
|
||||
|
||||
"peer_joined": emit_signal("peer_joined", [{"id": data["id"], "name": data["name"], "peerId": data["peerId"]}])
|
||||
"peer_left": emit_signal("peer_left", data["id"])
|
||||
|
||||
"candidate":
|
||||
print("Candidate received - Data: %s" % JSON.print(data["data"]))
|
||||
emit_signal("candidate_received", data["data"])
|
||||
|
@ -108,6 +116,7 @@ func handle_message(data: Dictionary):
|
|||
"answer":
|
||||
print("Answer received - Data: %s" % JSON.print(data["data"]))
|
||||
emit_signal("answer_received", data["data"])
|
||||
|
||||
_: print("Unhandled Message - Data: %s" % JSON.print(data))
|
||||
|
||||
func send_candidate(peerId, mid, index, sdp) -> int:
|
||||
|
|
Loading…
Reference in a new issue