From e0d1877fb9ac0fb3159abd8fec95157c350972fd Mon Sep 17 00:00:00 2001 From: poiuty Date: Sun, 2 Jul 2023 22:23:20 +0300 Subject: [PATCH] update --- server/websocket.go | 67 ++++++++++++++------------------------------- 1 file changed, 20 insertions(+), 47 deletions(-) diff --git a/server/websocket.go b/server/websocket.go index ca11197..0c2a214 100644 --- a/server/websocket.go +++ b/server/websocket.go @@ -4,6 +4,7 @@ import ( "fmt" "net/http" "time" + "runtime" "github.com/gorilla/websocket" jsoniter "github.com/json-iterator/go" @@ -14,11 +15,6 @@ type Client struct { Conn *websocket.Conn } -type sendMsg struct { - Message []byte - Conn *websocket.Conn -} - var ( wsClients = make(map[*Client]bool) @@ -26,12 +22,10 @@ var ( ws = struct { Broadcast chan []byte - Send chan sendMsg Add chan *Client Del chan *Client }{ Broadcast: make(chan []byte, 100), - Send: make(chan sendMsg, 100), Add: make(chan *Client, 100), Del: make(chan *Client, 100), } @@ -59,24 +53,15 @@ func broadcast() { case conn := <-ws.Del: delete(wsClients, conn) - case r := <-ws.Send: - sendMessage(r.Conn, r.Message) - case r := <-ws.Broadcast: sendBroadcast(r) 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) { input := struct { Chanel string `json:"chanel"` @@ -89,25 +74,12 @@ func sendBroadcast(message []byte) { if client.Chanel != input.Chanel { 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) { conn, err := upgrader.Upgrade(w, r, nil) if err != nil { @@ -132,30 +104,31 @@ func readWS(conn *websocket.Conn) { if err := json.Unmarshal(message, &input); err != nil { return } - - client, ok := enterChannel(conn, input.Chanel) - if !ok { + + client := &Client{Conn: conn, Chanel: input.Chanel} + + chanels := map[string]bool{ + "chaturbate": true, + "bongacams": true, + "stripchat": true, + "camsoda": true, + } + + if !chanels[input.Chanel] { return } - + + ws.Add <- client + defer func() { ws.Del <- client }() - ping := time.Now().Unix() for { conn.SetReadDeadline(time.Now().Add(30 * time.Minute)) - _, message, err := conn.ReadMessage() + _, _, err := conn.ReadMessage() if err != nil { return } - - if string(message) == "ping" { - if time.Now().Unix() > ping { - ws.Send <- sendMsg{Conn: conn, Message: []byte("pong")} - ping = time.Now().Unix() + 15 - } - continue - } } }