websocket 增加多分组 fork https://github.com/olahol/melody
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.

26 lines
419 B

10 years ago
10 years ago
10 years ago
10 years ago
  1. package main
  2. import (
  3. "github.com/gin-gonic/gin"
  4. "gopkg.in/olahol/melody.v1"
  5. "net/http"
  6. )
  7. func main() {
  8. r := gin.Default()
  9. m := melody.New()
  10. r.GET("/", func(c *gin.Context) {
  11. http.ServeFile(c.Writer, c.Request, "index.html")
  12. })
  13. r.GET("/ws", func(c *gin.Context) {
  14. m.HandleRequest(c.Writer, c.Request)
  15. })
  16. m.HandleMessage(func(s *melody.Session, msg []byte) {
  17. m.Broadcast(msg)
  18. })
  19. r.Run(":5000")
  20. }