2016-06-08 17 views
5

Ho un'applicazione in cui devo inviare dati al server utilizzando la connessione socket e leggere i dati che invierà dal server. Sto usando la classe AsyncSocket per scrivere e leggere i dati. Ho avuto successo nel scrivere dati sul server e il server può vedere i dati inviati dall'applicazione. Ora, il problema è che ogni volta che il server invia alcuni dati all'applicazione, allora come posso ricevere quei dati usando la classe AsyncSocket. Di seguito è il mio codice. Metto il metodo dei delegati per leggere i dati ma non ha mai chiamato.Come ricevere dati dal server al socket in iOS?

var socket = AsyncSocket() 

socket = AsyncSocket(delegate: self) 
self.socketConnect() 

func socketConnect() { 
     do { 
      try socket?.connectToHost("IP Address", onPort: 6968) 
     } catch _ as NSError { 

     } 
    } 



//MARK: - AsyncSocket Delegates method 

    func onSocket(sock: AsyncSocket!, didConnectToHost host: String!, port: UInt16) { 
     print("Connected to host : \(host) with Port : \(port)") 


     let alert:UIAlertController = UIAlertController(title: "Connected", message: "Host:\(host) ** Port:\(port)", preferredStyle:.Alert) 
     self.presentViewController(alert, animated: true, completion: nil) 
     let action:UIAlertAction = UIAlertAction(title: "Ok", style: .Default) { (UIAlertAction) -> Void in 
      print("Ok Pressed") 
     } 
     alert .addAction(action) 

     let dict = ["iUserId":"100","iRideId":"276","type":"client"] // For client side 
     var jsonString = NSString() 
     do { 
      let data = try NSJSONSerialization.dataWithJSONObject(dict , options: NSJSONWritingOptions.PrettyPrinted) 
      jsonString = NSString(data: data, encoding: NSUTF8StringEncoding)! 

     }catch let error as NSError { 
      print(error) 
     } 
     let reqData = jsonString.dataUsingEncoding(NSUTF8StringEncoding) 
     socket.writeData(reqData, withTimeout: 1.0, tag: 100) 

    } 

    func onSocket(sock: AsyncSocket!, didReadPartialDataOfLength partialLength: UInt, tag: Int) { 
     print("Read partial data") 
    } 

    func onSocket(sock: AsyncSocket!, didWriteDataWithTag tag: Int) { 
     print("Data write successfully") 
    } 

    func onSocket(sock: AsyncSocket!, didReadData data: NSData!, withTag tag: Int) { 
     print("Data read successfully") 
    } 

    func onSocket(sock: AsyncSocket!, willDisconnectWithError err: NSError!) { 
     print("Socket disconnect with error :\(err.description)") 
    } 

    func onSocket(sock: AsyncSocket!, didAcceptNewSocket newSocket: AsyncSocket!) { 
     print("Accept new socket") 
    } 
+0

Questa classe (quella delegata) eredita da 'NSObject'? http://stackoverflow.com/a/24225381/653513 –

risposta

1

Ho trovato risposta per me stesso. Quello che ho perso è che devo mettere la linea readdata quando il socket è collegato all'host. Quindi, dopo aver scritto questo metodo, verrà chiamato il metodo ReadData e in tale metodo dovrà scrivere anche un codice a riga singola che ho scritto nel metodo didConnectToHost.

func socket(sock: GCDAsyncSocket!, didConnectToHost host: String!, port: UInt16) { 
     print("Connected to host : \(host) with Port : \(port)") 
     socket.readDataWithTimeout(-1, tag: 0) 
    } 

func socket(sock: GCDAsyncSocket!, didReadData data: NSData!, withTag tag: Int) { 
     print("Data read successfully") 


     socket.readDataWithTimeout(-1, tag: 0) 
}