Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up

A Flutter plugin that enables Network Service Discovery (mDNS) on various platforms via their native APIs.

License

NotificationsYou must be signed in to change notification settings

Nimrodda/flutter_nsd

Repository files navigation

A Flutter plugin for Network Service Discovery (mDNS) on Android, iOS, MacOS and Windows. The plugin uses the platform's own API when possible.

The plugin currently only supports discovery, but not registry of services.

Install

Add the dependency to pubspec.yaml:

dependencies:  flutter_nsd: ^1.4.0

Getting Started

InitializeFlutterNsd singleton and listen to the stream:

voidinit()async {final flutterNsd=FlutterNsd();  flutterNsd.stream.listen((nsdServiceInfo) {print('Discovered service name: ${nsdServiceInfo.name}');print('Discovered service hostname/IP: ${nsdServiceInfo.hostname}');print('Discovered service port: ${nsdServiceInfo.port}');  }, onError: (e) {if (eisNsdError) {// Check e.errorCode for the specific error    }  });}

Start discovery when needed. At this point, if any services are discovered, your stream listenerwill get notified. In case of an error,NsdError is emitted to the stream (Currently it'sjust a generic error, but in the future, it will be more specific).

voidstartDiscoveryButton()async {await flutterNsd.discoverServices('_http._tcp.');}

Stop discovery when done. If you don't call this method, discovery will continue until the appprocess is killed.

voidstopDiscoveryButton()async {await flutterNsd.stopDiscovery();}

See the example project for a more detailed Flutter app example.

Note about Android

Minimum Android API version supported is 21.

Android emulator doesn't support Network Service Discovery so you'll have to use a real device.

Note about iOS

This plugin usesNetServiceBrowser and can therefore support iOS version 9+.

On iOS 14+, you need to modifyInfo.plist file and add two keys:

  • Bonjour Services - this is an array, the first item should be the service you're trying todiscover. For example,_http._tcp..
  • Privacy - Local Network Usage Description - this key is for granting the app local network access.The value is the text which will be shown to the user in a permission dialog once you callflutterNsd.discoverServices().

Example:

<key>NSLocalNetworkUsageDescription</key><string>Reasoning for the user why you need this permission goes here</string><key>NSBonjourServices</key><array>   <string>_http._tcp.</string></array>

For more info about network discovery on iOS 14, I suggest you watchthisvideo.

Note that you don't need to worry about modifyingInfo.plist if you are just testing with a simulator.

The iOS and macOS implementations currently do not returnhostAddresses

Note about Windows

Windows does not have native support for MDNS, therefore it is implemented using thehttps://github.com/mjansson/mdns librarywhich implements MDNS using sockets. The library will return separate results for ipv4 and ipv6.

The current implementation will send MDNS multicast every 10 seconds until stopped.

Due to the native socket calls, any app using this plugin on windows will trigger a dialog from Windows to allow network access on the first launch.

For Windows development you will need Visual Studio 2019 or higher with the C++ workload installed, seehttps://docs.flutter.dev/desktop#additional-windows-requirements

Testing

On MacOS it's easy to test network service discovery via the following command which will create a mock service:

dns-sd -R TestService _http._tcp . 3000

Then scan for this service using the example app on any of the supported platforms.

License

Copyright 2023 Nimrod Dayan nimroddayan.com

Licensed under the Apache License, Version 2.0 (the "License");you may not use this file except in compliance with the License.You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, softwaredistributed under the License is distributed on an "AS IS" BASIS,WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.See the License for the specific language governing permissions andlimitations under the License.

About

A Flutter plugin that enables Network Service Discovery (mDNS) on various platforms via their native APIs.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

[8]ページ先頭

©2009-2025 Movatter.jp