supreme-disco/server/websocket.go

137 lines
2.4 KiB
Go
Raw Normal View History

2023-05-11 00:43:02 +03:00
package main
import (
"fmt"
"net/http"
2023-07-02 22:23:20 +03:00
"runtime"
2023-07-03 22:53:36 +03:00
"time"
2023-05-11 00:43:02 +03:00
"github.com/gorilla/websocket"
jsoniter "github.com/json-iterator/go"
)
2023-07-02 17:27:26 +03:00
type Client struct {
2023-05-11 00:43:02 +03:00
Chanel string
2023-07-02 17:27:26 +03:00
Conn *websocket.Conn
2023-05-11 00:43:02 +03:00
}
var (
2023-07-02 17:27:26 +03:00
wsClients = make(map[*Client]bool)
2023-05-11 00:43:02 +03:00
json = jsoniter.ConfigCompatibleWithStandardLibrary
ws = struct {
2023-07-02 17:27:26 +03:00
Broadcast chan []byte
Add chan *Client
Del chan *Client
2023-05-11 00:43:02 +03:00
}{
2023-07-03 22:53:36 +03:00
Broadcast: make(chan []byte, 1),
Add: make(chan *Client),
Del: make(chan *Client),
2023-07-02 17:27:26 +03:00
}
upgrader = websocket.Upgrader{
ReadBufferSize: 1024,
WriteBufferSize: 1024,
CheckOrigin: func(r *http.Request) bool {
params := r.URL.Query()
if params["api_key"] != nil || r.Header.Get("origin") == "https://statbate.com" {
return true
}
return false
},
2023-05-11 00:43:02 +03:00
}
)
func broadcast() {
2023-07-03 22:53:36 +03:00
ticker := time.NewTicker(20 * time.Second)
2023-05-11 00:43:02 +03:00
for {
select {
2023-07-02 17:27:26 +03:00
case client := <-ws.Add:
wsClients[client] = true
2023-05-11 00:43:02 +03:00
case conn := <-ws.Del:
delete(wsClients, conn)
2023-07-02 02:54:24 +03:00
case r := <-ws.Broadcast:
2023-07-03 22:53:36 +03:00
//fmt.Println(len(ws.Broadcast), cap(ws.Broadcast))
2023-07-02 02:54:24 +03:00
sendBroadcast(r)
2023-05-11 00:43:02 +03:00
case <-ticker.C:
2023-07-03 22:53:36 +03:00
fmt.Println(len(wsClients), runtime.NumGoroutine(), len(ws.Broadcast), cap(ws.Broadcast))
2023-05-11 00:43:02 +03:00
}
}
}
2023-07-02 02:54:24 +03:00
func sendBroadcast(message []byte) {
2023-07-01 17:17:28 +03:00
input := struct {
Chanel string `json:"chanel"`
}{}
if err := json.Unmarshal(message, &input); err != nil {
fmt.Println("json error: ", err.Error())
return
2023-05-11 00:43:02 +03:00
}
2023-07-02 17:27:26 +03:00
for client := range wsClients {
if client.Chanel != input.Chanel {
2023-05-11 00:43:02 +03:00
continue
}
2023-07-02 22:23:20 +03:00
if err := client.Conn.WriteMessage(1, message); err != nil {
2023-07-03 22:53:36 +03:00
delete(wsClients, client)
2023-07-02 22:23:20 +03:00
client.Conn.Close()
}
2023-07-02 17:27:26 +03:00
}
}
2023-05-11 00:43:02 +03:00
func wsHandler(w http.ResponseWriter, r *http.Request) {
conn, err := upgrader.Upgrade(w, r, nil)
if err != nil {
return
}
go readWS(conn)
}
func readWS(conn *websocket.Conn) {
defer conn.Close()
2023-07-02 17:27:26 +03:00
conn.SetReadDeadline(time.Now().Add(30 * time.Second))
_, message, err := conn.ReadMessage()
if err != nil {
return
}
input := struct {
Chanel string `json:"chanel"`
}{}
if err := json.Unmarshal(message, &input); err != nil {
return
}
2023-07-03 22:53:36 +03:00
2023-07-02 22:23:20 +03:00
client := &Client{Conn: conn, Chanel: input.Chanel}
2023-07-03 22:53:36 +03:00
2023-07-02 22:23:20 +03:00
chanels := map[string]bool{
"chaturbate": true,
"bongacams": true,
"stripchat": true,
"camsoda": true,
}
2023-07-03 22:53:36 +03:00
2023-07-02 22:23:20 +03:00
if !chanels[input.Chanel] {
2023-07-02 17:27:26 +03:00
return
}
2023-07-03 22:53:36 +03:00
2023-07-02 22:23:20 +03:00
ws.Add <- client
2023-07-03 22:53:36 +03:00
2023-05-11 00:43:02 +03:00
defer func() {
2023-07-02 17:27:26 +03:00
ws.Del <- client
2023-05-11 00:43:02 +03:00
}()
for {
2023-07-02 17:27:26 +03:00
conn.SetReadDeadline(time.Now().Add(30 * time.Minute))
2023-07-02 22:23:20 +03:00
_, _, err := conn.ReadMessage()
2023-05-11 00:43:02 +03:00
if err != nil {
return
}
}
}