From fa959a25708ae9e5a03954aab67c748d364d257d Mon Sep 17 00:00:00 2001 From: bel Date: Thu, 7 May 2020 19:39:27 -0600 Subject: [PATCH] video call with imperfect reconnect, persistent id, possibly many clients --- config.go | 6 ++--- main.go | 10 +++++++-- pool.go | 53 ++++++++++++++++++++++++++++++++------------ public/index.html | 7 ++++-- public/webrtc.js | 31 ++++++++++++++++++++++++-- server.go | 7 +++--- ws.go | 56 ++++++++++++++--------------------------------- 7 files changed, 105 insertions(+), 65 deletions(-) diff --git a/config.go b/config.go index 93fdaa2..7db9db4 100644 --- a/config.go +++ b/config.go @@ -6,9 +6,9 @@ func config() *args.ArgSet { as := args.NewArgSet() as.Append(args.INT, "p", "port to listen on", "58080") - as.Append(args.STRING, "d", "root dir to serve static", ".") - as.Append(args.STRING, "crt", "path to crt", "./cert.crt") - as.Append(args.STRING, "key", "path to key", "./cert.key") + as.Append(args.STRING, "d", "root dir to serve static", "./public") + as.Append(args.STRING, "crt", "path to crt", "./testdata/scratch.crt") + as.Append(args.STRING, "key", "path to key", "./testdata/scratch.key") if err := as.Parse(); err != nil { panic(err) diff --git a/main.go b/main.go index b52577d..a4a7282 100644 --- a/main.go +++ b/main.go @@ -28,7 +28,13 @@ func main() { c := as.GetString("crt") k := as.GetString("key") log.Printf("listening on %q", httpsServer.Addr) - if err := httpsServer.ListenAndServeTLS(c, k); err != nil { - panic(err) + if c == "" && k == "" { + if err := httpsServer.ListenAndServe(); err != nil { + panic(err) + } + } else { + if err := httpsServer.ListenAndServeTLS(c, k); err != nil { + panic(err) + } } } diff --git a/pool.go b/pool.go index 399db03..51b49aa 100644 --- a/pool.go +++ b/pool.go @@ -1,26 +1,51 @@ package main -import "io" +import ( + "io" + "io/ioutil" + "log" + "sync" + + "github.com/gorilla/websocket" +) type Pool struct { - writers []io.Writer - r *io.PipeReader - w *io.PipeWriter + conns *sync.Map //map[string]*websocket.Conn } func NewPool() *Pool { - r, w := io.Pipe() return &Pool{ - writers: make([]io.Writer, 0), - r: r, - w: w, + conns: &sync.Map{}, //map[string]*websocket.Conn{}, } } -func (p *Pool) Read(b []byte) (int, error) { - return p.r.Read(b) -} - -func (p *Pool) Write(b []byte) (int, error) { - return p.w.Write(b) +func (p *Pool) Broadcast(mt int, r io.Reader) error { + b, err := ioutil.ReadAll(r) + if err != nil { + return err + } + n := 1000000 + cnt := 0 + p.conns.Range(func(k, v interface{}) bool { + k = k.(string) + conn := v.(*websocket.Conn) + cnt += 1 + w, err := conn.NextWriter(mt) + if err != nil { + p.conns.Delete(k) + return true + } + defer w.Close() + m, err := w.Write(b) + if err != nil { + p.conns.Delete(k) + return true + } + if m < n { + n = m + } + return true + }) + log.Printf("%d writes, %d min size, %d wanted size", cnt, n, len(b)) + return nil } diff --git a/public/index.html b/public/index.html index 0f1299f..1b0ef31 100755 --- a/public/index.html +++ b/public/index.html @@ -5,9 +5,12 @@ diff --git a/public/webrtc.js b/public/webrtc.js index 97c860d..580cc49 100755 --- a/public/webrtc.js +++ b/public/webrtc.js @@ -12,13 +12,40 @@ var peerConnectionConfig = { ] }; +function getCookie(cname) { + var name = cname + "="; + var decodedCookie = decodeURIComponent(document.cookie); + var ca = decodedCookie.split(';'); + for(var i = 0; i