0

I'm trying to build a chat application, but I do know how to integrate socket I/O in Swift. I am following this tutorial.

  1. I'm adding a POD file
  2. Open xcodeworkspace file
  3. Import sockekIO file

This is my code image I am getting like this error

How do I know my socketIO connected to the server? I am not sure.

import UIKit
import SocketIO

class ViewController: UIViewController {

    override func viewDidLoad() {
        let socket = SocketIOClient(socketURL: NSURL(string: "http://localhost:8080")!)
        socket.on("connect") { _, _ in
            print("socket connected")
            socket.emit("ping", "data")
        }
        socket.on("ping") { _, _ in
            print("ping received")
        }
        socket.connect()
    }
}
hiyan
  • 1
  • 4

1 Answers1

0

You are supposed to use a SocketManager (which creates and manages clients). Here is an example in it's most basic form:

let socketManager = SocketManager(socketURL: URL(string: "http://localhost:8080")!)
let socket = socketManager.defaultSocket

socket.on("connect") { _, _ in
    print("socket connected")
    socket.emit("ping", "data")
}

socket.on("ping") { _, _ in
    print("ping received")
}

socketManager.connect()
Alladinian
  • 34,483
  • 6
  • 89
  • 91
  • This coding working..and I have doubt one..I'm copy and paste that code that working..but my console not show SocketConnected ..how do I'm found it my socket is connected or not connected – hiyan Apr 27 '18 at 13:11
  • 2018-04-27 18:42:24.368236+0530 iosswift[8414:298555] [MC] Loaded MobileCoreServices.framework. 2018-04-27 18:42:25.467831+0530 iosswift[8414:298676] TIC TCP Conn Failed [1:0x604000362280]: 1:61 Err(61) 2018-04-27 18:42:25.468546+0530 iosswift[8414:298676] Task <565F8588-6B28-4521-9BB7-5341F491B380>.<1> HTTP load failed (error code: -1004 [1:61]) 2018-04-27 18:42:25.494718+0530 iosswift[8414:298679] Task <565F8588-6B28-4521-9BB7-5341F491B380>.<1> finished with error - code: -1004 – hiyan Apr 27 '18 at 13:14
  • Make sure that you're holding a strong reference to socketManager in your controller, that your localhost server is listening and finally that your application is configured to allow http calls ([see this answer](https://stackoverflow.com/questions/30731785/how-do-i-load-an-http-url-with-app-transport-security-enabled-in-ios-9?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa)) – Alladinian Apr 27 '18 at 13:44