diff --git a/Procfile b/Procfile new file mode 100644 index 0000000..e3eca6b --- /dev/null +++ b/Procfile @@ -0,0 +1 @@ +web: deno run --allow-net --allow-read --cached-only server.js --port=${PORT} \ No newline at end of file diff --git a/README.md b/README.md index eaa7a32..1026298 100644 --- a/README.md +++ b/README.md @@ -2,11 +2,7 @@ ## Demo -### Join to chat page -![Chat join page](https://i.imgur.com/BCXT0Ba.png) - -### Chat page -![Chat page](https://i.imgur.com/h0FE6pY.png) +#### Check [the following link](https://deno-websocket-chat.herokuapp.com/chat.html) to see deployed version on heroku -------------------- @@ -15,8 +11,8 @@ You need to have [Deno installed](https://deno.land/#installation) in order to r 1. Clone the repository 2. Go to the project root using terminal -3. Run `deno run --allow-net server.js` -4. Open `public/chat.html` in browser +3. Run `deno run --allow-net --allow-read server.js` +4. Open http://localhost:3000/chat.html` in browser 5. That's all. diff --git a/chat.js b/chat.js index 3e26488..b99994b 100644 --- a/chat.js +++ b/chat.js @@ -1,6 +1,6 @@ // @ts-nocheck -import { isWebSocketCloseEvent } from "https://deno.land/std@0.58.0/ws/mod.ts"; -import { v4 } from "https://deno.land/std@0.58.0/uuid/mod.ts"; +import { isWebSocketCloseEvent } from "https://deno.land/std@0.65.0/ws/mod.ts"; +import { v4 } from "https://deno.land/std@0.65.0/uuid/mod.ts"; /** * userId: { diff --git a/public/client.js b/public/client.js index c984ff2..28d762d 100644 --- a/public/client.js +++ b/public/client.js @@ -9,7 +9,7 @@ let leaveGroupBtn = document.querySelector("#leaveGroupBtn"); let groupName = document.querySelector("#groupName"); window.addEventListener("DOMContentLoaded", () => { - ws = new WebSocket(`ws://localhost:3000/ws`); + ws = new WebSocket(`${window.location.protocol === 'http:' ? 'ws' : 'wss'}://${window.location.host}/ws`); ws.addEventListener("open", onConnectionOpen); ws.addEventListener("message", onMessageReceived); }); diff --git a/server.js b/server.js index 43ff182..033be35 100644 --- a/server.js +++ b/server.js @@ -1,8 +1,47 @@ -import { listenAndServe } from "https://deno.land/std@0.58.0/http/server.ts"; -import { acceptWebSocket, acceptable } from "https://deno.land/std@0.58.0/ws/mod.ts"; +import { listenAndServe } from "https://deno.land/std@0.67.0/http/server.ts"; +import { serveFile } from "https://deno.land/std@0.67.0/http/file_server.ts"; +import { + acceptWebSocket, + acceptable, +} from "https://deno.land/std@0.67.0/ws/mod.ts"; +import { parse } from "https://deno.land/std@0.67.0/flags/mod.ts"; import chat from "./chat.js"; -listenAndServe({ port: 3000 }, async (req) => { +async function fileExists(path) { + try { + const stats = await Deno.lstat(path); + return stats && stats.isFile; + } catch (e) { + if (e && e instanceof Deno.errors.NotFound) { + return false; + } else { + throw e; + } + } +} + +const DEFAULT_PORT = 3000; +const argPort = parse(Deno.args).port; +const port = argPort ? parseInt(argPort) : DEFAULT_PORT; + +listenAndServe({ port: port }, async (req) => { + let url = req.url; + if (req.method === "GET" && url === "/") { + url = "/index.html"; + } + + const position = req.url.indexOf("?"); + + if (position > -1) { + url = url.substring(0, position); + } + const path = `${Deno.cwd()}/public${url}`; // /index.html + if (await fileExists(path)) { + const content = await serveFile(req, path); + req.respond(content); + return; + } + if (req.method === "GET" && req.url === "/ws") { if (acceptable(req)) { acceptWebSocket({ @@ -14,4 +53,4 @@ listenAndServe({ port: 3000 }, async (req) => { } } }); -console.log("Server started on port 3000"); +console.log(`Server started on port ${port}`);