0
我要發送的MSearch請求我的DLNA服務器,並使用Javascript獲得響應。我已經在C#中實現了這些代碼,但是我在寫一個Chrome加載項(Html,JS,CSS)後,我在JavaScript中需要它。 問題是我不知道Js,我已經閱讀了Node.js文檔,但無法理解它。廣播消息,並使用Javascript獲取響應(UPnP的M-SEARCH)
這是我的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。
如果我唯一的選擇是使用外部JavaScript庫像Node.js的,請給我解釋一下簡單的語言如何有refrence圖書館,使我能夠使用它的功能。