package addon import ( "net/http" "text/template" "github.com/gorilla/websocket" "github.com/sirupsen/logrus" ) func (web *WebAddon) echo(w http.ResponseWriter, r *http.Request) { c, err := web.upgrader.Upgrade(w, r, nil) if err != nil { web.log.Print("upgrade:", err) return } defer c.Close() for { mt, message, err := c.ReadMessage() if err != nil { web.log.Println("read:", err) break } web.log.Printf("recv: %s", message) err = c.WriteMessage(mt, message) if err != nil { web.log.Println("write:", err) break } } } func home(w http.ResponseWriter, r *http.Request) { homeTemplate.Execute(w, "ws://"+r.Host+"/echo") } type WebAddon struct { Base addr string upgrader *websocket.Upgrader serverMux *http.ServeMux server *http.Server log *logrus.Entry } func NewWebAddon() *WebAddon { web := new(WebAddon) web.addr = ":9081" web.upgrader = &websocket.Upgrader{} web.serverMux = new(http.ServeMux) web.serverMux.HandleFunc("/echo", web.echo) web.serverMux.HandleFunc("/", home) web.server = &http.Server{Addr: web.addr, Handler: web.serverMux} web.log = log.WithField("in", "WebAddon") go func() { web.log.Infof("server start listen at %v\n", web.addr) err := web.server.ListenAndServe() web.log.Error(err) }() return web } var homeTemplate = template.Must(template.New("").Parse(`

Click "Open" to create a connection to the server, "Send" to send a message to the server and "Close" to close the connection. You can change the message and send multiple times.

`))