mirror of
https://github.com/statbate/special-pancake.git
synced 2026-08-11 03:12:42 +00:00
remove echo ws
This commit is contained in:
parent
e1a12157f7
commit
fd9906d0c1
3 changed files with 0 additions and 99 deletions
|
|
@ -49,12 +49,9 @@ func listHandler(w http.ResponseWriter, _ *http.Request) {
|
|||
}
|
||||
|
||||
func debugHandler(w http.ResponseWriter, _ *http.Request) {
|
||||
ws.Count <- 0
|
||||
l := <-ws.Count
|
||||
runtime.ReadMemStats(&memInfo)
|
||||
j, err := json.Marshal(struct {
|
||||
Goroutines int
|
||||
WebSocket int
|
||||
Uptime int64
|
||||
Alloc uint64
|
||||
HeapSys uint64
|
||||
|
|
@ -63,7 +60,6 @@ func debugHandler(w http.ResponseWriter, _ *http.Request) {
|
|||
Alloc: memInfo.Alloc,
|
||||
HeapSys: memInfo.HeapSys,
|
||||
Uptime: uptime,
|
||||
WebSocket: l,
|
||||
})
|
||||
if err == nil {
|
||||
fmt.Fprint(w, string(j))
|
||||
|
|
|
|||
93
app/echo.go
93
app/echo.go
|
|
@ -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
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -54,9 +54,7 @@ func main() {
|
|||
go announceCount()
|
||||
go saveDB()
|
||||
go saveLogs()
|
||||
go broadcast()
|
||||
|
||||
http.HandleFunc("/ws/", wsHandler)
|
||||
http.HandleFunc("/cmd/", cmdHandler)
|
||||
http.HandleFunc("/list/", listHandler)
|
||||
http.HandleFunc("/debug/", debugHandler)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue