You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

42 lines
773 B

2 years ago
package moo
import (
"github.com/gorilla/websocket"
"github.com/han-joker/moo-layout/api/moo/confm"
"log"
"net/http"
)
func (s *server) StartWebSocket() {
http.HandleFunc("/", echo)
log.Fatal(http.ListenAndServe(confm.Get().String("websocket.addr"), nil))
}
var upgrader = websocket.Upgrader{
CheckOrigin: func(r *http.Request) bool {
return true
},
}
func echo(w http.ResponseWriter, r *http.Request) {
c, err := upgrader.Upgrade(w, r, nil)
if err != nil {
log.Print("upgrade:", err)
return
}
defer c.Close()
for {
mt, message, err := c.ReadMessage()
if err != nil {
log.Println("read:", err)
break
}
log.Printf("recv: %s", message)
err = c.WriteMessage(mt, message)
if err != nil {
log.Println("write:", err)
break
}
}
}