Compare commits
No commits in common. "0c5a95a0cb4c5bc0338876d4cf89e4ef28c91b29" and "610052661042bd292602eaa6e1ea9bf46609f7a7" have entirely different histories.
0c5a95a0cb
...
6100526610
4 changed files with 26 additions and 33 deletions
|
@ -2,13 +2,19 @@ package websocket
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/gorilla/websocket"
|
"github.com/gorilla/websocket"
|
||||||
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"git.smsvc.net/pomodoro/GoTomato/internal/helper"
|
"git.smsvc.net/pomodoro/GoTomato/internal/helper"
|
||||||
"git.smsvc.net/pomodoro/GoTomato/pkg/models"
|
"git.smsvc.net/pomodoro/GoTomato/pkg/models"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// Clients is a map of connected WebSocket clients, where each client is represented by the WebsocketClient struct
|
||||||
|
var Clients = make(map[net.Addr]*models.WebsocketClient)
|
||||||
|
var mu sync.Mutex // Mutex to protect access to the Clients map
|
||||||
|
|
||||||
// Upgrade HTTP requests to WebSocket connections
|
// Upgrade HTTP requests to WebSocket connections
|
||||||
var upgrader = websocket.Upgrader{
|
var upgrader = websocket.Upgrader{
|
||||||
CheckOrigin: func(r *http.Request) bool { return true },
|
CheckOrigin: func(r *http.Request) bool { return true },
|
||||||
|
|
|
@ -1,18 +0,0 @@
|
||||||
package websocket
|
|
||||||
|
|
||||||
import (
|
|
||||||
"net"
|
|
||||||
"sync"
|
|
||||||
|
|
||||||
"git.smsvc.net/pomodoro/GoTomato/pkg/models"
|
|
||||||
)
|
|
||||||
|
|
||||||
const SEND_TIMEOUT = 10
|
|
||||||
const STALE_CLIENT_TIMEOUT = 90
|
|
||||||
const STALE_CHECK_INTERVALL = 30
|
|
||||||
|
|
||||||
// Clients is a map of connected WebSocket clients, where each client is represented by the WebsocketClient struct
|
|
||||||
var Clients = make(map[net.Addr]*models.WebsocketClient)
|
|
||||||
|
|
||||||
// Mutex to protect access to the Clients map
|
|
||||||
var mu sync.Mutex
|
|
|
@ -8,6 +8,21 @@ import (
|
||||||
"github.com/gorilla/websocket"
|
"github.com/gorilla/websocket"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func sendPing(client *models.WebsocketClient) bool {
|
||||||
|
client.Conn.SetWriteDeadline(time.Now().Add(SEND_TIMEOUT * time.Second))
|
||||||
|
err := client.Conn.WriteMessage(websocket.PingMessage, nil)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
func isStale(client *models.WebsocketClient) bool {
|
||||||
|
return time.Since(client.LastPong) > (STALE_CLIENT_TIMEOUT * time.Second)
|
||||||
|
}
|
||||||
|
|
||||||
// Check and remove stale clients
|
// Check and remove stale clients
|
||||||
func RemoveStaleClients() {
|
func RemoveStaleClients() {
|
||||||
ticker := time.NewTicker(STALE_CHECK_INTERVALL * time.Second)
|
ticker := time.NewTicker(STALE_CHECK_INTERVALL * time.Second)
|
||||||
|
@ -25,18 +40,3 @@ func RemoveStaleClients() {
|
||||||
mu.Unlock()
|
mu.Unlock()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func sendPing(client *models.WebsocketClient) bool {
|
|
||||||
client.Conn.SetWriteDeadline(time.Now().Add(SEND_TIMEOUT * time.Second))
|
|
||||||
err := client.Conn.WriteMessage(websocket.PingMessage, nil)
|
|
||||||
|
|
||||||
if err != nil {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
|
|
||||||
func isStale(client *models.WebsocketClient) bool {
|
|
||||||
return time.Since(client.LastPong) > (STALE_CLIENT_TIMEOUT * time.Second)
|
|
||||||
}
|
|
||||||
|
|
5
internal/websocket/timeouts.go
Normal file
5
internal/websocket/timeouts.go
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
package websocket
|
||||||
|
|
||||||
|
const SEND_TIMEOUT = 10
|
||||||
|
const STALE_CLIENT_TIMEOUT = 90
|
||||||
|
const STALE_CHECK_INTERVALL = 30
|
Loading…
Reference in a new issue