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.

32 lines
629 B

  1. package main
  2. import (
  3. "github.com/gin-gonic/gin"
  4. "github.com/olahol/melody"
  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("/channel/:name", func(c *gin.Context) {
  14. http.ServeFile(c.Writer, c.Request, "chan.html")
  15. })
  16. r.GET("/channel/:name/ws", func(c *gin.Context) {
  17. m.HandleRequest(c.Writer, c.Request)
  18. })
  19. m.HandleMessage(func(s *melody.Session, msg []byte) {
  20. m.BroadcastFilter(msg, func(q *melody.Session) bool {
  21. return q.Request.URL.Path == s.Request.URL.Path
  22. })
  23. })
  24. r.Run(":5000")
  25. }