Error during connection
This commit is contained in:
parent
ff009674bb
commit
aad8b99dc1
2 changed files with 48 additions and 44 deletions
|
@ -4,7 +4,7 @@ const MultiplayerClient = preload("multiplayer_client.gd")
|
|||
|
||||
onready var client = MultiplayerClient.new()
|
||||
|
||||
const MULTIPLAYER_URL = "ws://localhost:9080"
|
||||
const MULTIPLAYER_URL = "wss://webrtc-signaller.deno.dev/"
|
||||
|
||||
func _ready():
|
||||
client.connect("lobby_joined", self, "_lobby_joined")
|
||||
|
|
90
server.ts
90
server.ts
|
@ -216,50 +216,54 @@ for await (const conn of server) {
|
|||
const httpConn = Deno.serveHttp(conn);
|
||||
for await (const requestEvent of httpConn) {
|
||||
if (requestEvent) {
|
||||
const { socket, response } = Deno.upgradeWebSocket(
|
||||
requestEvent.request,
|
||||
);
|
||||
const id = randomId();
|
||||
const peer = new Peer(id, socket);
|
||||
socket.onopen = (_ev) => {
|
||||
if (peersCount >= MAX_PEERS) {
|
||||
socket.close(4000, STR_TOO_MANY_PEERS);
|
||||
return;
|
||||
}
|
||||
peersCount++;
|
||||
};
|
||||
socket.onmessage = (ev) => {
|
||||
try {
|
||||
parseMsg(peer, ev.data);
|
||||
} catch (e) {
|
||||
const code = e.code || 4000;
|
||||
console.log(`Error parsing message from ${id}:\n` + ev.data);
|
||||
socket.close(code, e.message);
|
||||
}
|
||||
};
|
||||
socket.onclose = (ev) => {
|
||||
peersCount--;
|
||||
console.log(
|
||||
`Connection with peer ${peer.id} closed ` +
|
||||
`with reason: ${ev.reason}`,
|
||||
try {
|
||||
const { socket, response } = Deno.upgradeWebSocket(
|
||||
requestEvent.request,
|
||||
);
|
||||
if (
|
||||
peer.lobby &&
|
||||
lobbies.has(peer.lobby) &&
|
||||
lobbies.get(peer.lobby).leave(peer)
|
||||
) {
|
||||
lobbies.delete(peer.lobby);
|
||||
console.log(`Deleted lobby ${peer.lobby}`);
|
||||
console.log(`Open lobbies: ${lobbies.size}`);
|
||||
peer.lobby = "";
|
||||
}
|
||||
if (peer.timeout >= 0) {
|
||||
clearTimeout(peer.timeout);
|
||||
peer.timeout = -1;
|
||||
}
|
||||
};
|
||||
socket.onerror = (e) => console.error("WebSocket error:", e);
|
||||
requestEvent.respondWith(response);
|
||||
const id = randomId();
|
||||
const peer = new Peer(id, socket);
|
||||
socket.onopen = (_ev) => {
|
||||
if (peersCount >= MAX_PEERS) {
|
||||
socket.close(4000, STR_TOO_MANY_PEERS);
|
||||
return;
|
||||
}
|
||||
peersCount++;
|
||||
};
|
||||
socket.onmessage = (ev) => {
|
||||
try {
|
||||
parseMsg(peer, ev.data);
|
||||
} catch (e) {
|
||||
const code = e.code || 4000;
|
||||
console.log(`Error parsing message from ${id}:\n` + ev.data);
|
||||
socket.close(code, e.message);
|
||||
}
|
||||
};
|
||||
socket.onclose = (ev) => {
|
||||
peersCount--;
|
||||
console.log(
|
||||
`Connection with peer ${peer.id} closed ` +
|
||||
`with reason: ${ev.reason}`,
|
||||
);
|
||||
if (
|
||||
peer.lobby &&
|
||||
lobbies.has(peer.lobby) &&
|
||||
lobbies.get(peer.lobby).leave(peer)
|
||||
) {
|
||||
lobbies.delete(peer.lobby);
|
||||
console.log(`Deleted lobby ${peer.lobby}`);
|
||||
console.log(`Open lobbies: ${lobbies.size}`);
|
||||
peer.lobby = "";
|
||||
}
|
||||
if (peer.timeout >= 0) {
|
||||
clearTimeout(peer.timeout);
|
||||
peer.timeout = -1;
|
||||
}
|
||||
};
|
||||
socket.onerror = (e) => console.error("WebSocket error:", e);
|
||||
requestEvent.respondWith(response);
|
||||
} catch (e) {
|
||||
console.log(`Error during connection:`, e);
|
||||
}
|
||||
}
|
||||
}
|
||||
})();
|
||||
|
|
Loading…
Reference in a new issue