我已经实现了node-ssdp
( npm install node-ssdp
) 的服务器/客户端实现。一切“似乎”都可以工作,但我的客户没有收到服务器的数据包。我从不同的设备/位置接收到许多其他有效负载,但不是来自我的node-ssdp
服务器的有效负载。
我在同一台机器上运行,我在 OSX 上运行。我有两个独立的节点项目:一个用于我的客户端,一个用于我的服务器。
注意:我还尝试在一台机器上运行服务器,在另一台机器上运行客户端,以防环回或其他问题。我还通过 Wireshark 验证了来自服务器的数据包正在被客户端机器读取。它NOTIFY * HTTP/1.1
在标头中发送一个。
这是我对客户端和服务器的实现:
服务器
var SSDP = require('node-ssdp').Server
, server = new SSDP({
location: 'http://' + require('ip').address() + ':33333',
ssdpPort: 33333
})
console.log(require('ip').address())
server.addUSN('upnp:rootdevice')
server.addUSN('urn:schemas-upnp-org:device:MediaServer:1')
server.addUSN('urn:schemas-upnp-org:service:ContentDirectory:1')
server.addUSN('urn:schemas-upnp-org:service:ConnectionManager:1')
server.on('advertise-alive', function (heads) {
console.log('advertise-alive', heads)
// Expire old devices from your cache.
// Register advertising device somewhere (as designated in http headers heads)
})
server.on('advertise-bye', function (heads) {
console.log('advertise-bye', heads)
// Remove specified device from cache.
})
// start server on all interfaces
console.log('starting ssdp server')
server.start()
客户
var ssdp = require('node-ssdp').Client
, client = new ssdp({
})
client.on('notify', function () {
//console.log('Got a notification.')
})
client.on('response', function inResponse(headers, code, rinfo) {
console.log('Got a response to an m-search:\n%d\n%s\n%s', code, JSON.stringify(headers, null, ' '), JSON.stringify(rinfo, null, ' '))
})
client.search('ssdp:all')
// And after 10 seconds, you want to stop
setTimeout(function () {
client.stop()
}, 10000)
我的想法不多了。这很奇怪,因为我之前已经实现了一个 UDP 多播解决方案并且它可以工作。据我了解,SSDP 是幕后的 UDP 多播。