Created
January 6, 2016 21:56
-
-
Save darkwave/fa9e2ac190c7f4e79d02 to your computer and use it in GitHub Desktop.
Bonjour using jmDNS on Processing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import javax.jmdns.*; | |
import java.util.*; | |
import java.net.*; | |
JmDNS jmdns; | |
static String message = ""; | |
void setup() { | |
size(640, 480); | |
InetAddress ipAddress = getLocalIpAddress(); | |
try { | |
jmdns = JmDNS.create(ipAddress, InetAddress.getByName(ipAddress.getHostAddress()).getHostName()); | |
ServiceInfo service = ServiceInfo.create("_http._tcp.local.", "foo", 9191, 0, 0, true, ipAddress.getHostAddress() + "ciao"); | |
jmdns.registerService(service); | |
} | |
catch(Exception ex) { | |
println(ex); | |
} | |
jmdns.addServiceListener("_http._tcp.local.", new SampleListener()); | |
} | |
void exit() { | |
jmdns.unregisterAllServices(); | |
} | |
void draw() { | |
background(0); | |
text(message, 10, 10, width - 10, height - 10); | |
} | |
InetAddress getLocalIpAddress() { | |
try { | |
Enumeration e = NetworkInterface.getNetworkInterfaces(); | |
while (e.hasMoreElements ()) | |
{ | |
NetworkInterface n = (NetworkInterface) e.nextElement(); | |
Enumeration ee = n.getInetAddresses(); | |
while (ee.hasMoreElements ()) | |
{ | |
InetAddress i = (InetAddress) ee.nextElement(); | |
String ipAddress = i.getHostAddress(); | |
String[] ips = split(ipAddress, '.'); | |
if (ipAddress.indexOf("127.") != 0 && ipAddress.indexOf(":") < 0 | |
&& !ips[3].equals("1") // linux virtual ip quick fix | |
) { | |
return i; | |
} | |
} | |
} | |
} | |
catch (Exception ex) { | |
println("AH"); | |
} | |
return null; | |
} | |
static class SampleListener implements javax.jmdns.ServiceListener { | |
public void serviceAdded(ServiceEvent event) { | |
message += "Service added : " + event.getName() + "." + event.getType() + "\n"; | |
} | |
public void serviceRemoved(ServiceEvent event) { | |
message += "Service removed : " + event.getName() + "." + event.getType() + "\n"; | |
} | |
public void serviceResolved(ServiceEvent event) { | |
message += "Service resolved: " + event.getInfo() + "\n"; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment