Hello friends,
I decided to read the zeroconf plugin documentation again and found a solution to my problem.
I promised to try to make working examples of using zeroconf plugin to send and receive data.
Here is the code for the examples.
If you want to see how they work, make two new projects with Corona Simulator and writing the Sender code in main.lua file and entering the Receiver code in the other in main.lua file.
Then start Sender first and then Receiver and you will see the result on the console.
----------------------------------------------------------------------------------------- -- -- main.lua -- ----------------------------------------------------------------------------------------- ---- Sender local zeroconf = require( "plugin.zeroconf" ) local json = require( "json" ) local first = "Corona " local seccond = "is" local third = "awesome !" local record = {} record.first = first record.seccond = seccond record.third = third print( json.prettify( record ) ) -- Publishes a service of type '\_corona\_test.\_tcp' -- 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", data=record } )
----------------------------------------------------------------------------------------- -- -- main.lua -- ----------------------------------------------------------------------------------------- ---- Receiver local zeroconf = require( "plugin.zeroconf" ) local json = require( "json" ) -- Publishes a service of type '\_corona\_test.\_tcp' and then starts discovery for the service -- 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 if event.data then local ed = event.data print( json.prettify( ed ) ) 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 ) -- Start looking for published services local browser = zeroconf.browse( { type="\_corona\_test.\_tcp" } ) --------------------------------------------------------------------