🎶 Minimalist websocket framework for Go. 🎶 This is fork project by github.com/olahol/melody
go get github.com/z9905080/melody
Using Gin:
import (
"github.com/gin-gonic/gin"
"github.com/z9905080/melody"
"net/http"
"time"
)
var textTopic = "topic1"
var binaryTopic = "topic2"
func main() {
r := gin.Default()
m := melody.New(
melody.DialChannelBufferSize(100),
melody.DialWriteBufferSize(1024))
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) {
// subscribe topic
s.AddSub(textTopic)
})
m.HandleMessageBinary(func(s *melody.Session, msg []byte) {
// subscribe topic
s.AddSub(binaryTopic)
})
go func() {
for {
// PubMsg is Send TextMessage
m.PubMsg([]byte("this is a text message."), false, textTopic)
// PubTextMsg is Send TextMessage (Same to PubMsg)
m.PubTextMsg([]byte("this is a text message."), false, textTopic)
// PubBinaryMsg is Send BinaryMessage
m.PubBinaryMsg([]byte("this is an binary message."), false, binaryTopic)
time.Sleep(5 * time.Second)
}
}()
r.Run(":5000")
}
- Shouting (@z9905080)
- Ola Holmström (@olahol)
- Shogo Iwano (@shiwano)
- Matt Caldwell (@mattcaldwell)
- Heikki Uljas (@huljas)
- Robbie Trencheny (@robbiet480)
- yangjinecho (@yangjinecho)
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 }