Platforms to show: All Mac Windows Linux Cross-Platform

/MacFrameworks/NSNetService/Bonjour Browser Desktop


Required plugins for this example: MBS MacFrameworks Plugin, MBS MacBase Plugin, MBS Main Plugin

You find this example project in your Plugins Download as a Xojo project file within the examples folder: /MacFrameworks/NSNetService/Bonjour Browser Desktop

This example is the version from Sat, 19th Feb 2021.

Project "Bonjour Browser Desktop.xojo_binary_project"
Class App Inherits Application
Const kEditClear = "&Delete"
Const kFileQuit = "&Quit"
Const kFileQuitShortcut = ""
End Class
Class ServiceBrowser Inherits NSNetServiceBrowserMBS
EventHandler Sub DidFindDomain(domainName as String, moreComing as Boolean) #Pragma Unused moreComing System.DebugLog CurrentMethodName List.AddRow "Found Domain: "+domainName End EventHandler
EventHandler Sub DidFindService(service as NSNetServiceMBS, moreComing as Boolean) #Pragma Unused moreComing System.DebugLog CurrentMethodName List.AddRow "Found Service: """+Service.name+""" of type: "+Service.type // make a new object so we have our events installed Dim m As New NetworkService(Service) m.list = list m.resolve // keep Xojo object alive for later services.Append m End EventHandler
EventHandler Sub DidNotSearch(error as Dictionary) System.DebugLog CurrentMethodName Dim ErrorCode As Integer = error.Lookup(NSNetServiceMBS.NSNetServicesErrorCode, "") Dim errorDomain As String = error.Lookup(NSNetServiceMBS.NSNetServicesErrorDomain, "") List.AddRow "Did not search: "+Str(errorDomain)+": "+Str(ErrorCode) End EventHandler
EventHandler Sub DidRemoveDomain(domainName as String, moreComing as Boolean) #Pragma Unused moreComing System.DebugLog CurrentMethodName List.AddRow "Remove Domain: "+domainName End EventHandler
EventHandler Sub DidRemoveService(service as NSNetServiceMBS, moreComing as Boolean) #Pragma Unused moreComing System.DebugLog CurrentMethodName List.AddRow "Remove Service: "+Service.name End EventHandler
EventHandler Sub DidStopSearch() System.DebugLog CurrentMethodName List.AddRow "Stop search." End EventHandler
EventHandler Sub WillSearch() System.DebugLog CurrentMethodName List.AddRow "will search..." End EventHandler
Property Protected Services() As NSNetServiceMBS
Property list As listbox
End Class
Class NetworkService Inherits NSNetServiceMBS
EventHandler Sub DidNotPublish(Error as Dictionary) Dim ErrorCode As Integer = error.Lookup(NSNetServicesErrorCode, "") 'Dim errorDomain As String = error.Lookup(NSNetServicesErrorDomain, "") System.DebugLog CurrentMethodName List.AddRow "Did not publish with errorcode: "+str(ErrorCode) End EventHandler
EventHandler Sub DidNotResolve(Error as Dictionary) Dim ErrorCode As Integer = error.Lookup(NSNetServicesErrorCode, "") 'Dim errorDomain As String = error.Lookup(NSNetServicesErrorDomain, "") System.DebugLog CurrentMethodName List.AddRow "Did not resolve with errorcode: "+str(ErrorCode) End EventHandler
EventHandler Sub DidPublish() System.DebugLog CurrentMethodName List.AddRow "Did publish" End EventHandler
EventHandler Sub DidResolveAddress() System.DebugLog CurrentMethodName List.AddRow "Did resolve address for "+Me.name+" with type "+Me.type+": "+Join(me.addresses, ", ")+":"+Str(Me.port) End EventHandler
EventHandler Sub DidStop() System.DebugLog CurrentMethodName List.AddRow "Did stop" End EventHandler
EventHandler Sub DidUpdateTXTRecordData(data as MemoryBlock) System.DebugLog CurrentMethodName dim s as string = data if encodings.UTF8.IsValidData(s) then s = DefineEncoding(S, encodings.UTF8) else s = DefineEncoding(s, encodings.ISOLatin1) end if List.AddRow "Did update text record: "+s End EventHandler
EventHandler Sub WillPublish() System.DebugLog CurrentMethodName List.AddRow "Will publish" End EventHandler
EventHandler Sub WillResolve() System.DebugLog CurrentMethodName List.AddRow "Will resolve "+me.name+" of type "+me.type End EventHandler
Property List As Listbox
End Class
Class MainWindow Inherits Window
Control List Inherits Listbox
ControlInstance List Inherits Listbox
End Control
Control PublishButton Inherits PushButton
ControlInstance PublishButton Inherits PushButton
EventHandler Sub Action() Const domain = "" // default const type = "_test._tcp." Const name = "MyTestService" const port = 12345 testService = New NetworkService(domain, type, name, port) testService.List = list testService.publish End EventHandler
End Control
Control ResolveButton Inherits PushButton
ControlInstance ResolveButton Inherits PushButton
EventHandler Sub Action() Const domain = "" const type = "_test._tcp." Const name = "MyTestService" resolveService = new NetworkService(domain, type, name) resolveService.List = list resolveService.resolve End EventHandler
End Control
EventHandler Sub Open() searchForBrowsableDomains // search for a few types searchForServicesOfType "_test._tcp." searchForServicesOfType "_smb._tcp." searchForServicesOfType "_afpovertcp._tcp." searchForServicesOfType "_printer._tcp." searchForServicesOfType "_http._tcp." searchForServicesOfType "_presence._tcp." searchForServicesOfType "_whats-my-name._tcp." searchForServicesOfType "_rfb._tcp." End EventHandler
Protected Sub searchForBrowsableDomains() dim m as new ServiceBrowser m.list = List // search for domains m.searchForBrowsableDomains browsers.Append m End Sub
Protected Sub searchForServicesOfType(type as text) dim m as new ServiceBrowser m.list = List // search for domains m.searchForServicesOfType type, "" browsers.Append m End Sub
Property Protected browsers() As ServiceBrowser
Property resolveService As NetworkService
Property Private testService As NetworkService
End Class
MenuBar MainMenuBar
MenuItem FileMenu = "&File"
MenuItem FileQuit = "#App.kFileQuit"
MenuItem EditMenu = "&Edit"
MenuItem EditUndo = "&Undo"
MenuItem EditSeparator1 = "-"
MenuItem EditCut = "Cu&t"
MenuItem EditCopy = "&Copy"
MenuItem EditPaste = "&Paste"
MenuItem EditClear = "#App.kEditClear"
MenuItem EditSeparator2 = "-"
MenuItem EditSelectAll = "Select &All"
End MenuBar
End Project

See also:

The items on this page are in the following plugins: MBS MacFrameworks Plugin.


The biggest plugin in space...