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.

33 lines
749 B

  1. package main
  2. import (
  3. "github.com/labstack/echo"
  4. "github.com/labstack/echo/engine/standard"
  5. "github.com/labstack/echo/middleware"
  6. "gopkg.in/olahol/melody.v1"
  7. "net/http"
  8. )
  9. func main() {
  10. e := echo.New()
  11. m := melody.New()
  12. e.Use(middleware.Logger())
  13. e.Use(middleware.Recover())
  14. e.GET("/", func(c echo.Context) error {
  15. http.ServeFile(c.Response().(*standard.Response).ResponseWriter, c.Request().(*standard.Request).Request, "index.html")
  16. return nil
  17. })
  18. e.GET("/ws", func(c echo.Context) error {
  19. m.HandleRequest(c.Response().(*standard.Response).ResponseWriter, c.Request().(*standard.Request).Request)
  20. return nil
  21. })
  22. m.HandleMessage(func(s *melody.Session, msg []byte) {
  23. m.Broadcast(msg)
  24. })
  25. e.Run(standard.New(":5000"))
  26. }