richardschneider / net-mdns

Simple multicast DNS
MIT License
227 stars 79 forks source link

How to detect a device is offline? #97

Closed kingeaglewang closed 4 years ago

kingeaglewang commented 4 years ago
 mdns.NetworkInterfaceDiscovered += (s, e) =>
 {
       sd.QueryServiceInstances("_http._tcp");
 };

sd.ServiceInstanceDiscovered += (s, e) =>
{
      mdns.SendQuery(e.ServiceInstanceName);
 };

mdns.AnswerReceived += (s, e) =>
{
    // Get device information
}

I have tried the example code to browser the nearby devices. I can get all the devices I want. But if I disconnect one of the devices, there is no event to know the device has gone.

I try sd.ServiceInstanceShutdown,but program does not run into this event .

Is there any event or method to know a device is disconnected ? Thanks

queequac commented 4 years ago

Have you double-checked (e.g. via WireShark) that the device being disconnected is actually unadvertising itself?

The library is not book-keeping all the services it has seen, so there is also no timer that will track all the individual services' TTLs to fire ServiceInstanceShutdown if a service is no longer advertising.

kingeaglewang commented 4 years ago

It does not unadvertise itself even I disconnect the network cable. If I really want to check if a device is connected or disconnected. Can I use a timer to call sd.QueryServiceInstances("_http._tcp"); function ?

Or is there any good methods to do this ? Thanks for help

queequac commented 4 years ago

It does not unadvertise itself even I disconnect the network cable.

Well, if you remove the network cable or suddenly power off the device, how shall it unadvertise itself? Obviously unadvertising works only if the device is shutting down properly.

If I really want to check if a device is connected or disconnected. Can I use a timer to call sd.QueryServiceInstances("_http._tcp"); function ?

Might also depend on your use case and the resolution of the timer. But seems okay, if you don't do this every second and your query is quite specific to the actual target device(s).