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
630 B
32 lines
630 B
package main
|
|
|
|
import (
|
|
"github.com/gin-gonic/gin"
|
|
"gopkg.in/olahol/melody.v1"
|
|
"net/http"
|
|
)
|
|
|
|
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")
|
|
}
|