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.
tooooommy
1333d0784c
|
5 years ago | |
---|---|---|
examples | 5 years ago | |
.gitignore | 5 years ago | |
.travis.yml | 8 years ago | |
CHANGELOG.md | 8 years ago | |
LICENSE | 10 years ago | |
README.md | 5 years ago | |
channel.go | 5 years ago | |
config.go | 9 years ago | |
doc.go | 10 years ago | |
envelope.go | 5 years ago | |
hub.go | 5 years ago | |
melody.go | 5 years ago | |
melody_test.go | 8 years ago | |
session.go | 5 years ago |
README.md
melody
🎶 Minimalist websocket framework for Go.
Melody is websocket framework based on github.com/gorilla/websocket that abstracts away the tedious parts of handling websockets. It gets out of your way so you can write real-time apps. Features include:
- Clear and easy interface similar to
net/http
or Gin. - A simple way to broadcast to all or selected connected sessions.
- Message buffers making concurrent writing safe.
- Automatic handling of ping/pong and session timeouts.
- Store data on sessions.
Install
go get gopkg.in/olahol/melody.v1
Example: sub/pub
package main
import (
"log"
"melody"
"github.com/gin-gonic/gin"
)
func main() {
m := melody.New()
router := gin.Default()
router.GET("/chat", func(context *gin.Context) {
if err := m.HandleRequest(context.Writer, context.Request); err != nil {
log.Println(err)
}
})
m.HandleConnect(func(session *melody.Session) {
ch := session.Request.URL.Query().Get("channel")
if err := session.Subscribe(ch); err != nil {
log.Println(err)
}
})
m.HandleMessage(func(session *melody.Session, msg []byte) {
if err := session.Publish(msg); err != nil {
log.Println(err)
}
})
m.HandleSentMessage(func(session *melody.Session, bytes []byte) {
log.Printf("%+v", session.Channel().Online())
log.Printf("%+v", string(bytes))
})
router.Run(":8080")
}
Example: chat
Using Gin:
package main
import (
"github.com/gin-gonic/gin"
"gopkg.in/olahol/melody.v1"
"net/http"
)
func main() {
r := gin.Default()
m := melody.New()
r.GET("/", func(c *gin.Context) {
http.ServeFile(c.Writer, c.Request, "index.html")
})
r.GET("/ws", func(c *gin.Context) {
m.HandleRequest(c.Writer, c.Request)
})
m.HandleMessage(func(s *melody.Session, msg []byte) {
m.Broadcast(msg)
})
r.Run(":5000")
}
Using Echo:
package main
import (
"github.com/labstack/echo"
"github.com/labstack/echo/engine/standard"
"github.com/labstack/echo/middleware"
"gopkg.in/olahol/melody.v1"
"net/http"
)
func main() {
e := echo.New()
m := melody.New()
e.Use(middleware.Logger())
e.Use(middleware.Recover())
e.GET("/", func(c echo.Context) error {
http.ServeFile(c.Response().(*standard.Response).ResponseWriter, c.Request().(*standard.Request).Request, "index.html")
return nil
})
e.GET("/ws", func(c echo.Context) error {
m.HandleRequest(c.Response().(*standard.Response).ResponseWriter, c.Request().(*standard.Request).Request)
return nil
})
m.HandleMessage(func(s *melody.Session, msg []byte) {
m.Broadcast(msg)
})
e.Run(standard.New(":5000"))
}
Example: gophers
package main
import (
"github.com/gin-gonic/gin"
"gopkg.in/olahol/melody.v1"
"net/http"
"strconv"
"strings"
"sync"
)
type GopherInfo struct {
ID, X, Y string
}
func main() {
router := gin.Default()
mrouter := melody.New()
gophers := make(map[*melody.Session]*GopherInfo)
lock := new(sync.Mutex)
counter := 0
router.GET("/", func(c *gin.Context) {
http.ServeFile(c.Writer, c.Request, "index.html")
})
router.GET("/ws", func(c *gin.Context) {
mrouter.HandleRequest(c.Writer, c.Request)
})
mrouter.HandleConnect(func(s *melody.Session) {
lock.Lock()
for _, info := range gophers {
s.Write([]byte("set " + info.ID + " " + info.X + " " + info.Y))
}
gophers[s] = &GopherInfo{strconv.Itoa(counter), "0", "0"}
s.Write([]byte("iam " + gophers[s].ID))
counter += 1
lock.Unlock()
})
mrouter.HandleDisconnect(func(s *melody.Session) {
lock.Lock()
mrouter.BroadcastOthers([]byte("dis "+gophers[s].ID), s)
delete(gophers, s)
lock.Unlock()
})
mrouter.HandleMessage(func(s *melody.Session, msg []byte) {
p := strings.Split(string(msg), " ")
lock.Lock()
info := gophers[s]
if len(p) == 2 {
info.X = p[0]
info.Y = p[1]
mrouter.BroadcastOthers([]byte("set "+info.ID+" "+info.X+" "+info.Y), s)
}
lock.Unlock()
})
router.Run(":5000")
}
More examples
Documentation
Contributors
- Ola Holmström (@olahol)
- Shogo Iwano (@shiwano)
- Matt Caldwell (@mattcaldwell)
- Heikki Uljas (@huljas)
- Robbie Trencheny (@robbiet480)
- yangjinecho (@yangjinecho)
FAQ
If you are getting a 403
when trying to connect to your websocket you can change allow all origin hosts:
m := melody.New()
m.Upgrader.CheckOrigin = func(r *http.Request) bool { return true }