From 10c44a6f8cdb35413324f8ce0c82114997be995d Mon Sep 17 00:00:00 2001 From: Vishal Rana Date: Thu, 2 Jul 2015 22:55:00 -0700 Subject: [PATCH] Updated recipe for WebSocket Signed-off-by: Vishal Rana --- recipes/file-upload/server.go | 1 - recipes/streaming-file-upload/server.go | 1 - recipes/websocket/public/index.html | 15 +++-- recipes/websocket/server.go | 20 ++++-- website/docs/recipes/file-upload.md | 1 - website/docs/recipes/streaming-file-upload.md | 1 - website/docs/recipes/websocket.md | 66 +++++++++++++++++-- 7 files changed, 85 insertions(+), 20 deletions(-) diff --git a/recipes/file-upload/server.go b/recipes/file-upload/server.go index bf4a85e1..e687d133 100644 --- a/recipes/file-upload/server.go +++ b/recipes/file-upload/server.go @@ -46,7 +46,6 @@ func upload(c *echo.Context) error { func main() { e := echo.New() - e.Use(mw.Logger()) e.Use(mw.Recover()) diff --git a/recipes/streaming-file-upload/server.go b/recipes/streaming-file-upload/server.go index 9ef52350..6a826439 100644 --- a/recipes/streaming-file-upload/server.go +++ b/recipes/streaming-file-upload/server.go @@ -69,7 +69,6 @@ func upload(c *echo.Context) error { func main() { e := echo.New() - e.Use(mw.Logger()) e.Use(mw.Recover()) diff --git a/recipes/websocket/public/index.html b/recipes/websocket/public/index.html index 1a88724f..3a307896 100644 --- a/recipes/websocket/public/index.html +++ b/recipes/websocket/public/index.html @@ -5,16 +5,14 @@ WebSocket -
-    
+

+ diff --git a/recipes/websocket/server.go b/recipes/websocket/server.go index bcf464c7..01f02ec3 100644 --- a/recipes/websocket/server.go +++ b/recipes/websocket/server.go @@ -1,10 +1,9 @@ package main import ( - "io" - "github.com/labstack/echo" mw "github.com/labstack/echo/middleware" + "golang.org/x/net/websocket" ) func main() { @@ -14,9 +13,20 @@ func main() { e.Use(mw.Recover()) e.Static("/", "public") - e.WebSocket("/ws", func(c *echo.Context) error { - io.Copy(c.Socket(), c.Socket()) - return nil + e.WebSocket("/ws", func(c *echo.Context) (err error) { + ws := c.Socket() + msg := "" + + for { + if err = websocket.Message.Send(ws, "Hello, Client!"); err != nil { + return + } + if err = websocket.Message.Receive(ws, &msg); err != nil { + return + } + println(msg) + } + return }) e.Run(":1323") diff --git a/website/docs/recipes/file-upload.md b/website/docs/recipes/file-upload.md index fb486a68..6852ab09 100644 --- a/website/docs/recipes/file-upload.md +++ b/website/docs/recipes/file-upload.md @@ -57,7 +57,6 @@ func upload(c *echo.Context) error { func main() { e := echo.New() - e.Use(mw.Logger()) e.Use(mw.Recover()) diff --git a/website/docs/recipes/streaming-file-upload.md b/website/docs/recipes/streaming-file-upload.md index 4133122d..88d7096a 100644 --- a/website/docs/recipes/streaming-file-upload.md +++ b/website/docs/recipes/streaming-file-upload.md @@ -77,7 +77,6 @@ func upload(c *echo.Context) error { func main() { e := echo.New() - e.Use(mw.Logger()) e.Use(mw.Recover()) diff --git a/website/docs/recipes/websocket.md b/website/docs/recipes/websocket.md index c3250825..2fd41b38 100644 --- a/website/docs/recipes/websocket.md +++ b/website/docs/recipes/websocket.md @@ -6,22 +6,78 @@ package main import ( - "io" - "github.com/labstack/echo" mw "github.com/labstack/echo/middleware" + "golang.org/x/net/websocket" ) func main() { e := echo.New() + e.Use(mw.Logger()) - e.WebSocket("/ws", func(c *echo.Context) error { - io.Copy(c.Socket(), c.Socket()) - return nil + e.Use(mw.Recover()) + + e.Static("/", "public") + e.WebSocket("/ws", func(c *echo.Context) (err error) { + ws := c.Socket() + msg := "" + + for { + if err = websocket.Message.Send(ws, "Hello, Client!"); err != nil { + return + } + if err = websocket.Message.Receive(ws, &msg); err != nil { + return + } + println(msg) + } + return }) + e.Run(":1323") } ``` +`index.html` + +```html + + + + + WebSocket + + +

+ + + + +``` + ## [Source Code](https://github.com/labstack/echo/blob/master/recipes/websocket)