mvp control via browser
parent
934158b7a3
commit
52ee1e5083
1
go.mod
1
go.mod
|
|
@ -10,6 +10,7 @@ require (
|
||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/evanphx/json-patch/v5 v5.6.0 // indirect
|
github.com/evanphx/json-patch/v5 v5.6.0 // indirect
|
||||||
|
github.com/gorilla/websocket v1.5.0 // indirect
|
||||||
github.com/hajimehoshi/oto v0.7.1 // indirect
|
github.com/hajimehoshi/oto v0.7.1 // indirect
|
||||||
github.com/micmonay/keybd_event v1.1.1 // indirect
|
github.com/micmonay/keybd_event v1.1.1 // indirect
|
||||||
github.com/pkg/errors v0.9.1 // indirect
|
github.com/pkg/errors v0.9.1 // indirect
|
||||||
|
|
|
||||||
2
go.sum
2
go.sum
|
|
@ -11,6 +11,8 @@ github.com/go-audio/riff v1.0.0/go.mod h1:l3cQwc85y79NQFCRB7TiPoNiaijp6q8Z0Uv38r
|
||||||
github.com/go-audio/wav v1.0.0/go.mod h1:3yoReyQOsiARkvPl3ERCi8JFjihzG6WhjYpZCf5zAWE=
|
github.com/go-audio/wav v1.0.0/go.mod h1:3yoReyQOsiARkvPl3ERCi8JFjihzG6WhjYpZCf5zAWE=
|
||||||
github.com/go-yaml/yaml v2.1.0+incompatible h1:RYi2hDdss1u4YE7GwixGzWwVo47T8UQwnTLB6vQiq+o=
|
github.com/go-yaml/yaml v2.1.0+incompatible h1:RYi2hDdss1u4YE7GwixGzWwVo47T8UQwnTLB6vQiq+o=
|
||||||
github.com/go-yaml/yaml v2.1.0+incompatible/go.mod h1:w2MrLa16VYP0jy6N7M5kHaCkaLENm+P+Tv+MfurjSw0=
|
github.com/go-yaml/yaml v2.1.0+incompatible/go.mod h1:w2MrLa16VYP0jy6N7M5kHaCkaLENm+P+Tv+MfurjSw0=
|
||||||
|
github.com/gorilla/websocket v1.5.0 h1:PPwGk2jz7EePpoHN/+ClbZu8SPxiqlu12wZP/3sWmnc=
|
||||||
|
github.com/gorilla/websocket v1.5.0/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
|
||||||
github.com/hajimehoshi/go-mp3 v0.3.0/go.mod h1:qMJj/CSDxx6CGHiZeCgbiq2DSUkbK0UbtXShQcnfyMM=
|
github.com/hajimehoshi/go-mp3 v0.3.0/go.mod h1:qMJj/CSDxx6CGHiZeCgbiq2DSUkbK0UbtXShQcnfyMM=
|
||||||
github.com/hajimehoshi/oto v0.6.1/go.mod h1:0QXGEkbuJRohbJaxr7ZQSxnju7hEhseiPx2hrh6raOI=
|
github.com/hajimehoshi/oto v0.6.1/go.mod h1:0QXGEkbuJRohbJaxr7ZQSxnju7hEhseiPx2hrh6raOI=
|
||||||
github.com/hajimehoshi/oto v0.7.1 h1:I7maFPz5MBCwiutOrz++DLdbr4rTzBsbBuV2VpgU9kk=
|
github.com/hajimehoshi/oto v0.7.1 h1:I7maFPz5MBCwiutOrz++DLdbr4rTzBsbBuV2VpgU9kk=
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,99 @@
|
||||||
|
<!DOCTYPE html>
|
||||||
|
<html>
|
||||||
|
<header>
|
||||||
|
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/water.css@2/out/dark.css">
|
||||||
|
</header>
|
||||||
|
<body>
|
||||||
|
<form>
|
||||||
|
<h3>WHO AM I</h3>
|
||||||
|
<div>
|
||||||
|
<select id="user">
|
||||||
|
<option>bel</option>
|
||||||
|
<option>zach</option>
|
||||||
|
<option>chase</option>
|
||||||
|
<option>mason</option>
|
||||||
|
<option>nat</option>
|
||||||
|
<option>roxy</option>
|
||||||
|
<option>bill</option>
|
||||||
|
</select>
|
||||||
|
</div>
|
||||||
|
<h3>CONTROLS</h3>
|
||||||
|
<div id="controls" style="display: flex; flex-wrap: wrap;">
|
||||||
|
<div>
|
||||||
|
<input type="text" maxLength=1 value="w" name="w" placeholder="up" onchange="recontrol()">
|
||||||
|
<input type="text" maxLength=1 value="s" name="s" placeholder="down" onchange="recontrol()">
|
||||||
|
<input type="text" maxLength=1 value="a" name="a" placeholder="left" onchange="recontrol()">
|
||||||
|
<input type="text" maxLength=1 value="d" name="d" placeholder="right" onchange="recontrol()">
|
||||||
|
</div>
|
||||||
|
<div>
|
||||||
|
<input type="text" maxLength=1 value="5" name="5" placeholder="start" onchange="recontrol()">
|
||||||
|
<input type="text" maxLength=1 value="q" name="q" placeholder="l" onchange="recontrol()">
|
||||||
|
<input type="text" maxLength=1 value="e" name="e" placeholder="r" onchange="recontrol()">
|
||||||
|
</div>
|
||||||
|
<div>
|
||||||
|
<input type="text" maxLength=1 value="1" name="1" placeholder="a" onchange="recontrol()">
|
||||||
|
<input type="text" maxLength=1 value="2" name="2" placeholder="b" onchange="recontrol()">
|
||||||
|
<input type="text" maxLength=1 value="3" name="3" placeholder="x" onchange="recontrol()">
|
||||||
|
<input type="text" maxLength=1 value="4" name="4" placeholder="y" onchange="recontrol()">
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</form>
|
||||||
|
<div id="ntfy"></div>
|
||||||
|
<div id="ws"></div>
|
||||||
|
</body>
|
||||||
|
<footer>
|
||||||
|
<script>
|
||||||
|
var socket = new WebSocket("ws://"+window.location.host+"/api/ws")
|
||||||
|
|
||||||
|
function nosend(data) {
|
||||||
|
}
|
||||||
|
function dosend(data) {
|
||||||
|
console.log(JSON.stringify(data))
|
||||||
|
socket.send(JSON.stringify(data))
|
||||||
|
}
|
||||||
|
send = nosend
|
||||||
|
|
||||||
|
socket.addEventListener("open", (_) => {
|
||||||
|
console.log("ws open")
|
||||||
|
send = dosend
|
||||||
|
})
|
||||||
|
socket.addEventListener("message", (event) => console.log("ws recv:", event.data))
|
||||||
|
socket.addEventListener("close", (event) => console.log("ws closed"))
|
||||||
|
|
||||||
|
keys = {}
|
||||||
|
document.addEventListener('keydown', (event) => {
|
||||||
|
var name = controls[event.key]
|
||||||
|
if (!name)
|
||||||
|
return
|
||||||
|
if (keys[name])
|
||||||
|
return
|
||||||
|
keys[name] = true
|
||||||
|
sendKeys(name, "")
|
||||||
|
})
|
||||||
|
document.addEventListener('keyup', (event) => {
|
||||||
|
var name = controls[event.key]
|
||||||
|
if (!name)
|
||||||
|
return
|
||||||
|
keys[name] = false
|
||||||
|
sendKeys("", name)
|
||||||
|
})
|
||||||
|
function sendKeys(y, n) {
|
||||||
|
send({
|
||||||
|
T: new Date().getTime(),
|
||||||
|
U: document.getElementById("user").value,
|
||||||
|
Y: y,
|
||||||
|
N: n,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
var controls = {}
|
||||||
|
function recontrol() {
|
||||||
|
for (var k in controls)
|
||||||
|
controls[k] = false
|
||||||
|
for (var e of document.getElementById("controls").getElementsByTagName("input"))
|
||||||
|
controls[e.value] = e.name
|
||||||
|
}
|
||||||
|
recontrol()
|
||||||
|
</script>
|
||||||
|
</footer>
|
||||||
|
</html>
|
||||||
|
|
@ -9,6 +9,8 @@ import (
|
||||||
var (
|
var (
|
||||||
FlagRawKeyboard = os.Getenv("RAW_KEYBOARD") == "true"
|
FlagRawKeyboard = os.Getenv("RAW_KEYBOARD") == "true"
|
||||||
FlagRawUDP = os.Getenv("RAW_UDP")
|
FlagRawUDP = os.Getenv("RAW_UDP")
|
||||||
|
FlagRawWS = os.Getenv("RAW_WS")
|
||||||
|
FlagDebug = os.Getenv("DEBUG") != ""
|
||||||
FlagRawRandomWeightFile = os.Getenv("RAW_RANDOM_WEIGHT_FILE")
|
FlagRawRandomWeightFile = os.Getenv("RAW_RANDOM_WEIGHT_FILE")
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
@ -21,6 +23,9 @@ func New(ctx context.Context) Raw {
|
||||||
if FlagRawKeyboard {
|
if FlagRawKeyboard {
|
||||||
return NewKeyboard()
|
return NewKeyboard()
|
||||||
}
|
}
|
||||||
|
if port, _ := strconv.Atoi(FlagRawWS); port != 0 {
|
||||||
|
return NewWS(ctx, port)
|
||||||
|
}
|
||||||
if port, _ := strconv.Atoi(FlagRawUDP); port != 0 {
|
if port, _ := strconv.Atoi(FlagRawUDP); port != 0 {
|
||||||
return NewUDP(ctx, port)
|
return NewUDP(ctx, port)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -6,4 +6,5 @@ func TestRaw(t *testing.T) {
|
||||||
var _ Raw = &Random{}
|
var _ Raw = &Random{}
|
||||||
var _ Raw = UDP{}
|
var _ Raw = UDP{}
|
||||||
var _ Raw = Keyboard{}
|
var _ Raw = Keyboard{}
|
||||||
|
var _ Raw = WS{}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -4,14 +4,9 @@ import (
|
||||||
"context"
|
"context"
|
||||||
"log"
|
"log"
|
||||||
"net"
|
"net"
|
||||||
"os"
|
|
||||||
"strconv"
|
"strconv"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
|
||||||
FlagDebug = os.Getenv("DEBUG") == "true"
|
|
||||||
)
|
|
||||||
|
|
||||||
type UDP struct {
|
type UDP struct {
|
||||||
conn net.PacketConn
|
conn net.PacketConn
|
||||||
c chan []byte
|
c chan []byte
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,120 @@
|
||||||
|
package raw
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
_ "embed"
|
||||||
|
"fmt"
|
||||||
|
"log"
|
||||||
|
"net/http"
|
||||||
|
"os"
|
||||||
|
|
||||||
|
"github.com/gorilla/websocket"
|
||||||
|
)
|
||||||
|
|
||||||
|
type WS struct {
|
||||||
|
ctx context.Context
|
||||||
|
can context.CancelFunc
|
||||||
|
ch chan []byte
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewWS(ctx context.Context, port int) WS {
|
||||||
|
ctx, can := context.WithCancel(ctx)
|
||||||
|
ws := WS{ctx: ctx, can: can, ch: make(chan []byte, 256)}
|
||||||
|
go ws.listen(port)
|
||||||
|
return ws
|
||||||
|
}
|
||||||
|
|
||||||
|
func (ws WS) Read() []byte {
|
||||||
|
select {
|
||||||
|
case v := <-ws.ch:
|
||||||
|
return v
|
||||||
|
case <-ws.ctx.Done():
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (ws WS) Close() {
|
||||||
|
ws.can()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (ws WS) listen(port int) {
|
||||||
|
server := &http.Server{
|
||||||
|
Addr: fmt.Sprintf(":%d", port),
|
||||||
|
Handler: ws,
|
||||||
|
}
|
||||||
|
go func() {
|
||||||
|
if err := server.ListenAndServe(); err != nil && ws.ctx.Err() == nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
log.Println("WS on", port)
|
||||||
|
<-ws.ctx.Done()
|
||||||
|
server.Close()
|
||||||
|
}
|
||||||
|
|
||||||
|
var upgrader = websocket.Upgrader{
|
||||||
|
ReadBufferSize: 1024,
|
||||||
|
WriteBufferSize: 1024,
|
||||||
|
}
|
||||||
|
|
||||||
|
func (ws WS) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||||
|
r = r.WithContext(ws.ctx)
|
||||||
|
if err := ws.serveHTTP(w, r); err != nil {
|
||||||
|
log.Println(err)
|
||||||
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (ws WS) serveHTTP(w http.ResponseWriter, r *http.Request) error {
|
||||||
|
switch r.URL.Path {
|
||||||
|
case "/":
|
||||||
|
return ws.serveIndex(w, r)
|
||||||
|
case "/api/ws":
|
||||||
|
return ws.serveWS(w, r)
|
||||||
|
}
|
||||||
|
http.NotFound(w, r)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
//go:embed public/root.html
|
||||||
|
var rootHTML string
|
||||||
|
|
||||||
|
func (ws WS) serveIndex(w http.ResponseWriter, r *http.Request) error {
|
||||||
|
v := rootHTML
|
||||||
|
if FlagDebug {
|
||||||
|
b, _ := os.ReadFile("src/device/input/raw/public/root.html")
|
||||||
|
v = string(b)
|
||||||
|
}
|
||||||
|
w.Write([]byte(v))
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (ws WS) serveWS(w http.ResponseWriter, r *http.Request) error {
|
||||||
|
if err := ws._serveWS(w, r); err != nil {
|
||||||
|
log.Println("_serveWS:", err)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (ws WS) _serveWS(w http.ResponseWriter, r *http.Request) error {
|
||||||
|
conn, err := upgrader.Upgrade(w, r, nil)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
defer conn.Close()
|
||||||
|
|
||||||
|
for ws.ctx.Err() == nil {
|
||||||
|
msgType, p, err := conn.ReadMessage()
|
||||||
|
if err != nil {
|
||||||
|
if websocket.IsCloseError(err) || websocket.IsUnexpectedCloseError(err) {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if msgType == websocket.TextMessage {
|
||||||
|
log.Println(string(p))
|
||||||
|
ws.ch <- p
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue