Skip to content
forked from z9905080/melody

🎶 Minimalist websocket framework for Go

License

Notifications You must be signed in to change notification settings

lzgcucool/melody

 
 

Repository files navigation

melody

Build Status codecov GoDoc

🎶 Minimalist websocket framework for Go. 🎶 This is fork project by github.com/olahol/melody

Install

go get github.com/z9905080/melody

[Example: DialOption]

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")
}

Contributors

  • Shouting (@z9905080)
  • 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 }

About

🎶 Minimalist websocket framework for Go

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Go 100.0%