diff --git a/client/ws_webrtc_client.gd b/client/ws_webrtc_client.gd index c5974bf..e50971d 100644 --- a/client/ws_webrtc_client.gd +++ b/client/ws_webrtc_client.gd @@ -1,6 +1,6 @@ extends Node -enum Message {JOIN, ID, PEER_CONNECT, PEER_DISCONNECT, OFFER, ANSWER, CANDIDATE, SEAL} +enum Message {JOIN, ID, PEER_CONNECT, PEER_DISCONNECT, OFFER, ANSWER, CANDIDATE, SEAL, GET_PUBLIC} @export var autojoin := true @export var lobby := "" # Will create a new lobby if empty. @@ -20,6 +20,8 @@ signal answer_received(id, answer) signal candidate_received(id, mid, index, sdp) signal lobby_sealed() +signal public_lobbies_recived(id, list) + func connect_to_url(url): close() @@ -88,13 +90,15 @@ func _parse_msg(): if not candidate[1].is_valid_int(): return false candidate_received.emit(src_id, candidate[0], candidate[1].to_int(), candidate[2]) + elif type == Message.GET_PUBLIC: + public_lobbies_recived.emit(src_id, JSON.parse_string(msg.data)) else: return false return true # Parsed -func join_lobby(lobby: String, single_host:=false): - var data = {"lobby":lobby,"single_host":single_host} +func join_lobby(lobby: String, single_host:=false, public:=false): + var data = {"lobby":lobby,"single_host":single_host,"public":public} return _send_msg(Message.JOIN, 0 if mesh else 1, JSON.stringify(data)) @@ -114,6 +118,10 @@ func send_answer(id, answer) -> int: return _send_msg(Message.ANSWER, id, answer) +func ask_for_public_lobbies(id) -> int: + return _send_msg(Message.GET_PUBLIC, id, "") + + func _send_msg(type: int, id: int, data:="") -> int: return ws.send_text(JSON.stringify({ "type": type, diff --git a/server/ws_webrtc_server.gd b/server/ws_webrtc_server.gd index 138e0cc..061cfef 100644 --- a/server/ws_webrtc_server.gd +++ b/server/ws_webrtc_server.gd @@ -1,6 +1,6 @@ extends Node -enum Message {JOIN, ID, PEER_CONNECT, PEER_DISCONNECT, OFFER, ANSWER, CANDIDATE, SEAL} +enum Message {JOIN, ID, PEER_CONNECT, PEER_DISCONNECT, OFFER, ANSWER, CANDIDATE, SEAL, GET_PUBLIC} const TIMEOUT = 1000 # Unresponsive clients times out after 1 sec const SEAL_TIME = 10000 # A sealed room will be closed after this time @@ -45,11 +45,13 @@ class Lobby extends RefCounted: var time = 0 var mesh := true var single_host := false + var public := false - func _init(host_id: int, use_mesh: bool, is_single_host: bool): + func _init(host_id: int, use_mesh: bool, is_single_host: bool, is_public:bool): host = host_id mesh = use_mesh single_host = is_single_host + public=is_public func join(peer: Peer) -> bool: if sealed: return false @@ -168,11 +170,11 @@ func poll(): peers.erase(id) -func _join_lobby(peer: Peer, lobby: String, mesh: bool, single_host:bool) -> bool: +func _join_lobby(peer: Peer, lobby: String, mesh: bool, single_host:bool, public:bool) -> bool: if lobby == "": for _i in range(0, 32): lobby += char(_alfnum[rand.randi_range(0, ALFNUM.length()-1)]) - lobbies[lobby] = Lobby.new(peer.id, mesh, single_host) + lobbies[lobby] = Lobby.new(peer.id, mesh, single_host, public) elif not lobbies.has(lobby): return false lobbies[lobby].join(peer) @@ -202,7 +204,7 @@ func _parse_msg(peer: Peer) -> bool: if peer.lobby: # Peer must not have joined a lobby already! return false var dt = JSON.parse_string(msg.data) - return _join_lobby(peer, dt.lobby, msg.id == 0, dt.single_host) + return _join_lobby(peer, dt.lobby, msg.id == 0, dt.single_host, dt.public) if not lobbies.has(peer.lobby): # Lobby not found? return false @@ -213,6 +215,14 @@ func _parse_msg(peer: Peer) -> bool: # Client is sealing the room return lobby.seal(peer.id) + if msg.type == Message.GET_PUBLIC: + var pb = [] + for lb in lobbies.keys(): + if lobbies[lb].public: + pb.append(lb) + peers[peer.id].send(msg.type, 0, JSON.stringify(pb)) + return true + var dest_id: int = msg.id if dest_id == MultiplayerPeer.TARGET_PEER_SERVER: dest_id = lobby.host diff --git a/server_node/server.js b/server_node/server.js index 064c5f4..8808533 100644 --- a/server_node/server.js +++ b/server_node/server.js @@ -35,6 +35,7 @@ const CMD = { ANSWER: 5, // eslint-disable-line sort-keys CANDIDATE: 6, // eslint-disable-line sort-keys SEAL: 7, // eslint-disable-line sort-keys + GET_PUBLIC: 8, // eslint-disable-line sort-keys }; function randomInt(low, high) { @@ -85,11 +86,12 @@ class Peer { } class Lobby { - constructor(name, host, mesh, single_host) { + constructor(name, host, mesh, single_host, public) { this.name = name; this.host = host; this.mesh = mesh; this.single_host = single_host; + this.public = public; this.peers = []; this.sealed = false; this.closeTimer = -1; @@ -158,7 +160,7 @@ class Lobby { const lobbies = new Map(); let peersCount = 0; -function joinLobby(peer, pLobby, mesh, single_host) { +function joinLobby(peer, pLobby, mesh, single_host, public) { let lobbyName = pLobby; if (lobbyName === '') { if (lobbies.size >= MAX_LOBBIES) { @@ -169,7 +171,7 @@ function joinLobby(peer, pLobby, mesh, single_host) { throw new ProtoError(4000, STR_ALREADY_IN_LOBBY); } lobbyName = randomSecret(); - lobbies.set(lobbyName, new Lobby(lobbyName, peer.id, mesh, single_host)); + lobbies.set(lobbyName, new Lobby(lobbyName, peer.id, mesh, single_host, public)); console.log(`Peer ${peer.id} created lobby ${lobbyName}`); console.log(`Open lobbies: ${lobbies.size}`); } @@ -206,7 +208,7 @@ function parseMsg(peer, msg) { // Lobby joining. if (type === CMD.JOIN) { const dt = JSON.parse(data); - joinLobby(peer, dt['lobby'], id === 0, dt['single_host']); + joinLobby(peer, dt['lobby'], id === 0, dt['single_host'], dt['public']); return; } @@ -223,6 +225,18 @@ function parseMsg(peer, msg) { lobby.seal(peer); return; } + + if (type === CMD.GET_PUBLIC) { + let pb = [] + for (lb in lobbies.keys()){ + if (lobbies[lb].public){ + pb.append(lb) + } + } + const data = JSON.stringify(pb) + peer.ws.send(ProtoMessage(type, 0, data)); + return; + } // Message relaying format: // diff --git a/webrtc/lib/~libwebrtc_native.windows.template_debug.x86_64.dll b/webrtc/lib/~libwebrtc_native.windows.template_debug.x86_64.dll new file mode 100644 index 0000000..a4b2697 Binary files /dev/null and b/webrtc/lib/~libwebrtc_native.windows.template_debug.x86_64.dll differ