Categorygithub.com/huskar-t/melody
modulepackage
0.0.0-20240407104517-11dcf4a47591
Repository: https://github.com/huskar-t/melody.git
Documentation: pkg.go.dev

# README

melody

fork from https://github.com/olahol/melody

Melody is websocket framework based on 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:

  • Clear and easy interface similar to net/http or Gin.
  • A simple way to broadcast to all or selected connected sessions.
  • Message buffers making concurrent writing safe.
  • Automatic handling of ping/pong and session timeouts.
  • Store data on sessions.

Install

go get github.com/huskar-t/melody

Example

Using Gin:

package main

import (
	"github.com/gin-gonic/gin"
	"github.com/huskar-t/melody"
	"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")
}

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 }

# Functions

FormatCloseMessage formats closeCode and text as a WebSocket close message.
New creates a new melody instance with default UpGrader and Config.

# Constants

No description provided by the author
No description provided by the author

# Structs

Config melody configuration struct.
Melody implements a websocket manager.
Session wrapper around websocket connections.