multicast

package module
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Mar 26, 2021 License: MIT Imports: 8 Imported by: 0

README

go-websockets-multicast

golang package provide multicast sending messages to all connected clients

Usage:

package main

import (
    multicast "github.com/asmyasnikov/go-websockets-multicast"
    "github.com/gorilla/websocket"
	"net/http"
    "fmt"
    "time"
)

var (
	upgrader = websocket.Upgrader{
		ReadBufferSize: 128,
		WriteBufferSize: 128,
	}
)

func main() {
    m := multicast.New(nil, true)
    go func() {
        for {
            m.SendAll(time.Now())
            time.Sleep(time.Second)
        }
    }()
    http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
		conn, err := upgrader.Upgrade(w, r, nil)
		if err != nil {
			fmt.Println(err)
			return
		}
		m.Add(conn)
    })
	if err := http.ListenAndServe(":80", nil); err != nil {
		fmt.Println(err)
	}
}

Documentation

Overview

Package multicast provides multicast helpers over websocket.

Index

Constants

View Source
const (
	MIN_DELAY = time.Duration(1000/24) * time.Millisecond
	MAX_DELAY = time.Second
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Multicast

type Multicast struct {
	sync.RWMutex
	// contains filtered or unexported fields
}

func New

func New(snapshot interface{}, ignoreDelays ...bool) *Multicast

Creates new Multicast with empty connections and channels

func (*Multicast) Add

func (m *Multicast) Add(conn *websocket.Conn) <-chan []byte

Add creates worker, that handle websocket data sending and receiving Returns channels of receive data from connection Closing of receive channel are equal for closing connection

func (*Multicast) Json

func (m *Multicast) Json(data interface{}) ([]byte, error)

Json get diff of object if its possible otherwise marshal full object

func (*Multicast) SendAll

func (m *Multicast) SendAll(data interface{}) error

SendAll sends diff data for all connections. If diff is empty, than data will not be sent

Jump to

Keyboard shortcuts

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