I am trying to register a service for discovery using Bonjour and can't seem to get it to work on a Mac.
I created a really simple example showing what I'm doing but all I ever see is the "Bonjour starting search" and "Bonjour will publish service 'Chat Service'". No other Console.WriteLines appear.
Here is my code...
using System; using MonoMac.Foundation; using System.Threading;
namespace BonjourTest { static class TestService { static NSNetService _testService;
public static void InitTestService()
{
_testService = new NSNetService ("", "_testservice._tcp", "Test Service", 28132);
_testService.Published += delegate(object sender, EventArgs e) {
NSNetService ns = sender as NSNetService;
Console.WriteLine("Bonjour published service '{0}'", ns.Name);
};
_testService.WillPublish += delegate(object sender, EventArgs e) {
NSNetService ns = sender as NSNetService;
Console.WriteLine("Bonjour will publish service '{0}'", ns.Name);
};
_testService.PublishFailure += delegate(object sender, NSNetServiceErrorEventArgs e) {
NSNetService ns = sender as NSNetService;
Console.WriteLine("Bonjour failed to publish service '{0}'", ns.Name);
};
_testService.Publish ();
}
}
class MainClass
{
static NSNetServiceBrowser _bonjourBrowserService;
static void InitBonjourBrowser()
{
_bonjourBrowserService = new NSNetServiceBrowser ();
_bonjourBrowserService.FoundService += delegate(object sender, NSNetServiceEventArgs e) {
Console.WriteLine("Bonjour found service '{0}'", e.Service.Name);
};
_bonjourBrowserService.ServiceRemoved += delegate(object sender, NSNetServiceEventArgs e) {
Console.WriteLine("Bonjour removed service '{0}'", e.Service.Name);
};
_bonjourBrowserService.SearchStarted += delegate(object sender, EventArgs e) {
Console.WriteLine("Bonjour starting search");
};
_bonjourBrowserService.SearchStopped += delegate(object sender, EventArgs e) {
Console.WriteLine("Bonjour stopped searching");
};
_bonjourBrowserService.NotSearched += delegate(object sender, NSNetServiceErrorEventArgs e) {
Console.WriteLine("Bonjour not searched");
};
_bonjourBrowserService.SearchForServices ("_testservice._tcp", "");
}
static void Main (string[] args)
{
InitBonjourBrowser ();
TestService.InitTestService ();
while (true) {
Thread.Sleep (1000);
}
}
}
}