mirror of
https://github.com/statbate/shiny-potato.git
synced 2026-08-11 03:12:42 +00:00
initial work
Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
This commit is contained in:
commit
2ef3e1fc8d
12 changed files with 1100 additions and 0 deletions
49
app/echo.go
Normal file
49
app/echo.go
Normal file
|
|
@ -0,0 +1,49 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/gorilla/websocket"
|
||||
)
|
||||
|
||||
var (
|
||||
wsClients = make(map[*websocket.Conn]struct{})
|
||||
|
||||
ws = struct {
|
||||
Count chan int
|
||||
Send chan []byte
|
||||
Add chan *websocket.Conn
|
||||
}{
|
||||
Count: make(chan int, 100),
|
||||
Send: make(chan []byte, 100),
|
||||
Add: make(chan *websocket.Conn, 100),
|
||||
}
|
||||
)
|
||||
|
||||
func broadcast() {
|
||||
for {
|
||||
select {
|
||||
case conn := <-ws.Add:
|
||||
wsClients[conn] = struct{}{}
|
||||
|
||||
case <-ws.Count:
|
||||
ws.Count <- len(wsClients)
|
||||
|
||||
case message := <-ws.Send:
|
||||
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 := websocket.Upgrade(w, r, w.Header(), 1024, 1024)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
ws.Add <- conn
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue