add WSUser.WriteToPageBytesMulti

This commit is contained in:
Azareal 2020-07-16 20:51:34 +10:00
parent da5f383c5b
commit 0c230e12ec
1 changed files with 26 additions and 0 deletions

View File

@ -80,6 +80,32 @@ func (u *WSUser) WriteToPageBytes(msg []byte, page string) error {
return nil
}
// Inefficient as it looks for sockets for a page even if there are none
func (u *WSUser) WriteToPageBytesMulti(msgs [][]byte, page string) error {
var success bool
for _, socket := range u.Sockets {
if socket == nil {
continue
}
if socket.Page != page {
continue
}
w, err := socket.conn.NextWriter(websocket.TextMessage)
if err != nil {
continue // Skip dead sockets, a dedicated goroutine handles those
}
for _, msg := range msgs {
_, _ = w.Write(msg)
}
w.Close()
success = true
}
if !success {
return ErrNoneOnPage
}
return nil
}
func (u *WSUser) AddSocket(conn *websocket.Conn, page string) {
u.Lock()
// If the number of the sockets is small, then we can keep the size of the slice mostly static and just walk through it looking for empty slots