From 2578ee1848ec0f447325f0659d1b881e4cbf1082 Mon Sep 17 00:00:00 2001 From: Neale Pickett Date: Thu, 9 Apr 2020 23:09:33 -0600 Subject: [PATCH] Working --- README.md | 11 +++++++++ main.go | 57 +++++++++++++++++++++++++++++++++++++++++++ static/index.html | 11 +++++++++ static/vail.css | 0 static/vail.js | 61 +++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 140 insertions(+) create mode 100644 README.md create mode 100644 main.go create mode 100644 static/index.html create mode 100644 static/vail.css create mode 100644 static/vail.js diff --git a/README.md b/README.md new file mode 100644 index 0000000..5e31c7f --- /dev/null +++ b/README.md @@ -0,0 +1,11 @@ +This is a thing you can run to practice morse code with your friends. + +It's very simple. +It doesn't try to prevent people from screwing with each other, +or the server. + +You can use any key on the keyboard, +or a mouse button, +to key down. + +Network jitter is going to make this horrible. diff --git a/main.go b/main.go new file mode 100644 index 0000000..4781e1e --- /dev/null +++ b/main.go @@ -0,0 +1,57 @@ +package main + +import ( + "log" + "net/http" + "golang.org/x/net/websocket" +) + +type Client struct { + ws *websocket.Conn + active bool +} + +var clients []Client + +func (c Client) Chat() { + for c.active { + buf := make([]byte, 800) + n, err := c.ws.Read(buf) + if err != nil { + c.active = false + } + buf = buf[:n] + + for i, o := range clients { + if o.active == false { + nclients := len(clients) + clients[i] = clients[nclients - 1] + clients[nclients - 1] = Client{} + clients = clients[:nclients - 1] + } else if o == c { + // Don't send it back to the sending client + } else { + o.ws.Write(buf) + } + } + } +} + +func ChatServer(ws *websocket.Conn) { + me := Client{ + ws: ws, + active: true, + } + clients = append(clients, me) + + me.Chat() +} + +func main() { + http.Handle("/chat", websocket.Handler(ChatServer)) + http.Handle("/", http.FileServer(http.Dir("static"))) + err := http.ListenAndServe(":8080", nil) + if err != nil { + log.Fatal(err.Error()) + } +} diff --git a/static/index.html b/static/index.html new file mode 100644 index 0000000..915db79 --- /dev/null +++ b/static/index.html @@ -0,0 +1,11 @@ + + + + Vail + + + +

Vail

+
+ + \ No newline at end of file diff --git a/static/vail.css b/static/vail.css new file mode 100644 index 0000000..e69de29 diff --git a/static/vail.js b/static/vail.js new file mode 100644 index 0000000..4c62aa0 --- /dev/null +++ b/static/vail.js @@ -0,0 +1,61 @@ +var ac = new AudioContext() +var gain = ac.createGain() +gain.connect(ac.destination) +gain.gain.value = 0.1 + +var longest = 500 +var audioFreq = 660 +var audioFreqMe = audioFreq * 6 / 5 // I think this works out to a minor third +var myosc + +function message(event) { + let duration = Number(event.data) || 0 + duration = Math.min(duration, longest) + + let osc = ac.createOscillator() + osc.connect(gain) + osc.frequency.value = audioFreq + osc.start(ac.currentTime) + osc.stop(ac.currentTime + (duration * 0.001)) +} + +function key(event) { + if (event.type.endsWith("down")) { + if (! event.repeat) { + window.down = event.timeStamp + } + if (! myosc) { + myosc = ac.createOscillator() + myosc.connect(gain) + myosc.frequency.value = audioFreqMe + myosc.start(ac.currentTime) + } + } else { + let duration = event.timeStamp - window.down + duration = Math.min(duration, longest) + console.log(event.timeStamp, window.down, duration) + window.socket.send(duration) + + if (myosc) { + myosc.stop(ac.currentTime) + myosc = null + } + } +} + +function init() { + window.socket = new WebSocket("ws://penguin.linux.test:8080/chat") + window.socket.addEventListener("message", message) + + document.addEventListener("mousedown", e => key(e)) + document.addEventListener("mouseup", e => key(e)) + document.addEventListener("keydown", e => key(e)) + document.addEventListener("keyup", e => key(e)) +} + + +if (document.readyState === "loading") { + document.addEventListener("DOMContentLoaded", init) +} else { + init() +}