melody

package module
v0.0.0-...-241a687 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 24, 2015 License: BSD-2-Clause Imports: 4 Imported by: 0

README

melody

Build Status Coverage Status GoDoc

🎶 Minimalist websocket framework for Go.

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.

Install

go get github.com/olahol/melody

Example

Multi channel chat server, error handling left as en exercise for the developer.

Chat demo

package main

import (
	"github.com/olahol/melody"
	"github.com/gin-gonic/gin"
	"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("/channel/:name", func(c *gin.Context) {
		http.ServeFile(c.Writer, c.Request, "chan.html")
	})

	r.GET("/channel/:name/ws", func(c *gin.Context) {
		m.HandleRequest(c.Writer, c.Request)
	})

	m.HandleMessage(func(s *melody.Session, msg []byte) {
		m.BroadcastFilter(msg, func(q *melody.Session) bool {
			return q.Request.URL.Path == s.Request.URL.Path
		})
	})

	r.Run(":5000")
}
More examples

Documentation

Contributors

  • Ola Holmström (@olahol)
  • Shogo Iwano (@shiwano)

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 }

Documentation

Overview

Package melody implements a framework for dealing with WebSockets.

Example

A broadcasting echo server:

func main() {
	r := gin.Default()
	m := melody.New()
	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")
}

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config struct {
	WriteWait         time.Duration // Milliseconds until write times out.
	PongWait          time.Duration // Timeout for waiting on pong.
	PingPeriod        time.Duration // Milliseconds between pings.
	MaxMessageSize    int64         // Maximum size in bytes of a message.
	MessageBufferSize int           // The max amount of messages that can be in a sessions buffer before it starts dropping them.
}

Melody configuration.

type Melody

type Melody struct {
	Config   *Config
	Upgrader *websocket.Upgrader
	// contains filtered or unexported fields
}

func New

func New() *Melody

Returns a new melody instance with default Upgrader and Config.

func (*Melody) Broadcast

func (m *Melody) Broadcast(msg []byte)

Broadcasts a text message to all sessions.

func (*Melody) BroadcastBinary

func (m *Melody) BroadcastBinary(msg []byte)

Broadcasts a binary message to all sessions.

func (*Melody) BroadcastBinaryFilter

func (m *Melody) BroadcastBinaryFilter(msg []byte, fn func(*Session) bool)

Broadcasts a binary message to all sessions that fn returns true for.

func (*Melody) BroadcastBinaryOthers

func (m *Melody) BroadcastBinaryOthers(msg []byte, s *Session)

Broadcasts a binary message to all sessions except session s.

func (*Melody) BroadcastFilter

func (m *Melody) BroadcastFilter(msg []byte, fn func(*Session) bool)

Broadcasts a text message to all sessions that fn returns true for.

func (*Melody) BroadcastOthers

func (m *Melody) BroadcastOthers(msg []byte, s *Session)

Broadcasts a text message to all sessions except session s.

func (*Melody) Close

func (m *Melody) Close()

Closes the melody instance and all connected sessions.

func (*Melody) HandleConnect

func (m *Melody) HandleConnect(fn func(*Session))

Fires fn when a session connects.

func (*Melody) HandleDisconnect

func (m *Melody) HandleDisconnect(fn func(*Session))

Fires fn when a session disconnects.

func (*Melody) HandleError

func (m *Melody) HandleError(fn func(*Session, error))

Fires when a session has an error.

func (*Melody) HandleMessage

func (m *Melody) HandleMessage(fn func(*Session, []byte))

Callback when a text message comes in.

func (*Melody) HandleMessageBinary

func (m *Melody) HandleMessageBinary(fn func(*Session, []byte))

Callback when a binary message comes in.

func (*Melody) HandleRequest

func (m *Melody) HandleRequest(w http.ResponseWriter, r *http.Request)

Handles http requests and upgrades them to websocket connections.

type Session

type Session struct {
	Request *http.Request
	// contains filtered or unexported fields
}

A melody session.

func (*Session) Close

func (s *Session) Close()

Close session.

func (*Session) Write

func (s *Session) Write(msg []byte)

Write message to session.

func (*Session) WriteBinary

func (s *Session) WriteBinary(msg []byte)

Write binary message to session.

Directories

Path Synopsis
examples

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL