1
0
mirror of https://github.com/labstack/echo.git synced 2025-01-10 00:28:23 +02:00
echo/recipe/websocket/net/server.go
Vishal Rana f4b0004d2b website/recipe in the main repo
Signed-off-by: Vishal Rana <vr@labstack.com>
2016-10-20 09:11:07 -07:00

41 lines
741 B
Go

package main
import (
"fmt"
"log"
"github.com/labstack/echo"
"github.com/labstack/echo/engine/standard"
"github.com/labstack/echo/middleware"
"golang.org/x/net/websocket"
)
func hello() websocket.Handler {
return websocket.Handler(func(ws *websocket.Conn) {
for {
// Write
err := websocket.Message.Send(ws, "Hello, Client!")
if err != nil {
log.Fatal(err)
}
// Read
msg := ""
err = websocket.Message.Receive(ws, &msg)
if err != nil {
log.Fatal(err)
}
fmt.Printf("%s\n", msg)
}
})
}
func main() {
e := echo.New()
e.Use(middleware.Logger())
e.Use(middleware.Recover())
e.Use(middleware.Static("../public"))
e.GET("/ws", standard.WrapHandler(hello()))
e.Run(standard.New(":1323"))
}