简体   繁体   English

使用GCDAsyncUdpSocket的iOS快速应用程序

[英]iOS swift application using GCDAsyncUdpSocket

I'm very new to building iOS apps. 我是开发iOS应用程序的新手。

I'm trying to make a class to receive udp multicast messages, but I can't seam to get it working at all... 我正在尝试创建一个类来接收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!");

    }
}

Can anyone see where I'm making a mistake? 有人可以看到我在哪里犯错吗? I'm getting the UDP socket was opened msg, but not receiving any packages. 我正在打开UDP套接字已打开msg,但未收到任何包。 I know they are being sent as I'm capturing them with wireshark. 我知道他们正在被发送,因为我正在用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. 任何建议都会有所帮助。

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. 通常,多播具有较低的TTL,除非将您的数据包遍历的路由器配置为允许它,否则TTL不能太高。

I was running into the same issue in my custom class. 我在自定义课程中遇到了同样的问题。 Simply add @obj public to your delegate functions. 只需将@obj public添加到您的委托函数中。 Now the delegate will be called properly. 现在将正确调用该代表。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM