2

I'm very new to building iOS apps.

I'm trying to make a class to receive udp multicast messages, but I can't seam to get it working at all...

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!");

    }
}

Can anyone see where I'm making a mistake? I'm getting the UDP socket was opened msg, but not receiving any packages. I know they are being sent as I'm capturing them with wireshark.

The discovery is called from my view controller

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


    }

Any suggestion will be helpful.

cholewa1992
  • 863
  • 1
  • 7
  • 16
  • Try implementing the `udpSocketDidClose` delegate method to be sure your socket hasn't closed. – Ralfonso Apr 16 '15 at 18:39
  • I'm not why it works, but when I moved the code to the already existing ViewController it worked. Do the delegate have to be the actually running view controller to work? – cholewa1992 Apr 16 '15 at 19:27
  • The delegate only has to be resident in memory to receive the callbacks. Maybe you didn't have an instance of `Discovery` initialized yet? If you post more of your code for context, I can try to explain a little better. – Ralfonso Apr 16 '15 at 19:30
  • @Ralfonso - If posted the snippet where i call discovery. Thanks! :-) – cholewa1992 Apr 16 '15 at 19:41
  • I must admit, I don't see what's wrong at first blush even with the additional code. I'll try to replicate it later on and see if I can identify the problem. Interesting bug! – Ralfonso Apr 16 '15 at 19:55

2 Answers2

0

Is your receiver on the same network as the broadcaster. Generally multicast has a low TTL and can't make it too far unless the routers your packets traverse are configured to allow it.

Vinnyt
  • 75
  • 6
0

I was running into the same issue in my custom class. Simply add @obj public to your delegate functions. Now the delegate will be called properly.