supreme-disco/server/websocket.go

162 lines
2.9 KiB
Go
Raw Normal View History

2023-05-11 00:43:02 +03:00
package main
import (
"fmt"
"net/http"
"time"
"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
}
2023-07-02 02:54:24 +03:00
type sendMsg struct {
Message []byte
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
Send chan sendMsg
Add chan *Client
Del chan *Client
2023-05-11 00:43:02 +03:00
}{
2023-07-02 17:27:26 +03:00
Broadcast: make(chan []byte, 100),
Send: make(chan sendMsg, 100),
Add: make(chan *Client, 100),
Del: make(chan *Client, 100),
}
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() {
ticker := time.NewTicker(30 * time.Second)
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)
case r := <-ws.Send:
2023-07-02 02:54:24 +03:00
sendMessage(r.Conn, r.Message)
2023-07-02 17:27:26 +03:00
2023-07-02 02:54:24 +03:00
case r := <-ws.Broadcast:
sendBroadcast(r)
2023-05-11 00:43:02 +03:00
case <-ticker.C:
2023-06-09 14:57:43 +03:00
fmt.Println("WebSocket:", len(wsClients))
2023-05-11 00:43:02 +03:00
}
}
}
2023-07-02 17:27:26 +03:00
func sendMessage(conn *websocket.Conn, message []byte) {
2023-07-02 02:54:24 +03:00
if err := conn.WriteMessage(1, message); err != nil {
conn.Close()
}
}
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 17:27:26 +03:00
sendMessage(client.Conn, message)
2023-05-11 00:43:02 +03:00
}
}
2023-07-02 17:27:26 +03:00
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
}
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
}
client, ok := enterChannel(conn, input.Chanel)
if !ok {
return
}
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
}()
2023-07-01 17:17:28 +03:00
ping := time.Now().Unix()
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-05-11 00:43:02 +03:00
_, message, err := conn.ReadMessage()
if err != nil {
return
}
2023-07-01 17:17:28 +03:00
if string(message) == "ping" {
if time.Now().Unix() > ping {
2023-07-02 02:54:24 +03:00
ws.Send <- sendMsg{Conn: conn, Message: []byte("pong")}
2023-07-01 17:17:28 +03:00
ping = time.Now().Unix() + 15
}
continue
}
2023-05-11 00:43:02 +03:00
}
}