mdns

package
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Jul 22, 2025 License: MIT Imports: 8 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// UsernameKey used to store the username of the service owner in mDNS TXT records
	UsernameKey     = "username"
	DefaultInstance = "letshare"
	Domain          = "local"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type MDNS

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

MDNS handles multicast DNS service registration and discovery on local networks. It provides methods to publish services and discover other services on the network.

func Get

func Get() *MDNS

Get creates and returns a new MDNS instance. The returned instance is initialized with an empty service entries map.

This function uses a singleton pattern to ensure only one MDNS instance exists across the application. Subsequent calls return the same instance.

func (*MDNS) Browse

func (r *MDNS) Browse(ctx context.Context) error

Browse discovers mDNS services on the local network. It listens for changes in the mDNS service entries and updates the internal state. The function blocks until the provided context is done. It uses a callback function to handle service events (added, updated, removed).

func (*MDNS) Entries

func (r *MDNS) Entries() ServiceEntries

Entries returns a copy of all currently discovered mDNS services. This method is safe for concurrent use from multiple goroutines.

Returns:

  • ServiceEntries: A map of service instance names to their network details(ServiceEntry).

func (*MDNS) NotifyOnChange

func (r *MDNS) NotifyOnChange() <-chan struct{}

NotifyOnChange returns a channel that blocks until a change occurs in the discovered mDNS entries. the channel is closed when a change happens, and it is reset for the next notification.

func (*MDNS) Publish

func (r *MDNS) Publish(ctx context.Context, instance, hostname, username string, port uint16) error

Publish registers a service instance with mDNS.

Params:

  • ctx: Context to control the lifetime of the service registration.
  • instance: Unique name for the service instance. (e.g., "letshare")
  • hostname: Hostname of the machine hosting the service. (e.g., "my-computer.local")
  • username: Username of the service owner, used in TXT records. (e.g., "john_doe")
  • port: Port on which the service is running. (e.g., 80)

Returns:

  • error: An error if the service registration fails, otherwise nil.

func (*MDNS) ReloadBrowser

func (r *MDNS) ReloadBrowser()

func (*MDNS) ReloadPublisher

func (r *MDNS) ReloadPublisher()

type ServiceEntries

type ServiceEntries map[string]ServiceEntry

ServiceEntries represents discovered mDNS services where: - Key: Service instance name - Value: Service network details (Hostname, IP, Port)

type ServiceEntry

type ServiceEntry struct {
	Owner, Hostname, IP string
	Port                uint16
}

ServiceEntry represents a discovered mDNS service with its network details.

Jump to

Keyboard shortcuts

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