Golang upnp/ssdp DiscoverDevices example
Here is some general information about using SSDP with Go:
- The goupnp library is a popular library for working with
SSDP
andUPnP
(Universal Plug and Play) protocols in Go. It provides a simple API for sending and receivingSSDP
messages, as well as managingUPnP
services and devices. - To use
SSDP
with Go, you will first need to import the goupnp library and create a newSSDPClient
object. This object allows you to send and receiveSSDP
messages. - To discover devices on the local network, you can use the
SSDPClient.Search
method to send a search request and receive a list of matching devices. - To communicate with a specific device, you can use the
SSDPClient.GetUPnPDeviceDescription
method to retrieve the device'sUPnP
description and get information about the services and actions it offers. - Once you have a device description, you can use the
UPnPDevice object
to interact with the device and call its services and actions.
Here is an example of how you might use the goupnp
library to discover devices on the local network and print their descriptions:
package main
import (
"fmt"
"github.com/huin/goupnp"
"github.com/huin/goupnp/ssdp"
"log"
)
func main() {
// "urn:schemas-upnp-org:device:InternetGatewayDevice:1"
devices, err := goupnp.DiscoverDevices(ssdp.UPNPRootDevice /*ssdp.SSDPAll*/)
if err != nil {
log.Fatal("failed to discover devices", err)
}
for _, device := range devices {
if device.Err != nil {
log.Println("failed to discover device. skipping.", device.Err, "url", device.Location.String())
continue
}
fmt.Printf("%s\n", device.Location.Host)
fmt.Printf(" Devices\n")
device.Root.Device.VisitDevices(func(dev *goupnp.Device) {
fmt.Printf(" %s - %s - %s\n", dev.FriendlyName, dev.Manufacturer, dev.DeviceType)
})
fmt.Printf(" Services:\n")
device.Root.Device.VisitServices(func(svc *goupnp.Service) {
fmt.Printf(" %s\n", svc.String())
})
}
}
This code creates a new SSDPClient object and uses the DiscoverDevices
method to discover devices on the local network. It then prints the description of each device it finds.
Note that this is just an example and may not be complete or fully functional. For more detailed information and examples, please see the goupnp library documentation.
0
See Also
- Deal with sticky tcp/socket packet in Golang
- Full LRU/FIFO Cache Implementation in Golang
- The way Convert an integer to a byte array with golang
- Golang Generics For Non-Beginners
- Hello, This's a Golang web forum!