2

我对构建 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 套接字已打开消息,但没有收到任何包。我知道它们正在被发送,因为我正在用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


    }

任何建议都会有所帮助。

4

2 回答 2

0

您的接收器是否与广播公司在同一网络上。通常,多播具有较低的 TTL,除非您的数据包遍历的路由器配置为允许它,否则它不会太远。

于 2015-04-16T23:19:09.273 回答
0

我在自定义课程中遇到了同样的问题。只需将 @obj public 添加到您的委托函数中。现在委托将被正确调用。

于 2015-12-22T19:15:44.710 回答