From d6ea697f57eddb802b191b8e8fc7f1f94ffa73da Mon Sep 17 00:00:00 2001 From: Bel LaPointe Date: Wed, 13 May 2020 16:36:20 -0600 Subject: [PATCH] bcast join --- pool.go | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/pool.go b/pool.go index 3fef169..a5ed2e9 100644 --- a/pool.go +++ b/pool.go @@ -3,6 +3,7 @@ package main import ( "io" "io/ioutil" + "strings" "sync" "github.com/gorilla/websocket" @@ -27,8 +28,9 @@ func NewPool() *Pool { func (p *Pool) Push(id string, conn *websocket.Conn) { p.lock.Lock() - defer p.lock.Unlock() p.conns.Store(id, &Conn{ws: *conn}) + p.lock.Unlock() + p.Broadcast(websocket.TextMessage, strings.NewReader(`{"joined":"`+id+`"}`)) } func (p *Pool) Broadcast(mt int, r io.Reader) error { @@ -43,9 +45,13 @@ func (p *Pool) Broadcast(mt int, r io.Reader) error { cnt := 0 p.conns.Range(func(k, v interface{}) bool { k = k.(string) - conn := &v.(*Conn).ws + conn := v.(*Conn) + lock := &conn.lock + lock.Lock() + defer lock.Unlock() + ws := &conn.ws cnt += 1 - w, err := conn.NextWriter(mt) + w, err := ws.NextWriter(mt) if err != nil { p.conns.Delete(k) return true