func main() { file := "file.txt" r := gin.Default() m := melody.New() w, _ := fsnotify.NewWatcher() r.GET("/", func(c *gin.Context) { http.ServeFile(c.Writer, c.Request, "index.html") }) r.GET("/ws", func(c *gin.Context) { m.HandleRequest(c.Writer, c.Request) }) m.HandleConnect(func(s *melody.Session) { content, _ := ioutil.ReadFile(file) s.Write(content) }) go func() { for { ev := <-w.Events if ev.Op == fsnotify.Write { content, _ := ioutil.ReadFile(ev.Name) m.Broadcast(content) } } }() w.Add(file) r.Run(":5000") }
func NewMatch(id string, capacity int, questions []models.Question) *Match { if capacity < MIN_CAPACITY { capacity = MIN_CAPACITY } m := &Match{ ID: id, Capacity: capacity, Hub: melody.New(), Questions: questions, Sessions: make(map[string]*melody.Session), Scores: make(Scores), playerConnect: make(chan playerSession), playerGuess: make(chan PlayerGuess), playerDisconnect: make(chan string), onRoundComplete: make(chan Round), } m.Hub.Upgrader.CheckOrigin = func(r *http.Request) bool { return true } m.Hub.HandleMessage(m.handlePlayerMessage) m.Hub.HandleConnect(m.handlePlayerConnect) m.Hub.HandleDisconnect(func(s *melody.Session) { playerID := s.Request.URL.Query().Get("player") m.playerDisconnect <- playerID }) go m.run() return m }
func main() { flag.Parse() initOauth() //clients = newClients() m := martini.Classic() mel := melody.New() mel.HandleConnect(func(s *melody.Session) { doPeriodicalStuff(mel) }) gracefulShutdown := NewGracefulShutdown(10 * time.Second) gracefulShutdown.RunOnShutDown(mel.Close) m.Use(gracefulShutdown.Handler) //limiter := &ConnectionLimit{limit: 2} //m.Use(limiter.Handler) m.Get("/", func() string { return "Hello world!" }) m.Get("/control/:action", func(p martini.Params) { if val, err := json.Marshal(&Message{p["action"], nil}); err != nil { mel.Broadcast(val) } //clients.messageOtherClients(&Message{p["action"], nil}) }) m.Get("/getsmhi", getSmhi) m.Get("/api/sun", func() string { return getSun("rise") + getSun("set") }) //m.Get("/websocket", sockets.JSON(Message{}), websocketRoute) m.Get("/websocket", func(w http.ResponseWriter, r *http.Request) { doPeriodicalStuff(mel) mel.HandleRequest(w, r) }) //OAUTH2 m.Get("/oauthsetup", handleSetupOauth) m.Get("/oauthredirect", handleOauthRedirect) m.Get("/cal", func() interface{} { return getEvents(6) }) initPeriodicalPush(mel) go func() { m.RunOnAddr(":" + *port) }() err := gracefulShutdown.WaitForSignal(syscall.SIGTERM, syscall.SIGINT) if err != nil { log.Println(err) } }
func New() *Server { ws := melody.New() routes := gin.Default() games := make([]*Game, 0) return &Server{ Ws: ws, Routes: routes, Games: games, } }
func main() { r := gin.Default() m := melody.New() r.GET("/", func(c *gin.Context) { http.ServeFile(c.Writer, c.Request, "index.html") }) r.GET("/ws", func(c *gin.Context) { m.HandleRequest(c.Writer, c.Request) }) m.HandleMessage(func(s *melody.Session, msg []byte) { m.Broadcast(msg) }) r.Run(":5000") }
func main() { r := gin.Default() m := melody.New() store := make(map[string]Match) r.GET("/create/:id/:p1/:p2", func(c *gin.Context) { id := c.Param("id") p1 := c.Param("p1") p2 := c.Param("p2") if uuid.Parse(id) == nil { c.JSON(400, gin.H{ "status": "Bad request", }) return } store[id] = Match{p1, p2, S_INIT} c.JSON(200, gin.H{ "status": "OK", }) }) // start a match r.GET("/ws/:id", func(c *gin.Context) { m.HandleRequest(c.Writer, c.Request) }) m.HandleMessage(func(s *melody.Session, msg []byte) { m.BroadcastFilter(msg, func(q *melody.Session) bool { return q.Request.URL.Path == s.Request.URL.Path }) }) s := &http.Server{ Addr: ":6000", Handler: r, ReadTimeout: 60 * time.Second, WriteTimeout: 60 * time.Second, } s.ListenAndServe() }
func main() { r := gin.Default() m := melody.New() m.Upgrader.CheckOrigin = func(r *http.Request) bool { return true } r.POST("/", func(c *gin.Context) { c.String(200, "received") d, err := ioutil.ReadAll(c.Request.Body) if err != nil { log.Fatal(err) } m.Broadcast(d) }) r.GET("/", func(c *gin.Context) { m.HandleRequest(c.Writer, c.Request) }) r.Run(":5000") }
func main() { r := gin.Default() m := melody.New() r.GET("/", func(c *gin.Context) { http.ServeFile(c.Writer, c.Request, "index.html") }) r.GET("/channel/:name", func(c *gin.Context) { http.ServeFile(c.Writer, c.Request, "chan.html") }) r.GET("/channel/:name/ws", func(c *gin.Context) { m.HandleRequest(c.Writer, c.Request) }) m.HandleMessage(func(s *melody.Session, msg []byte) { m.BroadcastFilter(msg, func(q *melody.Session) bool { return q.Request.URL.Path == s.Request.URL.Path }) }) r.Run(":5000") }
func main() { r := gin.New() m := melody.New() g := &Game{} r.Static("/jsnes", "./jsnes") r.Static("/public", "./public") r.Static("/roms/", "./roms") r.GET("/", func(c *gin.Context) { http.ServeFile(c.Writer, c.Request, "index.html") }) r.GET("/ws", func(c *gin.Context) { m.HandleRequest(c.Writer, c.Request) }) r.GET("/romlist", func(c *gin.Context) { files, _ := filepath.Glob("./roms/*.nes") c.JSON(200, gin.H{"roms": files}) }) m.HandleConnect(func(s *melody.Session) { s.Write(g.Status()) }) m.HandleMessage(func(s *melody.Session, msg []byte) { cmd := &Command{} err := json.Unmarshal(msg, cmd) if err != nil { log.Println(err) return } switch cmd.Cmd { case "screen": if g.Screen == nil { g.Lock.Lock() g.Screen = s s.Write(NewCommandJSON("whoami", "screen")) m.BroadcastOthers(NewCommandJSON("join", "screen"), s) g.Lock.Unlock() return } s.Write(NewCommandJSON("whoami", "notscreen")) case "player": if g.Player1 == nil { g.Lock.Lock() g.Player1 = s s.Write(NewCommandJSON("whoami", "player1")) m.BroadcastOthers(NewCommandJSON("join", "player1"), s) g.Lock.Unlock() return } if g.Player2 == nil { g.Lock.Lock() g.Player2 = s s.Write(NewCommandJSON("whoami", "player2")) m.BroadcastOthers(NewCommandJSON("join", "player2"), s) g.Lock.Unlock() return } s.Write(NewCommandJSON("whoami", "notplayer")) case "keyup", "keydown": if g.Screen != nil && (s == g.Player1 || s == g.Player2) { var player string if s == g.Player1 { player = "player1" } if s == g.Player2 { player = "player2" } g.Screen.Write(NewCommandJSON(player, cmd.Cmd+" "+cmd.Data)) } default: log.Printf("unknown command %s", cmd.Cmd) } }) m.HandleDisconnect(func(s *melody.Session) { g.Lock.Lock() if s == g.Screen { g.Screen = nil m.BroadcastOthers(NewCommandJSON("part", "screen"), s) } if s == g.Player1 { g.Player1 = nil m.BroadcastOthers(NewCommandJSON("part", "player1"), s) } if s == g.Player2 { g.Player2 = nil m.BroadcastOthers(NewCommandJSON("part", "player2"), s) } g.Lock.Unlock() }) r.Run(":5000") }
func main() { r := gin.New() m := melody.New() size := 65536 m.Upgrader = &websocket.Upgrader{ ReadBufferSize: size, WriteBufferSize: size, } m.Config.MaxMessageSize = int64(size) m.Config.MessageBufferSize = 2048 r.Static("/jsnes", "./jsnes") r.GET("/", func(c *gin.Context) { http.ServeFile(c.Writer, c.Request, "index.html") }) r.GET("/gamelist", func(c *gin.Context) { files, _ := filepath.Glob("*.nes") c.JSON(200, gin.H{"games": files}) }) r.GET("/games?name=:name", func(c *gin.Context) { name := c.Params.ByName("name") http.ServeFile(c.Writer, c.Request, name) }) r.GET("/ws", func(c *gin.Context) { m.HandleRequest(c.Writer, c.Request) }) var mutex sync.Mutex pairs := make(map[*melody.Session]*melody.Session) m.HandleConnect(func(s *melody.Session) { mutex.Lock() var partner *melody.Session for player1, player2 := range pairs { if player2 == nil { partner = player1 pairs[partner] = s partner.Write([]byte("join 1")) break } } pairs[s] = partner if partner != nil { s.Write([]byte("join 2")) } mutex.Unlock() }) m.HandleMessage(func(s *melody.Session, msg []byte) { partner := pairs[s] if partner != nil { partner.Write(msg) } }) m.HandleDisconnect(func(s *melody.Session) { mutex.Lock() partner := pairs[s] if partner != nil { pairs[partner] = nil partner.Write([]byte("part")) } delete(pairs, s) mutex.Unlock() }) r.Run(":5000") }