From 20cc0d355f4f4e12da60bc6eac84f85300c1eee3 Mon Sep 17 00:00:00 2001 From: lingling Date: Fri, 21 Apr 2023 20:28:23 +0800 Subject: [PATCH] =?UTF-8?q?=E5=9F=BA=E4=BA=8Eecho=E7=9A=84ws=E5=AE=9E?= =?UTF-8?q?=E7=8E=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- go.mod | 1 + go.sum | 2 ++ main.go | 64 +++++++++++++++++++++-------------------------- public/index.html | 39 +++++++++++++++++++++++++++++ 4 files changed, 71 insertions(+), 35 deletions(-) create mode 100644 public/index.html diff --git a/go.mod b/go.mod index 0f623f2..be335a8 100644 --- a/go.mod +++ b/go.mod @@ -4,6 +4,7 @@ go 1.16 require ( github.com/dgrijalva/jwt-go v3.2.0+incompatible // indirect + github.com/gorilla/websocket v1.5.0 github.com/howeyc/fsnotify v0.9.0 // indirect github.com/idoubi/goutils v1.3.2 // indirect github.com/idoubi/goz v1.4.4 // indirect diff --git a/go.sum b/go.sum index c507525..21f9249 100644 --- a/go.sum +++ b/go.sum @@ -13,6 +13,8 @@ github.com/golang-jwt/jwt v3.2.2+incompatible h1:IfV12K8xAKAnZqdXVzCZ+TOjboZ2keL github.com/golang-jwt/jwt v3.2.2+incompatible/go.mod h1:8pz2t5EyA70fFQQSrl6XZXzqecmYZeUEB8OUGHkxJ+I= github.com/google/go-cmp v0.5.7/go.mod h1:n+brtR0CgQNWTVd5ZUFpTBC8YFBDLK/h/bpaJ8/DtOE= github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= +github.com/gorilla/websocket v1.5.0 h1:PPwGk2jz7EePpoHN/+ClbZu8SPxiqlu12wZP/3sWmnc= +github.com/gorilla/websocket v1.5.0/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE= github.com/howeyc/fsnotify v0.9.0 h1:0gtV5JmOKH4A8SsFxG2BczSeXWWPvcMT0euZt5gDAxY= github.com/howeyc/fsnotify v0.9.0/go.mod h1:41HzSPxBGeFRQKEEwgh49TRw/nKBsYZ2cF1OzPjSJsA= github.com/idoubi/goutils v1.1.0/go.mod h1:BVikG2hf3aDWXsBzBw3X7l4jSuuxNQQVgrxQ6amIAOk= diff --git a/main.go b/main.go index 81f6ed7..e98e1be 100644 --- a/main.go +++ b/main.go @@ -3,53 +3,47 @@ package main import ( "fmt" "net/http" + "strconv" "github.com/labstack/echo/v4" "github.com/labstack/echo/v4/middleware" "golang.org/x/net/websocket" ) -type result struct { - // 通过json标签指定json字段名 - Sataus int `json:"sataus"` - Code int `json:"code"` - Tip_number int `json:"tip_number"` +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.CORSWithConfig(middleware.CORSConfig{AllowOrigins: []string{"*"}, AllowMethods: []string{echo.GET, echo.HEAD, echo.PUT, echo.PATCH, echo.POST, echo.DELETE}})) - - e.GET("/add", add) + 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")) } - -func add(c echo.Context) error { - tip_number++ - return c.JSON(http.StatusOK, "ok") -} -func ws(c echo.Context) error { - - websocket.Handler(func(ws *websocket.Conn) { - defer ws.Close() - for { - // Write - err := websocket.Message.Send(ws, tip_number) - if err != nil { - c.Logger().Error(err) - } - - // Read - msg := "" - err = websocket.Message.Receive(ws, &msg) - if err != nil { - c.Logger().Error(err) - } - fmt.Printf("%s\n", msg) - } - }).ServeHTTP(c.Response(), c.Request()) - return nil -} diff --git a/public/index.html b/public/index.html new file mode 100644 index 0000000..56cd284 --- /dev/null +++ b/public/index.html @@ -0,0 +1,39 @@ + + + + + + WebSocket + + + +

+ + + + +