summaryrefslogtreecommitdiff
path: root/std/examples/chat
diff options
context:
space:
mode:
Diffstat (limited to 'std/examples/chat')
-rw-r--r--std/examples/chat/server.ts6
-rw-r--r--std/examples/chat/server_test.ts15
2 files changed, 16 insertions, 5 deletions
diff --git a/std/examples/chat/server.ts b/std/examples/chat/server.ts
index 08aede05b..d28f9f43f 100644
--- a/std/examples/chat/server.ts
+++ b/std/examples/chat/server.ts
@@ -29,7 +29,9 @@ async function wsHandler(ws: WebSocket): Promise<void> {
}
}
-listenAndServe({ port: 8080 }, async req => {
+const addr = Deno.args[0] ?? "127.0.0.1:8080";
+
+listenAndServe(addr, async req => {
if (req.method === "GET" && req.url === "/") {
//Serve with hack
const u = new URL("./index.html", import.meta.url);
@@ -75,4 +77,4 @@ listenAndServe({ port: 8080 }, async req => {
}
}
});
-console.log("chat server starting on :8080....");
+console.log(`chat server starting on ${addr}....`);
diff --git a/std/examples/chat/server_test.ts b/std/examples/chat/server_test.ts
index 0d21b3787..65e0b5958 100644
--- a/std/examples/chat/server_test.ts
+++ b/std/examples/chat/server_test.ts
@@ -3,13 +3,22 @@ import { assert, assertEquals } from "../../testing/asserts.ts";
import { TextProtoReader } from "../../textproto/mod.ts";
import { BufReader } from "../../io/bufio.ts";
import { connectWebSocket, WebSocket } from "../../ws/mod.ts";
+import { randomPort } from "../../http/test_util.ts";
import { delay } from "../../util/async.ts";
+const port = randomPort();
+
const { test, build } = Deno;
async function startServer(): Promise<Deno.Process> {
const server = Deno.run({
- args: [Deno.execPath(), "--allow-net", "--allow-read", "server.ts"],
+ args: [
+ Deno.execPath(),
+ "--allow-net",
+ "--allow-read",
+ "server.ts",
+ `127.0.0.1:${port}`
+ ],
cwd: "examples/chat",
stdout: "piped"
});
@@ -35,7 +44,7 @@ test({
async fn() {
const server = await startServer();
try {
- const resp = await fetch("http://127.0.0.1:8080/");
+ const resp = await fetch(`http://127.0.0.1:${port}/`);
assertEquals(resp.status, 200);
assertEquals(resp.headers.get("content-type"), "text/html");
const html = await resp.body.text();
@@ -55,7 +64,7 @@ test({
const server = await startServer();
let ws: WebSocket | undefined;
try {
- ws = await connectWebSocket("http://127.0.0.1:8080/ws");
+ ws = await connectWebSocket(`http://127.0.0.1:${port}/ws`);
const it = ws.receive();
assertEquals((await it.next()).value, "Connected: [1]");
ws.send("Hello");