initial work

Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
This commit is contained in:
Vasiliy Tolstov 2022-08-12 01:12:24 +03:00
commit 2ef3e1fc8d
12 changed files with 1100 additions and 0 deletions

49
app/echo.go Normal file
View 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
}