From c572272f17ece487e7b48490f56864c5cd74cc01 Mon Sep 17 00:00:00 2001 From: Ola <1386739+olahol@users.noreply.github.com> Date: Tue, 18 Oct 2022 13:28:24 +0200 Subject: [PATCH] Update gophers example. --- .travis.yml | 9 ------ README.md | 59 ++++++++++++++++++++++++++-------------- examples/gophers/main.go | 59 ++++++++++++++++++++++++++-------------- 3 files changed, 78 insertions(+), 49 deletions(-) delete mode 100644 .travis.yml diff --git a/.travis.yml b/.travis.yml deleted file mode 100644 index 556fc18..0000000 --- a/.travis.yml +++ /dev/null @@ -1,9 +0,0 @@ -language: go -sudo: required -go: -- 1.x -- master -install: - - go get github.com/gorilla/websocket -script: - - go test diff --git a/README.md b/README.md index 7f5761a..b09a384 100644 --- a/README.md +++ b/README.md @@ -114,22 +114,29 @@ type GopherInfo struct { } func main() { - router := gin.Default() - mrouter := melody.New() + r := gin.Default() + m := melody.New() - router.GET("/", func(c *gin.Context) { + r.GET("/", func(c *gin.Context) { http.ServeFile(c.Writer, c.Request, "index.html") }) - router.GET("/ws", func(c *gin.Context) { - mrouter.HandleRequest(c.Writer, c.Request) + r.GET("/ws", func(c *gin.Context) { + m.HandleRequest(c.Writer, c.Request) }) - mrouter.HandleConnect(func(s *melody.Session) { - ss, _ := mrouter.Sessions() + m.HandleConnect(func(s *melody.Session) { + ss, _ := m.Sessions() for _, o := range ss { - info := o.MustGet("info").(*GopherInfo) + value, exists := o.Get("info") + + if !exists { + continue + } + + info := value.(*GopherInfo) + s.Write([]byte("set " + info.ID + " " + info.X + " " + info.Y)) } @@ -139,22 +146,34 @@ func main() { s.Write([]byte("iam " + id)) }) - mrouter.HandleDisconnect(func(s *melody.Session) { - info := s.MustGet("info").(*GopherInfo) - mrouter.BroadcastOthers([]byte("dis "+info.ID), s) - }) + m.HandleDisconnect(func(s *melody.Session) { + value, exists := s.Get("info") - mrouter.HandleMessage(func(s *melody.Session, msg []byte) { - p := strings.Split(string(msg), " ") - if len(p) == 2 { - info := s.MustGet("info").(*GopherInfo) - info.X = p[0] - info.Y = p[1] - mrouter.BroadcastOthers([]byte("set "+info.ID+" "+info.X+" "+info.Y), s) + if !exists { + return } + + info := value.(*GopherInfo) + + m.BroadcastOthers([]byte("dis "+info.ID), s) }) - router.Run(":5000") + m.HandleMessage(func(s *melody.Session, msg []byte) { + p := strings.Split(string(msg), " ") + value, exists := s.Get("info") + + if len(p) != 2 || !exists { + return + } + + info := value.(*GopherInfo) + info.X = p[0] + info.Y = p[1] + + m.BroadcastOthers([]byte("set "+info.ID+" "+info.X+" "+info.Y), s) + }) + + r.Run(":5000") } ``` diff --git a/examples/gophers/main.go b/examples/gophers/main.go index 56592d3..496f429 100644 --- a/examples/gophers/main.go +++ b/examples/gophers/main.go @@ -14,22 +14,29 @@ type GopherInfo struct { } func main() { - router := gin.Default() - mrouter := melody.New() + r := gin.Default() + m := melody.New() - router.GET("/", func(c *gin.Context) { + r.GET("/", func(c *gin.Context) { http.ServeFile(c.Writer, c.Request, "index.html") }) - router.GET("/ws", func(c *gin.Context) { - mrouter.HandleRequest(c.Writer, c.Request) + r.GET("/ws", func(c *gin.Context) { + m.HandleRequest(c.Writer, c.Request) }) - mrouter.HandleConnect(func(s *melody.Session) { - ss, _ := mrouter.Sessions() + m.HandleConnect(func(s *melody.Session) { + ss, _ := m.Sessions() for _, o := range ss { - info := o.MustGet("info").(*GopherInfo) + value, exists := o.Get("info") + + if !exists { + continue + } + + info := value.(*GopherInfo) + s.Write([]byte("set " + info.ID + " " + info.X + " " + info.Y)) } @@ -39,20 +46,32 @@ func main() { s.Write([]byte("iam " + id)) }) - mrouter.HandleDisconnect(func(s *melody.Session) { - info := s.MustGet("info").(*GopherInfo) - mrouter.BroadcastOthers([]byte("dis "+info.ID), s) - }) + m.HandleDisconnect(func(s *melody.Session) { + value, exists := s.Get("info") - mrouter.HandleMessage(func(s *melody.Session, msg []byte) { - p := strings.Split(string(msg), " ") - if len(p) == 2 { - info := s.MustGet("info").(*GopherInfo) - info.X = p[0] - info.Y = p[1] - mrouter.BroadcastOthers([]byte("set "+info.ID+" "+info.X+" "+info.Y), s) + if !exists { + return } + + info := value.(*GopherInfo) + + m.BroadcastOthers([]byte("dis "+info.ID), s) }) - router.Run(":5000") + m.HandleMessage(func(s *melody.Session, msg []byte) { + p := strings.Split(string(msg), " ") + value, exists := s.Get("info") + + if len(p) != 2 || !exists { + return + } + + info := value.(*GopherInfo) + info.X = p[0] + info.Y = p[1] + + m.BroadcastOthers([]byte("set "+info.ID+" "+info.X+" "+info.Y), s) + }) + + r.Run(":5000") }