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 node/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules
50 changes: 50 additions & 0 deletions node/bike-mock.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<html>

<head>
<title>Bikkkke</title>
<link href="https://fonts.googleapis.com/css2?family=Recursive&display=swap" rel="stylesheet">
<style>
body {
font-family: 'Recursive', sans-serif;
}

label {
display: block
}
</style>
</head>

<body>
<h1>Mock bike</h1>

<label>
Power (watts)
<input type="range" max="1000" />
</label>

<label>
Resistance
<output>--</output>
</label>

<script type="module">
import PubSub from '/pubsub.js'
const pubsub = new PubSub();

const input = document.querySelector('input')
const output = document.querySelector('output')

pubsub.on("resistance", (s) => {
console.log("resistance", s);
output.innerText = s;
})

input.addEventListener("change", () => {
pubsub.emit("power", input.valueAsNumber)
})

</script>

</body>

</html>
46 changes: 46 additions & 0 deletions node/bike.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<html>

<head>
<title>Bikkkke</title>
<link href="https://fonts.googleapis.com/css2?family=Recursive&display=swap" rel="stylesheet">
<style>
body {
font-family: 'Recursive', sans-serif;
}

small {
font-size: 0.2em
}
</style>
</head>

<body>

<h1>RYAN<small>s bike</small> CONTROL </h1>

<h1>Power <output>--</output></h1>

<h1>Resistance <input type="range" max="1000" /></h1>


<script type="module">
import PubSub from '/pubsub.js'
const pubsub = new PubSub();

const power = document.querySelector('output');
const resist = document.querySelector('input');

pubsub.on("power", s => {
power.innerText = s;
})

resist.addEventListener('change', () => {
pubsub.emit("resistance", resist.valueAsNumber)
})


</script>

</body>

</html>
32 changes: 32 additions & 0 deletions node/bike.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
const { join } = require("path");
const express = require("express");
const app = express();
const WebSocket = require("ws");
const http = require("http");

const port = 3000;

app.get("/", (req, res) => res.sendFile(join(__dirname, "bike.html")));
app.get("/mock", (req, res) => res.sendFile(join(__dirname, "bike-mock.html")));
app.get("/pubsub.js", (req, res) => res.sendFile(join(__dirname, "pubsub.js")));

const server = http.createServer(app);

const wss = new WebSocket.Server({ server });
wss.on("connection", (ws) => {
ws.on("message", (message) => {
console.log("received: %s", message);

wss.clients.forEach((client) => {
if (client !== ws) {
client.send(message);
}
});
});

ws.send("something");
});

server.listen(port, () =>
console.log(`Bike app listening at http://localhost:${port}`)
);
58 changes: 58 additions & 0 deletions node/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<html>

<head>
<style>
body {
font-family: sans-serif
}

script {
display: block;
font-family: monospace;
color: #333;
white-space: pre;
}

code {
border-bottom: 1px solid #443;
}
</style>

</head>

<body>
<h1>encodings</h1>

<script style="display: none">
const output = (name, text) =>
document.querySelector(`[data-out=${name}]`)
.innerText = text
</script>

<code>
<h2>Standard</h2>
<script>
fetch('/data')
.then(res => res.text())
.then(text => {
output('standard', text)
})
</script>
<output data-out="standard"></output>
</code>


<code>
<h2>chars</h2>
<script>
fetch('/data', { headers: { "content-encoding": 'chars' } })
.then(res => res.text())
.then(text => text.split(', ').map(c => String.fromCharCode(c)).join(''))
.then(text =>
output('chars', text))
</script>
<output data-out="chars"></output>
</code>
</body>

</html>
Loading