kopia lustrzana https://github.com/cyoung/stratux
45 wiersze
895 B
Go
45 wiersze
895 B
Go
package main
|
|
|
|
import (
|
|
"golang.org/x/net/websocket"
|
|
"time"
|
|
)
|
|
|
|
type uibroadcaster struct {
|
|
sockets []*websocket.Conn
|
|
messages chan []byte
|
|
}
|
|
|
|
func NewUIBroadcaster() *uibroadcaster {
|
|
ret := &uibroadcaster{
|
|
sockets: make([]*websocket.Conn, 0),
|
|
messages: make(chan []byte, 1024),
|
|
}
|
|
go ret.writer()
|
|
return ret
|
|
}
|
|
|
|
func (u *uibroadcaster) Send(msg []byte) {
|
|
u.messages <- msg
|
|
}
|
|
|
|
func (u *uibroadcaster) AddSocket(sock *websocket.Conn) {
|
|
u.sockets = append(u.sockets, sock)
|
|
}
|
|
|
|
func (u *uibroadcaster) writer() {
|
|
for {
|
|
msg := <-u.messages
|
|
// Send to all.
|
|
p := make([]*websocket.Conn, 0) // Keep a list of the writeable sockets.
|
|
for _, sock := range u.sockets {
|
|
err := sock.SetWriteDeadline(time.Now().Add(time.Second))
|
|
_, err2 := sock.Write(msg)
|
|
if err == nil && err2 == nil {
|
|
p = append(p, sock)
|
|
}
|
|
}
|
|
u.sockets = p // Save the list of writeable sockets.
|
|
}
|
|
}
|