csharpudpdiscovery
v1.0.1
Published
UDP Local Network Discovery in C#
Downloads
2
Readme
UDP Network Discovery for C#
Overview
This NPM package provides a simple and lightweight solution for UDP network discovery in C# applications. It allows you to easily discover and communicate with devices or services on your local network using the User Datagram Protocol (UDP).
Installation
You can install this package using NPM:
npm install udp-network-discovery-csharp
Alternatively, you can add it to Unity with a scoped registry
registry.npmjs.com
Usage
- Create a
DiscoveryService
instance:
DiscoveryService discovery = new DiscoveryService("MyServer", 1000, 25565);
Use parameters to configure polling intervals and UDP port to use.
- Start the discovery advertisement:
discovery.StartAdvertising();
- Listen for incoming discovery messages:
discovery.onHostIp += () =>
{
string message = args.Message; // The received message.
// Handle the received message.
};
discovery.StartListening();
- Set discovery messages to the network during advertisement:
discovery.SetAdvertisement("Hello, network!");
- Stop the discovery service when you're done:
discovery.StopAsyncTasks();
Note that it stops both listeners and advertisers
Example
Here's a simple example of how to use this package to discover devices or services on your local network in Unity context:
public class ExampleUDP : MonoBehaviour
{
//This example can be run on the same machine. Editor will be the host, and Standalone build will be the client. Just run the client after the host, and check the console for the Host ip address.
void Start()
{
DiscoveryService discoveryService = new DiscoveryService();
#if !UNITY_EDITOR
discoveryService.SetAdvertisement(SystemInfo.deviceName);
discoveryService.StartAdvertising();
#else
discoveryService.StartListening();
#endif
}
}
License
This package is provided under the MIT License.
Author
This package is maintained by Mykhailo "Hyst3r1a" Gorshenin. You can contact me at [email protected].
Happy network discovery with C#! 🚀