This commit is contained in:
poiuty 2023-07-02 22:23:20 +03:00
parent dd7ac94739
commit e0d1877fb9

View file

@ -4,6 +4,7 @@ import (
"fmt" "fmt"
"net/http" "net/http"
"time" "time"
"runtime"
"github.com/gorilla/websocket" "github.com/gorilla/websocket"
jsoniter "github.com/json-iterator/go" jsoniter "github.com/json-iterator/go"
@ -14,11 +15,6 @@ type Client struct {
Conn *websocket.Conn Conn *websocket.Conn
} }
type sendMsg struct {
Message []byte
Conn *websocket.Conn
}
var ( var (
wsClients = make(map[*Client]bool) wsClients = make(map[*Client]bool)
@ -26,12 +22,10 @@ var (
ws = struct { ws = struct {
Broadcast chan []byte Broadcast chan []byte
Send chan sendMsg
Add chan *Client Add chan *Client
Del chan *Client Del chan *Client
}{ }{
Broadcast: make(chan []byte, 100), Broadcast: make(chan []byte, 100),
Send: make(chan sendMsg, 100),
Add: make(chan *Client, 100), Add: make(chan *Client, 100),
Del: make(chan *Client, 100), Del: make(chan *Client, 100),
} }
@ -59,24 +53,15 @@ func broadcast() {
case conn := <-ws.Del: case conn := <-ws.Del:
delete(wsClients, conn) delete(wsClients, conn)
case r := <-ws.Send:
sendMessage(r.Conn, r.Message)
case r := <-ws.Broadcast: case r := <-ws.Broadcast:
sendBroadcast(r) sendBroadcast(r)
case <-ticker.C: case <-ticker.C:
fmt.Println("WebSocket:", len(wsClients)) fmt.Println("WebSocket:", len(wsClients), "Goroutines:", runtime.NumGoroutine())
} }
} }
} }
func sendMessage(conn *websocket.Conn, message []byte) {
if err := conn.WriteMessage(1, message); err != nil {
conn.Close()
}
}
func sendBroadcast(message []byte) { func sendBroadcast(message []byte) {
input := struct { input := struct {
Chanel string `json:"chanel"` Chanel string `json:"chanel"`
@ -89,25 +74,12 @@ func sendBroadcast(message []byte) {
if client.Chanel != input.Chanel { if client.Chanel != input.Chanel {
continue continue
} }
sendMessage(client.Conn, message) if err := client.Conn.WriteMessage(1, message); err != nil {
client.Conn.Close()
}
} }
} }
func enterChannel(conn *websocket.Conn, chanel string) (*Client, bool) {
chanels := map[string]bool{
"chaturbate": true,
"bongacams": true,
"stripchat": true,
"camsoda": true,
}
client := &Client{Conn: conn, Chanel: chanel}
if chanels[chanel] {
ws.Add <- client
return client, true
}
return client, false
}
func wsHandler(w http.ResponseWriter, r *http.Request) { func wsHandler(w http.ResponseWriter, r *http.Request) {
conn, err := upgrader.Upgrade(w, r, nil) conn, err := upgrader.Upgrade(w, r, nil)
if err != nil { if err != nil {
@ -133,29 +105,30 @@ func readWS(conn *websocket.Conn) {
return return
} }
client, ok := enterChannel(conn, input.Chanel) client := &Client{Conn: conn, Chanel: input.Chanel}
if !ok {
chanels := map[string]bool{
"chaturbate": true,
"bongacams": true,
"stripchat": true,
"camsoda": true,
}
if !chanels[input.Chanel] {
return return
} }
ws.Add <- client
defer func() { defer func() {
ws.Del <- client ws.Del <- client
}() }()
ping := time.Now().Unix()
for { for {
conn.SetReadDeadline(time.Now().Add(30 * time.Minute)) conn.SetReadDeadline(time.Now().Add(30 * time.Minute))
_, message, err := conn.ReadMessage() _, _, err := conn.ReadMessage()
if err != nil { if err != nil {
return return
} }
if string(message) == "ping" {
if time.Now().Unix() > ping {
ws.Send <- sendMsg{Conn: conn, Message: []byte("pong")}
ping = time.Now().Unix() + 15
}
continue
}
} }
} }