package main import ( "fmt" "net/http" "strconv" "github.com/labstack/echo/v4" "github.com/labstack/echo/v4/middleware" "golang.org/x/net/websocket" ) func ws(c echo.Context) error { websocket.Handler(server).ServeHTTP(c.Response(), c.Request()) return nil } func add(c echo.Context) error { tip_number++ return c.String(http.StatusOK, strconv.Itoa(tip_number)) } func server(ws *websocket.Conn) { defer ws.Close() fmt.Printf("new connection\n") for { if tip_number == 0 { continue } var strr string = strconv.Itoa(tip_number) err := websocket.Message.Send(ws, strr) if err != nil { fmt.Print(err) } tip_number = 0 } } var tip_number int = 0 func main() { e := echo.New() e.Use(middleware.Logger()) e.Use(middleware.Recover()) e.Static("/", "./public") e.GET("/ws", ws) e.GET("/add", add) e.Logger.Fatal(e.Start(":1323")) }