zeroconf.*

Type Library
Revision Release 2022.3683
Keywords ZeroConf, network
Platforms Android, iOS, macOS, Windows, tvOS

Overview

This plugin allows you to discover and publish services over a local network. It allows cross-platform implementation of zero-configuration systems based on mDNS which can be utilized for local multiplayer or to synchronize data over WiFi.

Notes
  • Service discovery is available by default on all platforms except Windows. To use this plugin on Windows, Bonjour version 3 or higher must be installed.

  • You can use "Bonjour Browser" type software to verify if service discovery is functioning properly.

Syntax

local zeroconf = require( "plugin.zeroconf" )

Functions

Initialization

zeroconf.init()

Service Publishing

zeroconf.unpublishAll()

Service Discovery

Events

Project Settings

To use this plugin, add an entry into the plugins table of build.settings. When added, the build server will integrate the plugin during the build phase.

settings =
{
    plugins =
    {
        ["plugin.zeroconf"] =
        {
            publisherId = "com.coronalabs"
        },
    },
}

Example

-- Publishes a service of type '_corona_test._tcp' and then starts discovery for the service

-- Require the plugin
local zeroconf = require( "plugin.zeroconf" )

-- Listener to be called for ZeroConf events
local function zeroconfListener( event )

    -- Service has been found
    if ( event.phase == "found" and not event.isError ) then

        print( "SERVICE FOUND" )
        print( "-------------" )

        if event.serviceName then
            print( "Service name: " .. event.serviceName )
        end

        if event.port then
            print( "Port: " .. tostring(event.port) )
        end

        if ( event.addresses and #event.addresses > 0 ) then
            print( "Service provider addresses:" )
            for i = 1,#event.addresses do
                print( "  " .. event.addresses[i] )
            end
        end

    -- Service has been lost!
    elseif event.phase == "lost" then

        print( "SERVICE LOST!" )
        print( "-------------" )

        if event.serviceName then
            print( "Service name: " .. event.serviceName )
        end
    end
end

-- Initialize listener
zeroconf.init( zeroconfListener )

-- Generate a service name
local serviceName = system.getInfo("name") .. " (" .. system.getInfo("platformName") .. ")"

-- Publish a service (make it discoverable over the network)
local service = zeroconf.publish( { port=2929, name=serviceName, type="_corona_test._tcp" } )

-- Start looking for published services
local browser = zeroconf.browse( { type="_corona_test._tcp" } )