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.

68 lines
2.0 KiB

10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 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](https://github.com/olahol/melody/tree/master/examples)
  18. [Multi channel chat server](https://github.com/olahol/melody/tree/master/examples/multichat),
  19. error handling left as en exercise for the developer.
  20. [![Chat demo](https://cdn.rawgit.com/olahol/melody/master/examples/chat/demo.gif "Demo")](https://github.com/olahol/melody/tree/master/examples/multichat)
  21. ```go
  22. package main
  23. import (
  24. "github.com/olahol/melody"
  25. "github.com/gin-gonic/gin"
  26. "net/http"
  27. )
  28. func main() {
  29. r := gin.Default()
  30. m := melody.New()
  31. r.GET("/", func(c *gin.Context) {
  32. http.ServeFile(c.Writer, c.Request, "index.html")
  33. })
  34. r.GET("/channel/:name", func(c *gin.Context) {
  35. http.ServeFile(c.Writer, c.Request, "chan.html")
  36. })
  37. r.GET("/channel/:name/ws", func(c *gin.Context) {
  38. m.HandleRequest(c.Writer, c.Request)
  39. })
  40. m.HandleMessage(func(s *melody.Session, msg []byte) {
  41. m.BroadcastFilter(msg, func(q *melody.Session) bool {
  42. return q.Request.URL.Path == s.Request.URL.Path
  43. })
  44. })
  45. r.Run(":5000")
  46. }
  47. ```
  48. ### [More examples](https://github.com/olahol/melody/tree/master/examples)
  49. ## [Documentation](https://godoc.org/github.com/olahol/melody)