Now we know
This commit is contained in:
parent
a1636ff2f5
commit
6d608b6f0e
20
gen.ts
20
gen.ts
|
@ -1,20 +0,0 @@
|
||||||
const ALFNUM = "0123456789";
|
|
||||||
const SECRET_LENGTH = 8;
|
|
||||||
|
|
||||||
function randomInt(low: number, high: number) {
|
|
||||||
return Math.floor(Math.random() * (high - low + 1) + low);
|
|
||||||
}
|
|
||||||
|
|
||||||
export function randomId() {
|
|
||||||
const arr = new Int32Array(1);
|
|
||||||
crypto.getRandomValues(arr);
|
|
||||||
return Math.abs(arr[0]);
|
|
||||||
}
|
|
||||||
|
|
||||||
export function randomSecret() {
|
|
||||||
let out = "";
|
|
||||||
for (let i = 0; i < SECRET_LENGTH; i++) {
|
|
||||||
out += ALFNUM[randomInt(0, ALFNUM.length - 1)];
|
|
||||||
}
|
|
||||||
return out;
|
|
||||||
}
|
|
|
@ -4,7 +4,9 @@
|
||||||
|
|
||||||
# Signalling Server
|
# Signalling Server
|
||||||
|
|
||||||
The signaling server is written in TypeScript to run on Deno. It can run in the
|
Requires Deno >= 1.16.1
|
||||||
|
|
||||||
|
The signaling server is written in TypeScript to run on Deno (1.16.1). It can run in the
|
||||||
cloud via Deno deploy.
|
cloud via Deno deploy.
|
||||||
|
|
||||||
PORT=8888 deno run --allow-env --allow-net server.ts
|
PORT=8888 deno run --allow-env --allow-net server.ts
|
||||||
|
|
70
server.ts
70
server.ts
|
@ -21,32 +21,52 @@ const lobbies = new Set<Lobby>();
|
||||||
const clients = new Set<Client>();
|
const clients = new Set<Client>();
|
||||||
|
|
||||||
console.log("Listening on port", PORT);
|
console.log("Listening on port", PORT);
|
||||||
for await (const conn of Deno.listen({ port: PORT })) {
|
const listener = Deno.listen({ port: PORT });
|
||||||
|
for await (const conn of listener) {
|
||||||
|
console.debug("Connection received:", conn);
|
||||||
(async () => {
|
(async () => {
|
||||||
for await (const { respondWith, request } of Deno.serveHttp(conn)) {
|
const server = Deno.serveHttp(conn);
|
||||||
const { socket, response } = Deno.upgradeWebSocket(request);
|
for await (const { respondWith, request } of server) {
|
||||||
const client: Client = { socket };
|
console.debug("HTTP Request Received", request);
|
||||||
socket.onmessage = (ev) => {
|
try {
|
||||||
console.log("Client Message Received", ev);
|
const { socket, response } = Deno.upgradeWebSocket(request);
|
||||||
};
|
const client: Client = { socket };
|
||||||
socket.onopen = (ev) => {
|
socket.onmessage = (ev) => {
|
||||||
console.log("New Client:", ev);
|
console.log("Client Message Received", ev);
|
||||||
clients.add(client);
|
};
|
||||||
socket.send("lobbies_start");
|
socket.onopen = (ev) => {
|
||||||
lobbies.forEach(({ name, clients }) => {
|
console.log("New Client:", ev);
|
||||||
socket.send(JSON.stringify({ name, numClients: clients.length }));
|
if (!clients.has(client)) clients.add(client);
|
||||||
});
|
socket.send("lobbies_start");
|
||||||
socket.send("lobbies_end");
|
lobbies.forEach(({ name, clients }) => {
|
||||||
};
|
socket.send(JSON.stringify({ name, numClients: clients.length }));
|
||||||
socket.onclose = (ev) => {
|
});
|
||||||
console.log("Client Socket Close:", ev);
|
socket.send("lobbies_end");
|
||||||
clients.delete(client);
|
};
|
||||||
};
|
socket.onclose = (ev) => {
|
||||||
socket.onerror = (ev) => {
|
console.log("Client Socket Close:", ev);
|
||||||
console.log("Client Socket Error:", ev);
|
if (clients.has(client)) clients.delete(client);
|
||||||
clients.delete(client);
|
};
|
||||||
};
|
socket.onerror = (ev) => {
|
||||||
respondWith(response);
|
console.log("Client Socket Error:", ev);
|
||||||
|
if (clients.has(client)) clients.delete(client);
|
||||||
|
};
|
||||||
|
respondWith(response);
|
||||||
|
} catch (e) {
|
||||||
|
let body = "400 Bad Request";
|
||||||
|
if (e instanceof TypeError) {
|
||||||
|
body += " - Expected to be able to upgrade to WebSocket connection";
|
||||||
|
console.log("Could not add client:", e);
|
||||||
|
} else {
|
||||||
|
console.log("Could not add client for unhandled reason:", e);
|
||||||
|
}
|
||||||
|
respondWith(
|
||||||
|
new Response(body, {
|
||||||
|
status: 400,
|
||||||
|
headers: { "content-type": "text/html" },
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
})();
|
})();
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue