Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/go/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何访问echo websocket(gorilla)另一个功能_Go_Websocket_Echo_Gorilla_Ws - Fatal编程技术网

如何访问echo websocket(gorilla)另一个功能

如何访问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

通过“/查看”接收POST请求时,
我想通过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"))
}