如何访问echo websocket(gorilla)另一个功能
通过“/查看”接收POST请求时,如何访问echo websocket(gorilla)另一个功能,go,websocket,echo,gorilla,ws,Go,Websocket,Echo,Gorilla,Ws,通过“/查看”接收POST请求时, 我想通过web套接字(ws)显示请求后正文数据 但我不知道如何访问ws 我使用echo服务器和gorilla websocket main.go package main import ( ... "github.com/gorilla/websocket" "github.com/labstack/echo" ) var ( rwMutex = new(sync.RW
我想通过web套接字(ws)显示请求后正文数据 但我不知道如何访问ws 我使用echo服务器和gorilla websocket main.go
package main
import (
...
"github.com/gorilla/websocket"
"github.com/labstack/echo"
)
var (
rwMutex = new(sync.RWMutex)
dataSendChannel = make(chan []byte)
upgrader = websocket.Upgrader{}
m = echo.Map{}
)
当有人访问“/”时生成ws
func handleHome(con echo.Context) error {
ws, err := upgrader.Upgrade(con.Response(), con.Request(), nil)
defer ws.Close()
return con.File("home.html")
}
func-dataMakeJSON(m-map[string]接口{},c-chan
func handleDataList(con echo.Context) error {
// socket write
if err := con.Bind(&m); err != nil {
...
}
go dataMakeJSON(m, dataSendChannel)
go dataSendToView(ws, dataSendChannel) // This is wrong!
return con.String(http.StatusOK, "Success!")
}
func dataMakeJSON(m map[string]interface{}, c chan<- []byte) {
...
c <- jsonString
}
func dataSendToView(ws *websocket.Conn, c <-chan []byte) {
err := ws.WriteMessage(websocket.TextMessage, <-c)
...
}
func main() {
e := echo.New()
e.GET("/", handleHome)
e.POST("/view", handleDataList)
e.Logger.Fatal(e.Start(":1323"))
}