This commit is contained in:
poiuty 2023-05-11 14:40:18 +03:00
parent 8bbd54aa5a
commit 0ca280b452
5 changed files with 31 additions and 104 deletions

View file

@ -48,12 +48,9 @@ func listHandler(w http.ResponseWriter, _ *http.Request) {
} }
func debugHandler(w http.ResponseWriter, _ *http.Request) { func debugHandler(w http.ResponseWriter, _ *http.Request) {
ws.Count <- 0
l := <-ws.Count
runtime.ReadMemStats(&memInfo) runtime.ReadMemStats(&memInfo)
j, err := json.Marshal(struct { j, err := json.Marshal(struct {
Goroutines int Goroutines int
WebSocket int
Uptime int64 Uptime int64
Alloc uint64 Alloc uint64
HeapSys uint64 HeapSys uint64
@ -62,7 +59,6 @@ func debugHandler(w http.ResponseWriter, _ *http.Request) {
Alloc: memInfo.Alloc, Alloc: memInfo.Alloc,
HeapSys: memInfo.HeapSys, HeapSys: memInfo.HeapSys,
Uptime: uptime, Uptime: uptime,
WebSocket: l,
}) })
if err == nil { if err == nil {
fmt.Fprint(w, string(j)) fmt.Fprint(w, string(j))

View file

@ -1,93 +0,0 @@
package main
import (
"fmt"
"time"
"net/http"
"github.com/gorilla/websocket"
)
var upgrader = websocket.Upgrader{
ReadBufferSize: 1024,
WriteBufferSize: 1024,
CheckOrigin: func(r *http.Request) bool {
if r.Header.Get("origin") == "https://statbate.com" {
return true
}
return false
},
}
var (
wsClients = make(map[*websocket.Conn]struct{})
ws = struct {
Count chan int
Send chan []byte
Add chan *websocket.Conn
Del chan *websocket.Conn
}{
Count: make(chan int, 100),
Send: make(chan []byte, 100),
Add: make(chan *websocket.Conn, 100),
Del: make(chan *websocket.Conn, 100),
}
)
func broadcast() {
ticker := time.NewTicker(30 * time.Second)
for {
select {
case conn := <-ws.Add:
wsClients[conn] = struct{}{}
case conn := <-ws.Del:
delete(wsClients, conn)
case <-ws.Count:
ws.Count <- len(wsClients)
case message := <-ws.Send:
sendMessage(message)
case <-ticker.C:
sendMessage([]byte("ping"))
}
}
}
func sendMessage(message []byte) {
for conn := range wsClients {
if err := conn.WriteMessage(1, message); err != nil {
conn.Close()
delete(wsClients, conn)
}
}
}
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()
ws.Add <- conn
defer func() {
ws.Del <- conn
}()
for {
_, _, err := conn.ReadMessage()
if err != nil {
fmt.Println("readWS", err.Error())
return
}
}
}

View file

@ -28,6 +28,8 @@ var (
Mysql, Clickhouse *sqlx.DB Mysql, Clickhouse *sqlx.DB
json = jsoniter.ConfigCompatibleWithStandardLibrary json = jsoniter.ConfigCompatibleWithStandardLibrary
socketServer = make(chan []byte, 100)
save = make(chan saveData, 100) save = make(chan saveData, 100)
slog = make(chan saveLog, 100) slog = make(chan saveLog, 100)
@ -54,9 +56,8 @@ func main() {
go announceCount() go announceCount()
go saveDB() go saveDB()
go saveLogs() go saveLogs()
go broadcast() go socketHandler()
http.HandleFunc("/bongacams/ws/", wsHandler)
http.HandleFunc("/bongacams/cmd/", cmdHandler) http.HandleFunc("/bongacams/cmd/", cmdHandler)
http.HandleFunc("/bongacams/list/", listHandler) http.HandleFunc("/bongacams/list/", listHandler)
http.HandleFunc("/bongacams/debug/", debugHandler) http.HandleFunc("/bongacams/debug/", debugHandler)
@ -90,6 +91,19 @@ func initClickhouse() {
Clickhouse = db Clickhouse = db
} }
func socketHandler() {
for {
select {
case b := <-socketServer:
conn, err := net.Dial("unix", "/tmp/echo.sock")
if err == nil {
conn.Write(b)
conn.Close()
}
}
}
}
func randInt(min int, max int) int { func randInt(min int, max int) int {
return min + rand.Intn(max-min) return min + rand.Intn(max-min)
} }

View file

@ -131,16 +131,18 @@ func saveDB() {
if m.Amount > 49 { if m.Amount > 49 {
msg, err := json.Marshal(struct { msg, err := json.Marshal(struct {
Chanel string `json:"chanel"`
Room string `json:"room"` Room string `json:"room"`
Donator string `json:"donator"` Donator string `json:"donator"`
Amount int64 `json:"amount"` Amount int64 `json:"amount"`
}{ }{
Chanel: "bongacams",
Room: m.Room, Room: m.Room,
Donator: m.From, Donator: m.From,
Amount: m.Amount, Amount: m.Amount,
}) })
if err == nil { if err == nil {
ws.Send <- msg socketServer <- msg
} }
} }
@ -154,10 +156,14 @@ func saveDB() {
if minutes >= 5 && now > index["last"]+30 { if minutes >= 5 && now > index["last"]+30 {
seconds += minutes * 60 seconds += minutes * 60
msg, err := json.Marshal(struct { msg, err := json.Marshal(struct {
Chanel string `json:"chanel"`
Index float64 `json:"index"` Index float64 `json:"index"`
}{Index: float64(index["tokens"]) / float64(seconds) * 3600 * 0.025 / 1000}) }{
Chanel: "bongacams",
Index: float64(index["tokens"]) / float64(seconds) * 3600 * 0.025 / 1000,
})
if err == nil { if err == nil {
ws.Send <- msg socketServer <- msg
} }
index["last"] = now index["last"] = now
} }

View file

@ -81,8 +81,12 @@ func announceCount() {
rooms.Count <- 0 rooms.Count <- 0
l := <-rooms.Count l := <-rooms.Count
msg, err := json.Marshal(struct { msg, err := json.Marshal(struct {
Count int `json:"count"` Chanel string `json:"chanel"`
}{Count: l}) Count int `json:"count"`
}{
Chanel: "bongacams",
Count: l,
})
if err == nil { if err == nil {
ws.Send <- msg ws.Send <- msg
} }