asedraw/main.go

44 lines
781 B
Go
Raw Normal View History

2023-11-04 14:13:24 +01:00
package main
import (
"flag"
"github.com/gorilla/websocket"
"log"
"net/http"
)
2023-11-04 18:55:29 +01:00
var addr = flag.String("addr", "localhost:8797", "http service address")
2023-11-04 14:13:24 +01:00
var upgrader = websocket.Upgrader{} // use default options
func echo(w http.ResponseWriter, r *http.Request) {
c, err := upgrader.Upgrade(w, r, nil)
if err != nil {
log.Print("upgrade:", err)
return
}
2023-11-04 18:55:29 +01:00
defer func(c *websocket.Conn) {
err := c.Close()
2023-11-04 14:13:24 +01:00
if err != nil {
2023-11-04 18:55:29 +01:00
log.Println("close: ", err)
return
2023-11-04 14:13:24 +01:00
}
2023-11-04 18:55:29 +01:00
}(c)
for {
mt, message, err := c.ReadMessage()
2023-11-04 14:13:24 +01:00
log.Printf("recv: %s", message)
err = c.WriteMessage(mt, message)
if err != nil {
2023-11-04 18:55:29 +01:00
log.Println(err)
return
2023-11-04 14:13:24 +01:00
}
}
}
func main() {
flag.Parse()
log.SetFlags(0)
http.HandleFunc("/", echo)
log.Fatal(http.ListenAndServe(*addr, nil))
}