mirror of
https://github.com/statbate/animated-sniffle.git
synced 2026-08-11 02:12:41 +00:00
Update echo.go
This commit is contained in:
parent
59f6db57f8
commit
783c03a2ee
1 changed files with 52 additions and 8 deletions
60
app/echo.go
60
app/echo.go
|
|
@ -1,11 +1,24 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
|
"time"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
"github.com/gorilla/websocket"
|
"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 (
|
var (
|
||||||
wsClients = make(map[*websocket.Conn]struct{})
|
wsClients = make(map[*websocket.Conn]struct{})
|
||||||
|
|
||||||
|
|
@ -13,37 +26,68 @@ var (
|
||||||
Count chan int
|
Count chan int
|
||||||
Send chan []byte
|
Send chan []byte
|
||||||
Add chan *websocket.Conn
|
Add chan *websocket.Conn
|
||||||
|
Del chan *websocket.Conn
|
||||||
}{
|
}{
|
||||||
Count: make(chan int, 100),
|
Count: make(chan int, 100),
|
||||||
Send: make(chan []byte, 100),
|
Send: make(chan []byte, 100),
|
||||||
Add: make(chan *websocket.Conn, 100),
|
Add: make(chan *websocket.Conn, 100),
|
||||||
|
Del: make(chan *websocket.Conn, 100),
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
func broadcast() {
|
func broadcast() {
|
||||||
|
ticker := time.NewTicker(30 * time.Second)
|
||||||
for {
|
for {
|
||||||
select {
|
select {
|
||||||
case conn := <-ws.Add:
|
case conn := <-ws.Add:
|
||||||
wsClients[conn] = struct{}{}
|
wsClients[conn] = struct{}{}
|
||||||
|
|
||||||
|
case conn := <-ws.Del:
|
||||||
|
delete(wsClients, conn)
|
||||||
|
|
||||||
case <-ws.Count:
|
case <-ws.Count:
|
||||||
ws.Count <- len(wsClients)
|
ws.Count <- len(wsClients)
|
||||||
|
|
||||||
case message := <-ws.Send:
|
case message := <-ws.Send:
|
||||||
for conn := range wsClients {
|
sendMessage(message)
|
||||||
if err := conn.WriteMessage(1, message); err != nil {
|
|
||||||
conn.Close()
|
case <-ticker.C:
|
||||||
delete(wsClients, conn)
|
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) {
|
func wsHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
conn, err := websocket.Upgrade(w, r, w.Header(), 1024, 1024)
|
conn, err := upgrader.Upgrade(w, r, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
ws.Add <- conn
|
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
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue