我是构建iOS应用程序的新手。
我正在尝试创建一个接收udp多播消息的类,但是我无法接通它以使其正常工作...
class Discovery : GCDAsyncUdpSocketDelegate{
var udpSocket:GCDAsyncUdpSocket!;
init() {
udpSocket = GCDAsyncUdpSocket(delegate: self, delegateQueue: dispatch_get_main_queue())
var e:NSErrorPointer = nil;
//Binding to port
if(!udpSocket.bindToPort(2025, error: e)){
println(e);
return;
}
//Joining multicast group
if(!udpSocket.joinMulticastGroup("239.5.6.7", error: e)){
println(e);
return;
}
//Begin recieve
if(!udpSocket.beginReceiving(e)){
println(e);
return;
}
println("UDP socket was opened!")
}
func udpSocket(sock: GCDAsyncUdpSocket!, didReceiveData data: NSData!, fromAddress address: NSData!, withFilterContext filterContext: AnyObject!) {
println("Got data!");
}
}
谁能看到我犯错的地方?我得到UDP套接字已打开msg,但没有收到任何软件包。我知道他们正在被发送,因为我正在使用wireshark捕获它们。
从我的视图控制器调用发现
class ViewController: UIViewController, CLLocationManagerDelegate {
let peer = Peer(id: NSUUID.new())
let uuid = NSUUID.new()
let discovery:Discovery = Discovery()
let locationManager = CLLocationManager()
let region = CLBeaconRegion(proximityUUID: NSUUID(UUIDString: "8DEEFBB9-F738-4297-8040-96668BB44281"), identifier: "Roximity")
let com = OconCom()
override func viewDidLoad() {
super.viewDidLoad()
locationManager.delegate = self;
if (CLLocationManager.authorizationStatus() != CLAuthorizationStatus.AuthorizedAlways) {
locationManager.requestAlwaysAuthorization()
}
locationManager.startMonitoringForRegion(region) //Background
locationManager.startRangingBeaconsInRegion(region) //Foreground
}
任何建议都会有所帮助。
答案 0 :(得分:0)
您的接收器是否与广播公司处于同一网络。通常,多播具有较低的TTL,并且除非您的数据包遍历的路由器被配置为允许它,否则不能太远。
答案 1 :(得分:0)
我在自定义类中遇到了同样的问题。只需将@obj public添加到您的委托函数中即可。现在代理人将被正确调用。