我想向我的 DLNA 服务器发送 MSearch 请求并使用 Javascript 获取响应。我已经在 C# 中实现了代码,但我需要在 javascript 中使用它,因为我正在编写 Chrome 插件(Html、JS、CSS)。问题是我不太了解 Js,我已经阅读了 Node.js 文档但无法理解。
这是我的 C# 代码
public void search()
{
IPEndPoint LocalEndPoint = new IPEndPoint(IPAddress.Any, 6000); to 1900 to
IPEndPoint MulticastEndPoint = new IPEndPoint(IPAddress.Parse("239.255.255.250"), 1900);
Socket UdpSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
UdpSocket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
UdpSocket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Broadcast, true);
UdpSocket.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.MulticastTimeToLive, 2);
UdpSocket.Bind(LocalEndPoint);
UdpSocket.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.AddMembership, new MulticastOption(IPAddress.Parse("239.255.255.250"), LocalEndPoint.Address));
Console.WriteLine("UDP-Socket setup done...\r\n");
string SearchString = "M-SEARCH * HTTP/1.1\r\nHOST:239.255.255.250:1900\r\nMAN:\"ssdp:discover\"\r\nST:urn:schemas-upnp-org:device:MediaServer:1 \r\nMX:3\r\n\r\n"; //Wifi router connectify: => ST:urn:schemas-upnp-org:device:WANConnectionDevice:1
UdpSocket.SendTo(Encoding.UTF8.GetBytes(SearchString), SocketFlags.None, MulticastEndPoint); // ==> HTTPMU broadcast
UdpSocket.SendTo(Encoding.UTF8.GetBytes(SearchString), SocketFlags.None, MulticastEndPoint);
UdpSocket.SendTo(Encoding.UTF8.GetBytes(SearchString), SocketFlags.None, MulticastEndPoint);
Console.WriteLine("M-Search sent...\r\n");
byte[] ReceiveBuffer = new byte[64000];
int ReceivedBytes = 0;
while (true)
{
if (UdpSocket.Available > 0)
{
ReceivedBytes = UdpSocket.Receive(ReceiveBuffer, SocketFlags.None);
if (ReceivedBytes > 0)
{
string responseString = Encoding.UTF8.GetString(ReceiveBuffer, 0, ReceivedBytes);
if (!responseString.Contains("Windows/6.8 UPnP/1.1 Guru")) continue;
string url = GetResourceUriFromHTTPResp(responseString);
if (!string.IsNullOrEmpty(url))
{
Console.WriteLine(url);
UdpSocket.Close();
break;
}
}
}
}
}
我的主要目标是获取服务器的 url,当用户单击加载项时,导航到该 url。
如果我唯一的选择是使用像 Node.js 这样的外部 javascript 库,请用简单的语言向我解释如何引用该库以使我能够使用它的功能。