Also update to latest melody upstreampull/1991/head^2
parent
356ada872c
commit
89e2dbac15
@ -1,3 +1,11 @@
|
||||
## 2022-09-12 (v1.1.0)
|
||||
|
||||
* Create Go module.
|
||||
* Update examples.
|
||||
* Fix concurrent panic (PR-65).
|
||||
* Add `Sessions` to get all sessions (PR-53).
|
||||
* Add `LocalAddr` and `RemoteAddr` (PR-55).
|
||||
|
||||
## 2017-05-18
|
||||
|
||||
* Fix `HandleSentMessageBinary`.
|
0
vendor/gopkg.in/olahol/melody.v1/LICENSE → vendor/github.com/olahol/melody/LICENSE
generated
vendored
0
vendor/gopkg.in/olahol/melody.v1/LICENSE → vendor/github.com/olahol/melody/LICENSE
generated
vendored
@ -0,0 +1,157 @@
|
||||
# melody
|
||||
|
||||
![Build Status](https://github.com/olahol/melody/actions/workflows/test.yml/badge.svg)
|
||||
[![Codecov](https://img.shields.io/codecov/c/github/olahol/melody)](https://app.codecov.io/github/olahol/melody)
|
||||
[![Go Report Card](https://goreportcard.com/badge/github.com/olahol/melody)](https://goreportcard.com/report/github.com/olahol/melody)
|
||||
[![GoDoc](https://godoc.org/github.com/olahol/melody?status.svg)](https://godoc.org/github.com/olahol/melody)
|
||||
|
||||
> :notes: Minimalist websocket framework for Go.
|
||||
|
||||
Melody is websocket framework based on [github.com/gorilla/websocket](https://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:
|
||||
|
||||
* [x] Clear and easy interface similar to `net/http` or Gin.
|
||||
* [x] A simple way to broadcast to all or selected connected sessions.
|
||||
* [x] Message buffers making concurrent writing safe.
|
||||
* [x] Automatic handling of sending ping/pong heartbeats that timeout broken sessions.
|
||||
* [x] Store data on sessions.
|
||||
|
||||
## Install
|
||||
|
||||
```bash
|
||||
go get github.com/olahol/melody
|
||||
```
|
||||
|
||||
## [Example: chat](https://github.com/olahol/melody/tree/master/examples/chat)
|
||||
|
||||
[![Chat](https://cdn.rawgit.com/olahol/melody/master/examples/chat/demo.gif "Demo")](https://github.com/olahol/melody/tree/master/examples/chat)
|
||||
|
||||
```go
|
||||
package main
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/olahol/melody"
|
||||
)
|
||||
|
||||
func main() {
|
||||
m := melody.New()
|
||||
|
||||
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
|
||||
http.ServeFile(w, r, "index.html")
|
||||
})
|
||||
|
||||
http.HandleFunc("/ws", func(w http.ResponseWriter, r *http.Request) {
|
||||
m.HandleRequest(w, r)
|
||||
})
|
||||
|
||||
m.HandleMessage(func(s *melody.Session, msg []byte) {
|
||||
m.Broadcast(msg)
|
||||
})
|
||||
|
||||
http.ListenAndServe(":5000", nil)
|
||||
}
|
||||
```
|
||||
|
||||
## [Example: gophers](https://github.com/olahol/melody/tree/master/examples/gophers)
|
||||
|
||||
[![Gophers](https://cdn.rawgit.com/olahol/melody/master/examples/gophers/demo.gif "Demo")](https://github.com/olahol/melody/tree/master/examples/gophers)
|
||||
|
||||
```go
|
||||
package main
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/olahol/melody"
|
||||
)
|
||||
|
||||
type GopherInfo struct {
|
||||
ID, X, Y string
|
||||
}
|
||||
|
||||
func main() {
|
||||
m := melody.New()
|
||||
|
||||
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
|
||||
http.ServeFile(w, r, "index.html")
|
||||
})
|
||||
|
||||
http.HandleFunc("/ws", func(w http.ResponseWriter, r *http.Request) {
|
||||
m.HandleRequest(w, r)
|
||||
})
|
||||
|
||||
m.HandleConnect(func(s *melody.Session) {
|
||||
ss, _ := m.Sessions()
|
||||
|
||||
for _, o := range ss {
|
||||
value, exists := o.Get("info")
|
||||
|
||||
if !exists {
|
||||
continue
|
||||
}
|
||||
|
||||
info := value.(*GopherInfo)
|
||||
|
||||
s.Write([]byte("set " + info.ID + " " + info.X + " " + info.Y))
|
||||
}
|
||||
|
||||
id := uuid.NewString()
|
||||
s.Set("info", &GopherInfo{id, "0", "0"})
|
||||
|
||||
s.Write([]byte("iam " + id))
|
||||
})
|
||||
|
||||
m.HandleDisconnect(func(s *melody.Session) {
|
||||
value, exists := s.Get("info")
|
||||
|
||||
if !exists {
|
||||
return
|
||||
}
|
||||
|
||||
info := value.(*GopherInfo)
|
||||
|
||||
m.BroadcastOthers([]byte("dis "+info.ID), s)
|
||||
})
|
||||
|
||||
m.HandleMessage(func(s *melody.Session, msg []byte) {
|
||||
p := strings.Split(string(msg), " ")
|
||||
value, exists := s.Get("info")
|
||||
|
||||
if len(p) != 2 || !exists {
|
||||
return
|
||||
}
|
||||
|
||||
info := value.(*GopherInfo)
|
||||
info.X = p[0]
|
||||
info.Y = p[1]
|
||||
|
||||
m.BroadcastOthers([]byte("set "+info.ID+" "+info.X+" "+info.Y), s)
|
||||
})
|
||||
|
||||
http.ListenAndServe(":5000", nil)
|
||||
}
|
||||
```
|
||||
|
||||
### [More examples](https://github.com/olahol/melody/tree/master/examples)
|
||||
|
||||
## [Documentation](https://godoc.org/github.com/olahol/melody)
|
||||
|
||||
## Contributors
|
||||
|
||||
<a href="https://github.com/olahol/melody/graphs/contributors">
|
||||
<img src="https://contrib.rocks/image?repo=olahol/melody" />
|
||||
</a>
|
||||
|
||||
## FAQ
|
||||
|
||||
If you are getting a `403` when trying to connect to your websocket you can [change allow all origin hosts](http://godoc.org/github.com/gorilla/websocket#hdr-Origin_Considerations):
|
||||
|
||||
```go
|
||||
m := melody.New()
|
||||
m.Upgrader.CheckOrigin = func(r *http.Request) bool { return true }
|
||||
```
|
8
vendor/gopkg.in/olahol/melody.v1/doc.go → vendor/github.com/olahol/melody/doc.go
generated
vendored
8
vendor/gopkg.in/olahol/melody.v1/doc.go → vendor/github.com/olahol/melody/doc.go
generated
vendored
@ -0,0 +1,10 @@
|
||||
package melody
|
||||
|
||||
import "errors"
|
||||
|
||||
var (
|
||||
ErrClosed = errors.New("melody instance is closed")
|
||||
ErrSessionClosed = errors.New("session is closed")
|
||||
ErrWriteClosed = errors.New("tried to write to closed a session")
|
||||
ErrMessageBufferFull = errors.New("session message buffer is full")
|
||||
)
|
11
vendor/gopkg.in/olahol/melody.v1/hub.go → vendor/github.com/olahol/melody/hub.go
generated
vendored
11
vendor/gopkg.in/olahol/melody.v1/hub.go → vendor/github.com/olahol/melody/hub.go
generated
vendored
40
vendor/gopkg.in/olahol/melody.v1/melody.go → vendor/github.com/olahol/melody/melody.go
generated
vendored
40
vendor/gopkg.in/olahol/melody.v1/melody.go → vendor/github.com/olahol/melody/melody.go
generated
vendored
72
vendor/gopkg.in/olahol/melody.v1/session.go → vendor/github.com/olahol/melody/session.go
generated
vendored
72
vendor/gopkg.in/olahol/melody.v1/session.go → vendor/github.com/olahol/melody/session.go
generated
vendored
@ -1,10 +0,0 @@
|
||||
language: go
|
||||
sudo: required
|
||||
go:
|
||||
- 1.6
|
||||
- 1.7
|
||||
- 1.8
|
||||
install:
|
||||
- go get github.com/gorilla/websocket
|
||||
script:
|
||||
- go test
|
@ -1,185 +0,0 @@
|
||||
# melody
|
||||
|
||||
[![Build Status](https://travis-ci.org/olahol/melody.svg)](https://travis-ci.org/olahol/melody)
|
||||
[![Coverage Status](https://img.shields.io/coveralls/olahol/melody.svg?style=flat)](https://coveralls.io/r/olahol/melody)
|
||||
[![GoDoc](https://godoc.org/github.com/olahol/melody?status.svg)](https://godoc.org/github.com/olahol/melody)
|
||||
|
||||
> :notes: Minimalist websocket framework for Go.
|
||||
|
||||
Melody is websocket framework based on [github.com/gorilla/websocket](https://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:
|
||||
|
||||
* [x] Clear and easy interface similar to `net/http` or Gin.
|
||||
* [x] A simple way to broadcast to all or selected connected sessions.
|
||||
* [x] Message buffers making concurrent writing safe.
|
||||
* [x] Automatic handling of ping/pong and session timeouts.
|
||||
* [x] Store data on sessions.
|
||||
|
||||
## Install
|
||||
|
||||
```bash
|
||||
go get gopkg.in/olahol/melody.v1
|
||||
```
|
||||
|
||||
## [Example: chat](https://github.com/olahol/melody/tree/master/examples/chat)
|
||||
|
||||
[![Chat](https://cdn.rawgit.com/olahol/melody/master/examples/chat/demo.gif "Demo")](https://github.com/olahol/melody/tree/master/examples/chat)
|
||||
|
||||
Using [Gin](https://github.com/gin-gonic/gin):
|
||||
```go
|
||||
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](https://github.com/labstack/echo):
|
||||
```go
|
||||
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](https://github.com/olahol/melody/tree/master/examples/gophers)
|
||||
|
||||
[![Gophers](https://cdn.rawgit.com/olahol/melody/master/examples/gophers/demo.gif "Demo")](https://github.com/olahol/melody/tree/master/examples/gophers)
|
||||
|
||||
```go
|
||||
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](https://github.com/olahol/melody/tree/master/examples)
|
||||
|
||||
## [Documentation](https://godoc.org/github.com/olahol/melody)
|
||||
|
||||
## 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](http://godoc.org/github.com/gorilla/websocket#hdr-Origin_Considerations):
|
||||
|
||||
```go
|
||||
m := melody.New()
|
||||
m.Upgrader.CheckOrigin = func(r *http.Request) bool { return true }
|
||||
```
|
Loading…
Reference in New Issue