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.

51 lines
1.1 KiB

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. ## Install
  6. ```bash
  7. go get github.com/olahol/melody
  8. ```
  9. ## [Example](https://github.com/olahol/melody/tree/master/examples)
  10. [Simple broadcasting chat server](https://github.com/olahol/melody/tree/master/examples/chat),
  11. error handling left as en exercise for the developer.
  12. ```go
  13. package main
  14. import (
  15. "github.com/olahol/melody"
  16. "github.com/gin-gonic/gin"
  17. "net/http"
  18. )
  19. func main() {
  20. r := gin.Default()
  21. m := melody.New()
  22. r.GET("/", func(c *gin.Context) {
  23. http.ServeFile(c.Writer, c.Request, "index.html")
  24. })
  25. r.GET("/ws", func(c *gin.Context) {
  26. m.HandleRequest(c.Writer, c.Request)
  27. })
  28. m.HandleMessage(func(s *melody.Session, msg []byte) {
  29. m.Broadcast(msg)
  30. })
  31. r.Run(":5000")
  32. }
  33. ```
  34. [![Chat demo](https://cdn.rawgit.com/olahol/melody/master/examples/chat/demo.gif "Demo")](https://github.com/olahol/melody/tree/master/examples/chat)
  35. ## [Api](https://godoc.org/github.com/olahol/melody)