Update gophers example.

This commit is contained in:
Ola
2022-10-18 13:28:24 +02:00
parent cee8589c93
commit c572272f17
3 changed files with 78 additions and 49 deletions
-9
View File
@@ -1,9 +0,0 @@
language: go
sudo: required
go:
- 1.x
- master
install:
- go get github.com/gorilla/websocket
script:
- go test
+39 -20
View File
@@ -114,22 +114,29 @@ type GopherInfo struct {
} }
func main() { func main() {
router := gin.Default() r := gin.Default()
mrouter := melody.New() m := melody.New()
router.GET("/", func(c *gin.Context) { r.GET("/", func(c *gin.Context) {
http.ServeFile(c.Writer, c.Request, "index.html") http.ServeFile(c.Writer, c.Request, "index.html")
}) })
router.GET("/ws", func(c *gin.Context) { r.GET("/ws", func(c *gin.Context) {
mrouter.HandleRequest(c.Writer, c.Request) m.HandleRequest(c.Writer, c.Request)
}) })
mrouter.HandleConnect(func(s *melody.Session) { m.HandleConnect(func(s *melody.Session) {
ss, _ := mrouter.Sessions() ss, _ := m.Sessions()
for _, o := range ss { 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)) s.Write([]byte("set " + info.ID + " " + info.X + " " + info.Y))
} }
@@ -139,22 +146,34 @@ func main() {
s.Write([]byte("iam " + id)) s.Write([]byte("iam " + id))
}) })
mrouter.HandleDisconnect(func(s *melody.Session) { m.HandleDisconnect(func(s *melody.Session) {
info := s.MustGet("info").(*GopherInfo) value, exists := s.Get("info")
mrouter.BroadcastOthers([]byte("dis "+info.ID), s)
})
mrouter.HandleMessage(func(s *melody.Session, msg []byte) { if !exists {
p := strings.Split(string(msg), " ") return
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)
} }
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")
} }
``` ```
+39 -20
View File
@@ -14,22 +14,29 @@ type GopherInfo struct {
} }
func main() { func main() {
router := gin.Default() r := gin.Default()
mrouter := melody.New() m := melody.New()
router.GET("/", func(c *gin.Context) { r.GET("/", func(c *gin.Context) {
http.ServeFile(c.Writer, c.Request, "index.html") http.ServeFile(c.Writer, c.Request, "index.html")
}) })
router.GET("/ws", func(c *gin.Context) { r.GET("/ws", func(c *gin.Context) {
mrouter.HandleRequest(c.Writer, c.Request) m.HandleRequest(c.Writer, c.Request)
}) })
mrouter.HandleConnect(func(s *melody.Session) { m.HandleConnect(func(s *melody.Session) {
ss, _ := mrouter.Sessions() ss, _ := m.Sessions()
for _, o := range ss { 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)) s.Write([]byte("set " + info.ID + " " + info.X + " " + info.Y))
} }
@@ -39,20 +46,32 @@ func main() {
s.Write([]byte("iam " + id)) s.Write([]byte("iam " + id))
}) })
mrouter.HandleDisconnect(func(s *melody.Session) { m.HandleDisconnect(func(s *melody.Session) {
info := s.MustGet("info").(*GopherInfo) value, exists := s.Get("info")
mrouter.BroadcastOthers([]byte("dis "+info.ID), s)
})
mrouter.HandleMessage(func(s *melody.Session, msg []byte) { if !exists {
p := strings.Split(string(msg), " ") return
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)
} }
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")
} }