2018-09-08 05:12:48 +00:00
|
|
|
package common
|
|
|
|
|
|
|
|
import (
|
2022-02-21 03:32:53 +00:00
|
|
|
"errors"
|
|
|
|
"sync"
|
|
|
|
"time"
|
2018-09-08 05:12:48 +00:00
|
|
|
|
2022-02-21 03:32:53 +00:00
|
|
|
"github.com/gorilla/websocket"
|
2018-09-08 05:12:48 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
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 {
|
2022-02-21 03:32:53 +00:00
|
|
|
User *User
|
|
|
|
Sockets []*WSUserSocket
|
|
|
|
sync.Mutex
|
2018-09-08 05:12:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type WSUserSocket struct {
|
2022-02-21 03:32:53 +00:00
|
|
|
conn *websocket.Conn
|
|
|
|
Page string
|
2018-09-08 05:12:48 +00:00
|
|
|
}
|
|
|
|
|
2019-10-01 21:06:22 +00:00
|
|
|
func (u *WSUser) Ping() error {
|
2022-02-21 03:32:53 +00:00
|
|
|
var sockets []*WSUserSocket
|
|
|
|
var del int
|
|
|
|
func() {
|
|
|
|
u.Lock()
|
|
|
|
defer u.Unlock()
|
|
|
|
for i, s := range u.Sockets {
|
|
|
|
if s == nil || s.conn == nil {
|
|
|
|
del++
|
|
|
|
u.Sockets[i] = u.Sockets[len(u.Sockets)-del]
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
sockets = append(sockets, s)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
if del > 0 {
|
|
|
|
// TODO: Resize the capacity to release memory more eagerly?
|
|
|
|
u.Sockets = u.Sockets[:len(u.Sockets)-del]
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, s := range sockets {
|
|
|
|
_ = s.conn.SetWriteDeadline(time.Now().Add(time.Minute))
|
|
|
|
e := s.conn.WriteMessage(websocket.PingMessage, nil)
|
|
|
|
if e != nil {
|
|
|
|
s.conn.Close()
|
|
|
|
u.Lock()
|
|
|
|
s.conn = nil
|
|
|
|
u.Unlock()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2019-02-28 07:28:17 +00:00
|
|
|
}
|
|
|
|
|
2019-10-01 21:06:22 +00:00
|
|
|
func (u *WSUser) WriteAll(msg string) error {
|
2022-02-21 03:32:53 +00:00
|
|
|
msgbytes := []byte(msg)
|
|
|
|
for _, socket := range u.Sockets {
|
|
|
|
if socket == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
w, e := socket.conn.NextWriter(websocket.TextMessage)
|
|
|
|
if e != nil {
|
|
|
|
return e
|
|
|
|
}
|
|
|
|
_, _ = w.Write(msgbytes)
|
|
|
|
w.Close()
|
|
|
|
}
|
|
|
|
return nil
|
2018-09-08 05:12:48 +00:00
|
|
|
}
|
|
|
|
|
2020-02-05 02:48:35 +00:00
|
|
|
func (u *WSUser) WriteToPage(msg, page string) error {
|
2022-02-21 03:32:53 +00:00
|
|
|
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 {
|
2022-02-21 03:32:53 +00:00
|
|
|
var success bool
|
|
|
|
for _, socket := range u.Sockets {
|
|
|
|
if socket == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if socket.Page != page {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
w, e := socket.conn.NextWriter(websocket.TextMessage)
|
|
|
|
if e != nil {
|
|
|
|
continue // Skip dead sockets, a dedicated goroutine handles those
|
|
|
|
}
|
|
|
|
_, _ = w.Write(msg)
|
|
|
|
w.Close()
|
|
|
|
success = true
|
|
|
|
}
|
|
|
|
if !success {
|
|
|
|
return ErrNoneOnPage
|
|
|
|
}
|
|
|
|
return nil
|
2018-09-08 05:12:48 +00:00
|
|
|
}
|
|
|
|
|
2020-07-16 10:51:34 +00:00
|
|
|
// Inefficient as it looks for sockets for a page even if there are none
|
|
|
|
func (u *WSUser) WriteToPageBytesMulti(msgs [][]byte, page string) error {
|
2022-02-21 03:32:53 +00:00
|
|
|
var success bool
|
|
|
|
for _, socket := range u.Sockets {
|
|
|
|
if socket == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if socket.Page != page {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
w, e := socket.conn.NextWriter(websocket.TextMessage)
|
|
|
|
if e != 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
|
2020-07-16 10:51:34 +00:00
|
|
|
}
|
|
|
|
|
2021-05-08 06:34:46 +00:00
|
|
|
func (u *WSUser) CountSockets() int {
|
2022-02-21 03:32:53 +00:00
|
|
|
u.Lock()
|
|
|
|
defer u.Unlock()
|
|
|
|
return len(u.Sockets)
|
2021-05-08 06:34:46 +00:00
|
|
|
}
|
|
|
|
|
2019-10-01 21:06:22 +00:00
|
|
|
func (u *WSUser) AddSocket(conn *websocket.Conn, page string) {
|
2022-02-21 03:32:53 +00:00
|
|
|
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
|
|
|
|
/*if len(u.Sockets) < 6 {
|
|
|
|
for i, socket := range u.Sockets {
|
|
|
|
if socket == nil {
|
|
|
|
u.Sockets[i] = &WSUserSocket{conn, page}
|
|
|
|
u.Unlock()
|
|
|
|
//fmt.Printf("%+v\n", u.Sockets)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}*/
|
|
|
|
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) {
|
2022-02-21 03:32:53 +00:00
|
|
|
var del int
|
|
|
|
u.Lock()
|
|
|
|
defer u.Unlock()
|
|
|
|
for i, socket := range u.Sockets {
|
|
|
|
if socket == nil || socket.conn == nil {
|
|
|
|
del++
|
|
|
|
u.Sockets[i] = u.Sockets[len(u.Sockets)-del]
|
|
|
|
} else if socket.conn == conn {
|
|
|
|
del++
|
|
|
|
u.Sockets[i] = u.Sockets[len(u.Sockets)-del]
|
|
|
|
//break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
//Logf("%+v\n", u.Sockets)
|
|
|
|
//Log("del: ", del)
|
|
|
|
if del > 0 {
|
|
|
|
// TODO: Resize the capacity to release memory more eagerly?
|
|
|
|
u.Sockets = u.Sockets[:len(u.Sockets)-del]
|
|
|
|
}
|
|
|
|
//Logf("%+v\n", u.Sockets)
|
|
|
|
return
|
|
|
|
|
|
|
|
if len(u.Sockets) < 6 {
|
|
|
|
for i, socket := range u.Sockets {
|
|
|
|
if socket == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if socket.conn == conn {
|
|
|
|
u.Sockets[i] = nil
|
|
|
|
//fmt.Printf("%+v\n", wsUser.Sockets)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var key int
|
|
|
|
for i, socket := range u.Sockets {
|
|
|
|
if socket.conn == conn {
|
|
|
|
key = i
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
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
|
|
|
func (u *WSUser) SetPageForSocket(conn *websocket.Conn, page string) error {
|
2022-02-21 03:32:53 +00:00
|
|
|
if conn == nil {
|
|
|
|
return ErrInvalidSocket
|
|
|
|
}
|
|
|
|
|
|
|
|
u.Lock()
|
|
|
|
for _, socket := range u.Sockets {
|
|
|
|
if socket == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if socket.conn == conn {
|
|
|
|
socket.Page = page
|
|
|
|
}
|
|
|
|
}
|
|
|
|
u.Unlock()
|
|
|
|
|
|
|
|
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 {
|
2022-02-21 03:32:53 +00:00
|
|
|
u.Lock()
|
|
|
|
defer u.Unlock()
|
|
|
|
for _, socket := range u.Sockets {
|
|
|
|
if socket == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if socket.Page == page {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
2018-09-08 05:12:48 +00:00
|
|
|
}
|
|
|
|
|
2021-04-07 14:23:11 +00:00
|
|
|
func (u *WSUser) FinalizePage(page string, h func()) {
|
2022-02-21 03:32:53 +00:00
|
|
|
u.Lock()
|
|
|
|
defer u.Unlock()
|
|
|
|
for _, socket := range u.Sockets {
|
|
|
|
if socket == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if socket.Page == page {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
h()
|
2018-09-08 05:12:48 +00:00
|
|
|
}
|