You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
|
|
package main
import ( "net/http" "strconv" "strings" "sync"
"github.com/gin-gonic/gin" "gopkg.in/olahol/melody.v1" )
// GopherInfo contains information about the gopher on screen
type GopherInfo struct { ID, X, Y string }
func main() { router := gin.Default() mrouter := melody.New() gophers := make(map[*melody.Session]*GopherInfo) lock := new(sync.Mutex) counter := 0
router.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) })
mrouter.HandleConnect(func(s *melody.Session) { lock.Lock() for _, info := range gophers { s.Write([]byte("set " + info.ID + " " + info.X + " " + info.Y)) } gophers[s] = &GopherInfo{strconv.Itoa(counter), "0", "0"} s.Write([]byte("iam " + gophers[s].ID)) counter++ lock.Unlock() })
mrouter.HandleDisconnect(func(s *melody.Session) { lock.Lock() mrouter.BroadcastOthers([]byte("dis "+gophers[s].ID), s) delete(gophers, s) lock.Unlock() })
mrouter.HandleMessage(func(s *melody.Session, msg []byte) { p := strings.Split(string(msg), " ") lock.Lock() info := gophers[s] if len(p) == 2 { info.X = p[0] info.Y = p[1] mrouter.BroadcastOthers([]byte("set "+info.ID+" "+info.X+" "+info.Y), s) } lock.Unlock() })
router.Run(":5000") }
|