bcast join
parent
d2cebde4dc
commit
d6ea697f57
12
pool.go
12
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
|
||||
|
|
|
|||
Loading…
Reference in New Issue