multicast

package module
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Jun 13, 2021 License: Apache-2.0 Imports: 3 Imported by: 0

README

go-multicast

PkgGoDev GoDoc Go Report Card codecov tests lint

package provide multicast sending messages to all connected clients

Usage:

package main

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

func main() {
	ctx, cancel := context.WithCancel(context.Background())
	defer cancel()
	m := multicast.New(
		ctx,
		func(err error) {
			fmt.Println(err)
		},
		nil, // latest message replace snapshot
		time.Second,
	)
	go func() {
		for {
			m.SendAll(time.Now())
			time.Sleep(time.Millisecond * 100)
		}
	}()
	http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
		conn, err := websocket.Upgrader.Upgrade(w, r, nil)
		if err != nil {
			fmt.Println(err)
			return
		}
		websocket.Add(
			m,
			conn,
			nil,
		)
	})
	if err := http.ListenAndServe(":80", nil); err != nil {
		fmt.Println(err)
	}
}

Documentation

Overview

Package multicast provides multicast helpers over websocket.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ChangeIntervalMessage

type ChangeIntervalMessage struct {
	Interval time.Duration
	Error    error
}

ChangeIntervalMessage is a specified message for change interval of sending high-frequency messages

type Multicast

type Multicast struct {
	// contains filtered or unexported fields
}

Multicast is a main communicator for linking sender and receivers

func New

func New(
	ctx context.Context,
	onError func(error),
	merge func(source interface{}, diff interface{}) (merged interface{}),
	initInterval time.Duration,
) *Multicast

New creates new Multicast with empty clients

func (*Multicast) Add

func (m *Multicast) Add(
	read func() (msg interface{}, err error),
	write func(msg interface{}) (err error),
	onDone func(),
) (received <-chan interface{}, done chan<- struct{})

Add appends some client to multicast

func (*Multicast) SendAll

func (m *Multicast) SendAll(msg interface{})

SendAll provide sending message to multiple clients

Directories

Path Synopsis
helpers

Jump to

Keyboard shortcuts

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