add custom port through args

This commit is contained in:
Matthieu Jolimaitre 2024-04-10 03:42:26 +02:00
parent db1db1e519
commit 66c80fac5d
2 changed files with 22 additions and 5 deletions

View file

@ -14,18 +14,27 @@ import {
const log = log_from(import.meta); const log = log_from(import.meta);
async function main() { async function main() {
const args = parse_args(Deno.args);
log("Client starting."); log("Client starting.");
const interface_ = await ServerInterface.connect(9999); const interface_ = await ServerInterface.connect(args.port);
new InputHandler(interface_.outputs).spin();
const display = new DisplayHandler(interface_.outputs); const display = new DisplayHandler(interface_.outputs);
display.spin();
new MsgHandler(interface_.inputs, display).spin(); new MsgHandler(interface_.inputs, display).spin();
new InputHandler(interface_.outputs).spin();
display.spin();
log("Connected."); log("Connected.");
interface_.outputs.send({ kind: "ping", content: { message: "Machin." } }); interface_.outputs.send({ kind: "ping", content: { message: "Machin." } });
log("Sent ping."); log("Sent ping.");
} }
function parse_args(args: string[]) {
const [port_] = args;
let port = 9999;
if (port_ !== undefined) port = parseInt(port_);
return { port };
}
class MsgHandler { class MsgHandler {
receiver; receiver;
display; display;

View file

@ -10,6 +10,8 @@ import { enemy_plugin } from "./entities/enemy.ts";
const log = log_from(import.meta); const log = log_from(import.meta);
async function main() { async function main() {
const args = parse_args(Deno.args);
log("Starting."); log("Starting.");
const engine = new Engine(); const engine = new Engine();
enemy_plugin(engine); enemy_plugin(engine);
@ -18,12 +20,18 @@ async function main() {
engine.run(await sys_spawn_structure("../data/structures/houses.txt", v2(2, 2))); engine.run(await sys_spawn_structure("../data/structures/houses.txt", v2(2, 2)));
engine.run(sys_spawn_enemy(v2(1, 1))); engine.run(sys_spawn_enemy(v2(1, 1)));
const port = 9999; const gateway = new Gateway(args.port);
const gateway = new Gateway(port);
log("Awaiting connection."); log("Awaiting connection.");
for await (const client of gateway.accept()) { for await (const client of gateway.accept()) {
Session.init(client, engine).start(); Session.init(client, engine).start();
} }
} }
function parse_args(args: string[]) {
const [port_] = args;
let port = 9999;
if (port_ !== undefined) port = parseInt(port_);
return { port };
}
if (import.meta.main) await main(); if (import.meta.main) await main();