Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Procfile
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
web: deno run --allow-net --allow-read --cached-only server.js --port=${PORT}
10 changes: 3 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

--------------------

Expand All @@ -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.


Expand Down
4 changes: 2 additions & 2 deletions chat.js
Original file line number Diff line number Diff line change
@@ -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: {
Expand Down
2 changes: 1 addition & 1 deletion public/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
Expand Down
47 changes: 43 additions & 4 deletions server.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,47 @@
import { listenAndServe } from "https://deno.land/[email protected]/http/server.ts";
import { acceptWebSocket, acceptable } from "https://deno.land/[email protected]/ws/mod.ts";
import { listenAndServe } from "https://deno.land/[email protected]/http/server.ts";
import { serveFile } from "https://deno.land/[email protected]/http/file_server.ts";
import {
acceptWebSocket,
acceptable,
} from "https://deno.land/[email protected]/ws/mod.ts";
import { parse } from "https://deno.land/[email protected]/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({
Expand All @@ -14,4 +53,4 @@ listenAndServe({ port: 3000 }, async (req) => {
}
}
});
console.log("Server started on port 3000");
console.log(`Server started on port ${port}`);