mirror of
https://github.com/42wim/matterbridge.git
synced 2024-12-28 06:02:49 +01:00
.. | ||
.gitignore | ||
.travis.yml | ||
CHANGELOG.md | ||
config.go | ||
doc.go | ||
envelope.go | ||
hub.go | ||
LICENSE | ||
melody.go | ||
README.md | ||
session.go |
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: chat
Using Gin:
package main
import (
"github.com/gin-gonic/gin"
"gopkg.in/olahol/melody.v1"
"net/http"
)
func main() {
:= gin.Default()
r := melody.New()
m
.GET("/", func(c *gin.Context) {
r.ServeFile(c.Writer, c.Request, "index.html")
http})
.GET("/ws", func(c *gin.Context) {
r.HandleRequest(c.Writer, c.Request)
m})
.HandleMessage(func(s *melody.Session, msg []byte) {
m.Broadcast(msg)
m})
.Run(":5000")
r}
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() {
:= echo.New()
e := melody.New()
m
.Use(middleware.Logger())
e.Use(middleware.Recover())
e
.GET("/", func(c echo.Context) error {
e.ServeFile(c.Response().(*standard.Response).ResponseWriter, c.Request().(*standard.Request).Request, "index.html")
httpreturn nil
})
.GET("/ws", func(c echo.Context) error {
e.HandleRequest(c.Response().(*standard.Response).ResponseWriter, c.Request().(*standard.Request).Request)
mreturn nil
})
.HandleMessage(func(s *melody.Session, msg []byte) {
m.Broadcast(msg)
m})
.Run(standard.New(":5000"))
e}
Example: gophers
package main
import (
"github.com/gin-gonic/gin"
"gopkg.in/olahol/melody.v1"
"net/http"
"strconv"
"strings"
"sync"
)
type GopherInfo struct {
, X, Y string
ID}
func main() {
:= gin.Default()
router := melody.New()
mrouter := make(map[*melody.Session]*GopherInfo)
gophers := new(sync.Mutex)
lock := 0
counter
.GET("/", func(c *gin.Context) {
router.ServeFile(c.Writer, c.Request, "index.html")
http})
.GET("/ws", func(c *gin.Context) {
router.HandleRequest(c.Writer, c.Request)
mrouter})
.HandleConnect(func(s *melody.Session) {
mrouter.Lock()
lockfor _, info := range gophers {
.Write([]byte("set " + info.ID + " " + info.X + " " + info.Y))
s}
[s] = &GopherInfo{strconv.Itoa(counter), "0", "0"}
gophers.Write([]byte("iam " + gophers[s].ID))
s+= 1
counter .Unlock()
lock})
.HandleDisconnect(func(s *melody.Session) {
mrouter.Lock()
lock.BroadcastOthers([]byte("dis "+gophers[s].ID), s)
mrouterdelete(gophers, s)
.Unlock()
lock})
.HandleMessage(func(s *melody.Session, msg []byte) {
mrouter:= strings.Split(string(msg), " ")
p .Lock()
lock:= gophers[s]
info if len(p) == 2 {
.X = p[0]
info.Y = p[1]
info.BroadcastOthers([]byte("set "+info.ID+" "+info.X+" "+info.Y), s)
mrouter}
.Unlock()
lock})
.Run(":5000")
router}
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:
:= melody.New()
m .Upgrader.CheckOrigin = func(r *http.Request) bool { return true } m