0

I have a swift command line application which uses socket.io to communicate with a server. The issue is that the library that I use is asynchronous and as such once the application is done connecting and enters the loop, the event handlers in the main thread are never executed.

I understand I need to use some form of GDC, but most example on here only deal with synchronous processing after an asynchronous call. But what I want to do, is just leave the main thread free to handle events from workers.

Here is a naive approach that leaves the main thread unable to process handlers:

import Foundation

let socket = SocketIOClient(socketURL: "localhost:3000", options: [.Log(true), .ForceWebsockets(true)])

socket.on("connect") {data, ack in
    print("socket connected")
}

socket.on("config") {data, ack in
    print("got my config")
}

socket.connect()

while (true){
    sleep(1)
}

Now, how would I go about making a runloop, or using GDC to make it possible for the handlers to be executed?

oLeduc
  • 313
  • 1
  • 12
  • The answers to the referenced question should solve your problem. It not, let me know and I'll reopen the question. – Martin R Dec 30 '15 at 20:07
  • This seems like this is exactly what I was looking for, not quite sure how I missed that answer. Thank you. – oLeduc Dec 30 '15 at 20:37

0 Answers0