Table of Contents
ZEROCONF
This is the documentation of the ZEROCONF module that implements a Zeroconf discovery api;
Use import { Zeroconf } from "hop:zeroconf";
This imports the Zeroconf
constructor.
Example
zeroconf.js
"use hopscript"
import { Zeroconf } from "hop:zeroconf";
import * as hop from "hop";
const zc = Zeroconf();
console.log("zeroconf backend:", zc.backend);
console.log("publishing...");
zc.publish("Hop.js", "_dummy._tcp", hop.port);
console.log("scanning services...");
zc.addEventListener("", e => console.log(e.type,e.hostname));
Functions
Zeroconf()
Create a Zeroconf object, which following properties:
backend
: the name of the zeroconf backend;addEventListener
: add discovery listener;publish
: publish a service.
zeroconf#addEventListener(type, callback)
Add an discovery listener. The argument type, is either a type name,
e.g., http.tcp
, or the empty string. The callback
argument is
a procedure of one argument. It is invoked each time a service is discovered
with one object containing the following fields:
interface
: the network interface;protocol
: the network protocol;type
: the zeroconf type of the service;hostname
: the name of the host publishing the service;domainname
: the domain of the host publishing the service;port
: the port of the host;address
: the IP address of the host;options
: an array of options.
zeroconf#publish(name, type, [port, [option1, ...]])
Publish a service.
Types
The TypeScript type declarations are:
export interface DiscoveryEvent {
interface: number,
name: string,
protocol: string,
domain: string,
hostname: string,
port: number,
address: string,
string[],
type: string
}
type listener = (e: DiscoveryEvent) => void;
export interface Zeroconf {
backend: string,
addEventListener: (type: string, proc: listener) => void,
publish: (name: string, type: string, port?: number, ... opt: string[]) => void
}
export function Zeroconf(): Zeroconf;