Platforms to show: All Mac Windows Linux Cross-Platform

/MacFrameworks/NSNetService/Bonjour Browser Web


Required plugins for this example: MBS MacFrameworks Plugin, MBS MacCocoa 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 Web

This example is the version from Mon, 16th Jan 2022.

Project "Bonjour Browser Web.xojo_binary_project"
Class App Inherits WebApplication
EventHandler Sub Opening(args() as String) Initialize End EventHandler
Sub Initialize() EventTimer = New EventTimer // register test service Const domain = "local." // default Const type = "_test._tcp." Const name = "My Test Service" Const port = 12345 testService = New NSNetServiceMBS(domain, type, name, port) testService.publish End Sub
Property EventTimer As Timer
Property Protected testService As NSNetServiceMBS
End Class
Class Session Inherits WebSession
Const ErrorDialogCancel = "Do Not Send"
Const ErrorDialogMessage = "This application has encountered an error and cannot continue."
Const ErrorDialogQuestion = "Please describe what you were doing right before the error occurred:"
Const ErrorDialogSubmit = "Send"
Const ErrorThankYou = "Thank You"
Const ErrorThankYouMessage = "Your feedback helps us make improvements."
Const NoJavascriptInstructions = "To turn Javascript on, please refer to your browser settings window."
Const NoJavascriptMessage = "Javascript must be enabled to access this page."
End Class
Class MainPage Inherits WebPage
Control List Inherits WebListBox
ControlInstance List Inherits WebListBox
End Control
EventHandler Sub Opening() #If TargetMachO Then // run on main thread, so events get delivered xojo.core.timer.CallLater 0, WeakAddressOf searchForServices #Else MessageBox "This example requires OS X." #EndIf End EventHandler
Sub searchForServices() // search for 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 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
End Class
Class EventTimer Inherits Timer
EventHandler Sub Action() // run event loop once NSRunLoopMBS.currentRunLoop.runOnce System.DebugLog CurrentMethodName End EventHandler
Sub Constructor() me.Period = 50 me.mode = 2 End Sub
End Class
Class ServiceBrowser Inherits NSNetServiceBrowserMBS
EventHandler Sub DidFindService(service as NSNetServiceMBS, moreComing as Boolean) List.AddRow Service.name, Service.type, Service.hostName // 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 list.RowTagAt(list.LastAddedRowIndex) = m End EventHandler
EventHandler Sub DidNotSearch(error as Dictionary) Break // error? End EventHandler
EventHandler Sub DidRemoveService(service as NSNetServiceMBS, moreComing as Boolean) System.DebugLog CurrentMethodName dim index as integer = FindIndex(list, service) if index >= 0 then list.RemoveRowAt index end if End EventHandler
Property Protected Services() As NSNetServiceMBS
Property list As WebListBox
End Class
Class NetworkService Inherits NSNetServiceMBS
EventHandler Sub DidNotResolve(Error as Dictionary) System.DebugLog CurrentMethodName dim index as integer = FindIndex(list, self) if index >= 0 then List.CellValueAt(index, 3) = "failed" List.CellValueAt(index, 4) = "" end if End EventHandler
EventHandler Sub DidResolveAddress() System.DebugLog CurrentMethodName dim index as integer = FindIndex(list, self) if index >= 0 then List.CellValueAt(index, 2) = Me.hostName List.CellValueAt(index, 3) = Me.addressIPv4 List.CellValueAt(index, 4) = str(me.port) end if End EventHandler
EventHandler Sub WillResolve() System.DebugLog CurrentMethodName dim index as integer = FindIndex(list, self) if index >= 0 then List.CellValueAt(index, 4) = "..." end if End EventHandler
Property List As WebListBox
End Class
Module UtilityModule
Function FindIndex(list as WebListBox, Service as NSNetServiceMBS) As integer dim u as integer = list.RowCount-1 for i as integer = u downto 0 dim n as NSNetServiceMBS = list.RowTagAt(i) if n<>nil and n.handle = Service.handle then Return i end if next End Function
End Module
End Project

See also:

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


The biggest plugin in space...