bcast join
parent
d2cebde4dc
commit
d6ea697f57
12
pool.go
12
pool.go
|
|
@ -3,6 +3,7 @@ package main
|
||||||
import (
|
import (
|
||||||
"io"
|
"io"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
|
|
||||||
"github.com/gorilla/websocket"
|
"github.com/gorilla/websocket"
|
||||||
|
|
@ -27,8 +28,9 @@ func NewPool() *Pool {
|
||||||
|
|
||||||
func (p *Pool) Push(id string, conn *websocket.Conn) {
|
func (p *Pool) Push(id string, conn *websocket.Conn) {
|
||||||
p.lock.Lock()
|
p.lock.Lock()
|
||||||
defer p.lock.Unlock()
|
|
||||||
p.conns.Store(id, &Conn{ws: *conn})
|
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 {
|
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
|
cnt := 0
|
||||||
p.conns.Range(func(k, v interface{}) bool {
|
p.conns.Range(func(k, v interface{}) bool {
|
||||||
k = k.(string)
|
k = k.(string)
|
||||||
conn := &v.(*Conn).ws
|
conn := v.(*Conn)
|
||||||
|
lock := &conn.lock
|
||||||
|
lock.Lock()
|
||||||
|
defer lock.Unlock()
|
||||||
|
ws := &conn.ws
|
||||||
cnt += 1
|
cnt += 1
|
||||||
w, err := conn.NextWriter(mt)
|
w, err := ws.NextWriter(mt)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
p.conns.Delete(k)
|
p.conns.Delete(k)
|
||||||
return true
|
return true
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue