这个问题可能不需要这么具体,但是我正在研究一个正在寻找UPnP设备的项目(效果很好).
This question may not need to be so specific, but I am working on a project that is searching for UPnP devices (working great).
我这样得到数据;
func udpSocket(sock: GCDAsyncUdpSocket!, didReceiveData data: NSData!, fromAddress address: NSData!, withFilterContext filterContext: AnyObject!) { var host: NSString? var port1: UInt16 = 0 GCDAsyncUdpSocket.getHost(&host, port: &port1, fromAddress: address) //print(host) let response: NSString = NSString(data: data!, encoding: NSUTF8StringEncoding)! print(response) }数据响应作为NSString返回,看起来像这样;
The data response is returned as a NSString, and looks like so;
HTTP/1.1 200 OK CACHE-CONTROL: max-age=120 DATE: Sun, 31 Jan 2016 22:47:45 GMT ST: upnp:rootdevice USN: uuid:f7a65ab0-f527-4b85-a8ad-68104aa2b322::upnp:rootdevice EXT: SERVER: Linux/BHR4 UPnP/1.1 MiniUPnPd/1.8 LOCATION: 192.168.1.1:49420/rootDesc.xml OPT: "schemas.upnp/upnp/1/0/"; ns=01 01-NLS: 1 BOOTID.UPNP.ORG: 1 CONFIGID.UPNP.ORG: 1337 HTTP/1.1 200 OK Cache-Control: max-age=600 EXT: Location: 192.168.1.1:1901/root.xml SECURELOCATION.UPNP.ORG: 192.168.1.1:1902/root.xml Server: Linux/3.4 UPnP/2.0 bhr4/1.2 ST: upnp:rootdevice USN: uuid:9518ecfc-cf2f-57e4-bb23-5182aa6a23cd::upnp:rootdevice DATE: Wed, 19 Jan 2000 02:39:56 GMT BOOTID.UPNP.ORG: 1 HTTP/1.1 200 OK Cache-Control: max-age=600 EXT: Location: 192.168.1.1:1901/root.xml SECURELOCATION.UPNP.ORG: 192.168.1.1:1902/root.xml ST: urn:schemas-upnp-org:device:ManageableDevice:2 USN: uuid:9518acfc-cf2f-57e4-cc23-5a12aa6a23cd::urn:schemas-upnp-org:device:ManageableDevice:2 Server: Linux/3.4 UPnP/2.0 bhr4/1.2 DATE: Wed, 19 Jan 2000 02:39:56 GMT BOOTID.UPNP.ORG: 1我对如何解析XML或JSON响应有一定的了解,但是由于该响应似乎只是一个很长的字符串,因此我试图弄清楚如何将数据解析为字典,从而可以执行以下操作:
I have some understanding of how to parse XML or JSON responses, but being that this response seems to just be a lengthy string, I am trying to figure out how I could parse the data into a dictionary so I could do a;
for item in response { print(item.location) }
for item in response { print(item.location) }
这不一定特定于UPnP,而是要把握的总体前提.
This is not necessarily specific to UPnP, but an overall premise to grasp.
推荐答案不久前,我正在开发一个快速的UPNP库. 这是我使用正则表达式的一种方式: github/ambientlight/ambientUPNP/blob/master/ambientUPNP /SSDPMessage.swift
I was working on a swift UPNP library not so long ago. Here is one way how I did it with regular expressions: github/ambientlight/ambientUPNP/blob/master/ambientUPNP/SSDPMessage.swift
您可以用来解析SSDP消息,但是对于UPNP GENA事件,则不应使用此方法,因为它们是通过TCP进行的,因此,您具有一个流套接字,应将其作为流读取.在这里看看: github/ambientlight/ambientUPNP/blob/master /ambientUPNP/components/HTTPInternals.swift
You can use for parsing SSDP messages, however for UPNP GENA events you should not use this approach since they are over TCP, and therefore you have a stream-socket which you should read as a stream accordingly. Take a look here: github/ambientlight/ambientUPNP/blob/master/ambientUPNP/components/HTTPInternals.swift
参考(从UDP套接字读取-固定长度): github/ambientlight/ambientUPNP/blob/master/ambientUPNP /SSDPServer.swift
self.readMulticastSocket = try PosixInternals.initMulticastUDPSocket(SSDPDefaultPort, multicastAddress: SSDPMulticastAddress) let readMulticastSource = dispatch_source_create(DISPATCH_SOURCE_TYPE_READ, UInt(readMulticastSocket), 0, _readMulticastQueue) dispatch_source_set_event_handler(readMulticastSource) { do { let (data, senderAddress) = try PosixInternals.recvData(self.readMulticastSocket, readSize: SSDPDefaultReadSize) let message = try SSDPMessage.messageWithDataAndAddress(data, senderAddress: senderAddress) //print("\(message)\n") self._processMessage(message) } catch { print(error) } }更多推荐
在Swift中解析UPnP响应?
发布评论