shihray / melody

:notes: Minimalist websocket framework for Go

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

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)

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

:notes: Minimalist websocket framework for Go

License:BSD 2-Clause "Simplified" License


Languages

Language:Go 100.0%