Skip to content
forked from olahol/melody

🎶 Minimalist websocket framework for Go

License

Notifications You must be signed in to change notification settings

z9905080/melody

This branch is 50 commits ahead of, 53 commits behind olahol/melody:master.

Folders and files

NameName
Last commit message
Last commit date

Latest commit

1a2c991 · Apr 9, 2022
May 27, 2020
Dec 31, 2019
May 27, 2020
May 18, 2017
Jun 21, 2015
May 27, 2020
May 10, 2016
Jun 21, 2015
Sep 23, 2019
Apr 9, 2022
Mar 17, 2020
Mar 17, 2020
Dec 31, 2019
Apr 9, 2022
Mar 28, 2017
Feb 21, 2020
Jul 23, 2019
Apr 9, 2022

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

Packages

No packages published

Languages

  • Go 100.0%