2018-09-08 05:12:48 +00:00
|
|
|
package common
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"sync"
|
2019-02-28 07:28:17 +00:00
|
|
|
"time"
|
2018-09-08 05:12:48 +00:00
|
|
|
|
|
|
|
"github.com/gorilla/websocket"
|
|
|
|
)
|
|
|
|
|
|
|
|
var ErrNoneOnPage = errors.New("This user isn't on that page")
|
2018-09-12 09:50:57 +00:00
|
|
|
var ErrInvalidSocket = errors.New("That's not a valid WebSocket Connection")
|
2018-09-08 05:12:48 +00:00
|
|
|
|
|
|
|
type WSUser struct {
|
|
|
|
User *User
|
|
|
|
Sockets []*WSUserSocket
|
|
|
|
sync.Mutex
|
|
|
|
}
|
|
|
|
|
|
|
|
type WSUserSocket struct {
|
|
|
|
conn *websocket.Conn
|
|
|
|
Page string
|
|
|
|
}
|
|
|
|
|
2019-10-01 21:06:22 +00:00
|
|
|
func (u *WSUser) Ping() error {
|
|
|
|
for _, socket := range u.Sockets {
|
2019-02-28 07:28:17 +00:00
|
|
|
if socket == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
socket.conn.SetWriteDeadline(time.Now().Add(time.Minute))
|
|
|
|
err := socket.conn.WriteMessage(websocket.PingMessage, nil)
|
|
|
|
if err != nil {
|
|
|
|
socket.conn.Close()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-10-01 21:06:22 +00:00
|
|
|
func (u *WSUser) WriteAll(msg string) error {
|
2018-09-08 05:12:48 +00:00
|
|
|
msgbytes := []byte(msg)
|
2019-10-01 21:06:22 +00:00
|
|
|
for _, socket := range u.Sockets {
|
2018-09-08 12:47:37 +00:00
|
|
|
if socket == nil {
|
|
|
|
continue
|
|
|
|
}
|
2018-09-08 05:12:48 +00:00
|
|
|
w, err := socket.conn.NextWriter(websocket.TextMessage)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
_, _ = w.Write(msgbytes)
|
|
|
|
w.Close()
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-10-01 21:06:22 +00:00
|
|
|
func (u *WSUser) WriteToPage(msg string, page string) error {
|
|
|
|
return u.WriteToPageBytes([]byte(msg), page)
|
2018-09-08 05:12:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Inefficient as it looks for sockets for a page even if there are none
|
2019-10-01 21:06:22 +00:00
|
|
|
func (u *WSUser) WriteToPageBytes(msg []byte, page string) error {
|
2018-09-08 05:12:48 +00:00
|
|
|
var success bool
|
2019-10-01 21:06:22 +00:00
|
|
|
for _, socket := range u.Sockets {
|
2018-09-08 12:47:37 +00:00
|
|
|
if socket == nil {
|
|
|
|
continue
|
|
|
|
}
|
2018-09-08 05:12:48 +00:00
|
|
|
if socket.Page != page {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
w, err := socket.conn.NextWriter(websocket.TextMessage)
|
|
|
|
if err != nil {
|
|
|
|
continue // Skip dead sockets, a dedicated goroutine handles those
|
|
|
|
}
|
|
|
|
_, _ = w.Write(msg)
|
|
|
|
w.Close()
|
|
|
|
success = true
|
|
|
|
}
|
|
|
|
if !success {
|
|
|
|
return ErrNoneOnPage
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-10-01 21:06:22 +00:00
|
|
|
func (u *WSUser) AddSocket(conn *websocket.Conn, page string) {
|
|
|
|
u.Lock()
|
2018-09-08 05:12:48 +00:00
|
|
|
// 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
|
2019-10-01 21:06:22 +00:00
|
|
|
if len(u.Sockets) < 6 {
|
|
|
|
for i, socket := range u.Sockets {
|
2018-09-08 05:12:48 +00:00
|
|
|
if socket == nil {
|
2019-10-01 21:06:22 +00:00
|
|
|
u.Sockets[i] = &WSUserSocket{conn, page}
|
|
|
|
u.Unlock()
|
|
|
|
//fmt.Printf("%+v\n", u.Sockets)
|
2018-09-08 05:12:48 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-10-01 21:06:22 +00:00
|
|
|
u.Sockets = append(u.Sockets, &WSUserSocket{conn, page})
|
|
|
|
//fmt.Printf("%+v\n", u.Sockets)
|
|
|
|
u.Unlock()
|
2018-09-08 05:12:48 +00:00
|
|
|
}
|
|
|
|
|
2019-10-01 21:06:22 +00:00
|
|
|
func (u *WSUser) RemoveSocket(conn *websocket.Conn) {
|
|
|
|
u.Lock()
|
|
|
|
if len(u.Sockets) < 6 {
|
|
|
|
for i, socket := range u.Sockets {
|
2018-09-12 09:50:57 +00:00
|
|
|
if socket == nil {
|
|
|
|
continue
|
|
|
|
}
|
2018-09-08 05:12:48 +00:00
|
|
|
if socket.conn == conn {
|
2019-10-01 21:06:22 +00:00
|
|
|
u.Sockets[i] = nil
|
|
|
|
u.Unlock()
|
2018-09-23 23:42:17 +00:00
|
|
|
//fmt.Printf("%+v\n", wsUser.Sockets)
|
2018-09-08 05:12:48 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var key int
|
2019-10-01 21:06:22 +00:00
|
|
|
for i, socket := range u.Sockets {
|
2018-09-08 05:12:48 +00:00
|
|
|
if socket.conn == conn {
|
|
|
|
key = i
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2019-10-01 21:06:22 +00:00
|
|
|
u.Sockets = append(u.Sockets[:key], u.Sockets[key+1:]...)
|
|
|
|
//fmt.Printf("%+v\n", u.Sockets)
|
2018-09-08 05:12:48 +00:00
|
|
|
|
2019-10-01 21:06:22 +00:00
|
|
|
u.Unlock()
|
2018-09-08 05:12:48 +00:00
|
|
|
}
|
|
|
|
|
2019-10-01 21:06:22 +00:00
|
|
|
func (u *WSUser) SetPageForSocket(conn *websocket.Conn, page string) error {
|
2018-09-12 09:50:57 +00:00
|
|
|
if conn == nil {
|
|
|
|
return ErrInvalidSocket
|
|
|
|
}
|
|
|
|
|
2019-10-01 21:06:22 +00:00
|
|
|
u.Lock()
|
|
|
|
for _, socket := range u.Sockets {
|
2018-09-12 09:50:57 +00:00
|
|
|
if socket == nil {
|
|
|
|
continue
|
|
|
|
}
|
2018-09-08 05:12:48 +00:00
|
|
|
if socket.conn == conn {
|
|
|
|
socket.Page = page
|
|
|
|
}
|
|
|
|
}
|
2019-10-01 21:06:22 +00:00
|
|
|
u.Unlock()
|
2018-09-12 09:50:57 +00:00
|
|
|
|
|
|
|
return nil
|
2018-09-08 05:12:48 +00:00
|
|
|
}
|
|
|
|
|
2019-10-01 21:06:22 +00:00
|
|
|
func (u *WSUser) InPage(page string) bool {
|
|
|
|
u.Lock()
|
|
|
|
defer u.Unlock()
|
|
|
|
for _, socket := range u.Sockets {
|
2018-09-12 09:50:57 +00:00
|
|
|
if socket == nil {
|
|
|
|
continue
|
|
|
|
}
|
2018-09-08 05:12:48 +00:00
|
|
|
if socket.Page == page {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2019-10-01 21:06:22 +00:00
|
|
|
func (u *WSUser) FinalizePage(page string, handle func()) {
|
|
|
|
u.Lock()
|
|
|
|
defer u.Unlock()
|
|
|
|
for _, socket := range u.Sockets {
|
2018-09-12 09:50:57 +00:00
|
|
|
if socket == nil {
|
|
|
|
continue
|
|
|
|
}
|
2018-09-08 05:12:48 +00:00
|
|
|
if socket.Page == page {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
handle()
|
|
|
|
}
|