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.

58 lines
1.5 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
  1. # melody
  2. [![GoDoc](https://godoc.org/github.com/olahol/melody?status.svg)](https://godoc.org/github.com/olahol/melody)
  3. [![Build Status](https://travis-ci.org/olahol/melody.svg)](https://travis-ci.org/olahol/melody)
  4. > :notes: Simple websocket framework for Go
  5. Melody is websocket framework based on [github.com/gorilla/websocket](https://github.com/gorilla/websocket)
  6. that abstracts away the more tedious parts of handling websockets. Features include:
  7. * [x] Timeouts for write and read.
  8. * [x] Built-in ping/pong handling.
  9. * [x] Message buffer for connections making concurrent writing easy.
  10. * [x] Simple broadcasting to all or selected sessions.
  11. ## Install
  12. ```bash
  13. go get github.com/olahol/melody
  14. ```
  15. ## [Example](https://github.com/olahol/melody/tree/master/examples)
  16. [Simple broadcasting chat server](https://github.com/olahol/melody/tree/master/examples/chat),
  17. error handling left as en exercise for the developer.
  18. [![Chat demo](https://cdn.rawgit.com/olahol/melody/master/examples/chat/demo.gif "Demo")](https://github.com/olahol/melody/tree/master/examples/chat)
  19. ```go
  20. package main
  21. import (
  22. "github.com/olahol/melody"
  23. "github.com/gin-gonic/gin"
  24. "net/http"
  25. )
  26. func main() {
  27. r := gin.Default()
  28. m := melody.New()
  29. r.GET("/", func(c *gin.Context) {
  30. http.ServeFile(c.Writer, c.Request, "index.html")
  31. })
  32. r.GET("/ws", func(c *gin.Context) {
  33. m.HandleRequest(c.Writer, c.Request)
  34. })
  35. m.HandleMessage(func(s *melody.Session, msg []byte) {
  36. m.Broadcast(msg)
  37. })
  38. r.Run(":5000")
  39. }
  40. ```
  41. ### [Documentation](https://godoc.org/github.com/olahol/melody)