Skip to content

Commit

Permalink
fix: chat example's content-security-policy (denoland/deno#4091)
Browse files Browse the repository at this point in the history
  • Loading branch information
keroxp authored and denobot committed Jan 31, 2021
1 parent 21b7780 commit 20d7cc6
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 6 deletions.
7 changes: 4 additions & 3 deletions examples/chat/index.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>ws chat example</title>
</head>
<body>
Expand All @@ -10,7 +11,7 @@
<button id="closeButton" disabled>close</button>
</div>
<div id="status"></div>
<ul id="timeline"></div>
<ul id="timeline"></ul>
<script>
let ws;
function messageDom(msg) {
Expand All @@ -35,7 +36,7 @@
}
function connect() {
if (ws) ws.close();
ws = new WebSocket("ws://0.0.0.0:8080/ws");
ws = new WebSocket(`ws://${location.host}/ws`);
ws.addEventListener("open", () => {
console.log("open", ws);
applyState({connected: true});
Expand Down
19 changes: 16 additions & 3 deletions examples/chat/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,17 @@ listenAndServe({ port: 8080 }, async req => {
if (u.protocol.startsWith("http")) {
// server launched by deno run http(s)://.../server.ts,
fetch(u.href).then(resp => {
resp.headers.set("content-type", "text/html");
return req.respond(resp);
return req.respond({
status: resp.status,
headers: new Headers({
"content-type": "text/html"
}),
body: resp.body
});
});
} else {
// server launched by deno run ./server.ts
const file = await Deno.open("./index.html");
const file = await Deno.open(u.pathname);
req.respond({
status: 200,
headers: new Headers({
Expand All @@ -51,6 +56,14 @@ listenAndServe({ port: 8080 }, async req => {
});
}
}
if (req.method === "GET" && req.url === "/favicon.ico") {
req.respond({
status: 302,
headers: new Headers({
location: "https://deno.land/favicon.ico"
})
});
}
if (req.method === "GET" && req.url === "/ws") {
if (acceptable(req)) {
acceptWebSocket({
Expand Down

0 comments on commit 20d7cc6

Please sign in to comment.