mirror of https://github.com/nealey/vail.git
Working
This commit is contained in:
commit
2578ee1848
|
@ -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.
|
|
@ -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())
|
||||
}
|
||||
}
|
|
@ -0,0 +1,11 @@
|
|||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<title>Vail</title>
|
||||
<script src="vail.js"></script>
|
||||
</head>
|
||||
<body>
|
||||
<h1>Vail</h1>
|
||||
<div id="output"></div>
|
||||
</body>
|
||||
</html>
|
|
@ -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()
|
||||
}
|
Loading…
Reference in New Issue