v0.1: HTTP+SSE broadcast bus + phone web UI + Linux client

Single Go module with two binaries (server, client) and an embedded
phone UI. MVP transport is HTTP POST → SSE fanout; the roadmap calls
for upgrading to WebRTC P2P with Sign in with Apple for identity, mDNS
for discovery, and OS clipboard hooks.

- server/: Go HTTP server, embedded index.html, broadcast bus with
  short replay history, SSE stream endpoint, single-binary deploy.
- client/: subscribes to SSE feed and prints messages; -send for
  one-shot publish from CLI. No OS clipboard touched yet (v0.5).
- web/index.html: dark phone-first UI, paste-clipboard button (uses
  navigator.clipboard.readText), live feed of incoming messages via
  EventSource.

This commit is intentionally tiny — it proves the end-to-end shape so
the WebRTC/SiwA/mDNS pieces can be added incrementally without
restructuring.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
Claude Opus 4.7
2026-05-20 23:53:31 -05:00
commit b8f168df54
6 changed files with 502 additions and 0 deletions

99
client/main.go Normal file
View File

@@ -0,0 +1,99 @@
// tether-client: connects to a tether-server, prints messages to stdout.
// MVP — does not yet touch the OS clipboard.
package main
import (
"bufio"
"bytes"
"encoding/json"
"flag"
"fmt"
"io"
"log"
"net/http"
"os"
"strings"
"time"
)
type Message struct {
Text string `json:"text"`
Source string `json:"source,omitempty"`
TS int64 `json:"ts"`
}
func main() {
server := flag.String("server", "http://localhost:8765", "tether-server base URL")
label := flag.String("label", "linux-client", "X-Tether-Client label")
sendText := flag.String("send", "", "send this text and exit (otherwise listen)")
flag.Parse()
if *sendText != "" {
send(*server, *label, *sendText)
return
}
for {
if err := listen(*server, *label); err != nil {
log.Printf("stream error: %v — reconnecting in 3s", err)
time.Sleep(3 * time.Second)
}
}
}
func send(server, label, text string) {
body, _ := json.Marshal(Message{Text: text, Source: label})
req, _ := http.NewRequest("POST", server+"/api/send", bytes.NewReader(body))
req.Header.Set("Content-Type", "application/json")
req.Header.Set("X-Tether-Source", label)
r, err := http.DefaultClient.Do(req)
if err != nil {
log.Fatalf("send: %v", err)
}
defer r.Body.Close()
if r.StatusCode >= 300 {
log.Fatalf("send: HTTP %d", r.StatusCode)
}
fmt.Println("sent.")
}
func listen(server, label string) error {
req, _ := http.NewRequest("GET", server+"/api/stream", nil)
req.Header.Set("X-Tether-Client", label)
r, err := http.DefaultClient.Do(req)
if err != nil {
return err
}
defer r.Body.Close()
if r.StatusCode != 200 {
return fmt.Errorf("HTTP %d", r.StatusCode)
}
fmt.Fprintf(os.Stderr, "tether-client: connected to %s as %q\n", server, label)
sc := bufio.NewScanner(r.Body)
sc.Buffer(make([]byte, 1024*1024), 1024*1024)
var ev, data string
for sc.Scan() {
line := sc.Text()
switch {
case strings.HasPrefix(line, "event: "):
ev = strings.TrimPrefix(line, "event: ")
case strings.HasPrefix(line, "data: "):
data = strings.TrimPrefix(line, "data: ")
case line == "":
if ev == "clipboard" && data != "" {
var m Message
if err := json.Unmarshal([]byte(data), &m); err == nil {
ts := time.UnixMilli(m.TS).Format("15:04:05")
fmt.Printf("\n────── %s from %s ──────\n%s\n", ts, m.Source, m.Text)
}
}
ev, data = "", ""
case strings.HasPrefix(line, ": "):
// keepalive — ignore
}
}
if err := sc.Err(); err != nil && err != io.EOF {
return err
}
return nil
}