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.

181 lines
4.4 KiB

10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
9 years ago
10 years ago
9 years ago
10 years ago
10 years ago
9 years ago
10 years ago
9 years ago
10 years ago
9 years ago
10 years ago
9 years ago
9 years ago
  1. # melody
  2. [![Build Status](https://travis-ci.org/olahol/melody.svg)](https://travis-ci.org/olahol/melody)
  3. [![Coverage Status](https://img.shields.io/coveralls/olahol/melody.svg?style=flat)](https://coveralls.io/r/olahol/melody)
  4. [![GoDoc](https://godoc.org/github.com/olahol/melody?status.svg)](https://godoc.org/github.com/olahol/melody)
  5. > :notes: Minimalist websocket framework for Go.
  6. Melody is websocket framework based on [github.com/gorilla/websocket](https://github.com/gorilla/websocket)
  7. that abstracts away the tedious parts of handling websockets. It gets out of
  8. your way so you can write real-time apps. Features include:
  9. * [x] Clear and easy interface similar to `net/http` or Gin.
  10. * [x] A simple way to broadcast to all or selected connected sessions.
  11. * [x] Message buffers making concurrent writing safe.
  12. * [x] Automatic handling of ping/pong and session timeouts.
  13. ## Install
  14. ```bash
  15. go get github.com/olahol/melody
  16. ```
  17. ## [Example: chat](https://github.com/olahol/melody/tree/master/examples/chat)
  18. [![Chat](https://cdn.rawgit.com/olahol/melody/master/examples/chat/demo.gif "Demo")](https://github.com/olahol/melody/tree/master/examples/chat)
  19. Using [Gin](https://github.com/gin-gonic/gin):
  20. ```go
  21. package main
  22. import (
  23. "github.com/gin-gonic/gin"
  24. "github.com/olahol/melody"
  25. "net/http"
  26. )
  27. func main() {
  28. r := gin.Default()
  29. m := melody.New()
  30. r.GET("/", func(c *gin.Context) {
  31. http.ServeFile(c.Writer, c.Request, "index.html")
  32. })
  33. r.GET("/ws", func(c *gin.Context) {
  34. m.HandleRequest(c.Writer, c.Request)
  35. })
  36. m.HandleMessage(func(s *melody.Session, msg []byte) {
  37. m.Broadcast(msg)
  38. })
  39. r.Run(":5000")
  40. }
  41. ```
  42. Using [Echo](https://github.com/labstack/echo):
  43. ```go
  44. package main
  45. import (
  46. "github.com/labstack/echo"
  47. "github.com/labstack/echo/engine/standard"
  48. "github.com/labstack/echo/middleware"
  49. "github.com/olahol/melody"
  50. "net/http"
  51. )
  52. func main() {
  53. e := echo.New()
  54. m := melody.New()
  55. e.Use(middleware.Logger())
  56. e.Use(middleware.Recover())
  57. e.GET("/", func(c echo.Context) error {
  58. http.ServeFile(c.Response().(*standard.Response).ResponseWriter, c.Request().(*standard.Request).Request, "index.html")
  59. return nil
  60. })
  61. e.GET("/ws", func(c echo.Context) error {
  62. m.HandleRequest(c.Response().(*standard.Response).ResponseWriter, c.Request().(*standard.Request).Request)
  63. return nil
  64. })
  65. m.HandleMessage(func(s *melody.Session, msg []byte) {
  66. m.Broadcast(msg)
  67. })
  68. e.Run(standard.New(":5000"))
  69. }
  70. ```
  71. ## [Example: gophers](https://github.com/olahol/melody/tree/master/examples/gophers)
  72. [![Gophers](https://cdn.rawgit.com/olahol/melody/master/examples/gophers/demo.gif "Demo")](https://github.com/olahol/melody/tree/master/examples/gophers)
  73. ```go
  74. package main
  75. import (
  76. "github.com/gin-gonic/gin"
  77. "github.com/olahol/melody"
  78. "net/http"
  79. "strconv"
  80. "strings"
  81. "sync"
  82. )
  83. type GopherInfo struct {
  84. ID, X, Y string
  85. }
  86. func main() {
  87. router := gin.Default()
  88. mrouter := melody.New()
  89. gophers := make(map[*melody.Session]*GopherInfo)
  90. lock := new(sync.Mutex)
  91. counter := 0
  92. router.GET("/", func(c *gin.Context) {
  93. http.ServeFile(c.Writer, c.Request, "index.html")
  94. })
  95. router.GET("/ws", func(c *gin.Context) {
  96. mrouter.HandleRequest(c.Writer, c.Request)
  97. })
  98. mrouter.HandleConnect(func(s *melody.Session) {
  99. lock.Lock()
  100. for _, info := range gophers {
  101. s.Write([]byte("set " + info.ID + " " + info.X + " " + info.Y))
  102. }
  103. gophers[s] = &GopherInfo{strconv.Itoa(counter), "0", "0"}
  104. s.Write([]byte("iam " + gophers[s].ID))
  105. counter += 1
  106. lock.Unlock()
  107. })
  108. mrouter.HandleDisconnect(func(s *melody.Session) {
  109. lock.Lock()
  110. mrouter.BroadcastOthers([]byte("dis "+gophers[s].ID), s)
  111. delete(gophers, s)
  112. lock.Unlock()
  113. })
  114. mrouter.HandleMessage(func(s *melody.Session, msg []byte) {
  115. p := strings.Split(string(msg), " ")
  116. lock.Lock()
  117. info := gophers[s]
  118. if len(p) == 2 {
  119. info.X = p[0]
  120. info.Y = p[1]
  121. mrouter.BroadcastOthers([]byte("set "+info.ID+" "+info.X+" "+info.Y), s)
  122. }
  123. lock.Unlock()
  124. })
  125. router.Run(":5000")
  126. }
  127. ```
  128. ### [More examples](https://github.com/olahol/melody/tree/master/examples)
  129. ## [Documentation](https://godoc.org/github.com/olahol/melody)
  130. ## Contributors
  131. * Ola Holmström (@olahol)
  132. * Shogo Iwano (@shiwano)
  133. * Matt Caldwell (@mattcaldwell)
  134. ## FAQ
  135. If you are getting a `403` when trying to connect to your websocket you can [change allow all origin hosts](http://godoc.org/github.com/gorilla/websocket#hdr-Origin_Considerations):
  136. ```go
  137. m := melody.New()
  138. m.Upgrader.CheckOrigin = func(r *http.Request) bool { return true }
  139. ```