BLWebSocketsServer icon indicating copy to clipboard operation
BLWebSocketsServer copied to clipboard

10.9 Compatibility

Open bdkjones opened this issue 11 years ago • 3 comments

Hi Ben,

I talked with an Apple Engineer today about the effects that the new App Nap API will have on background apps in 10.9. Essentially, the system is going to "intelligently" throttle the resources available to apps that are in the background so that they are de-prioritized on the CPU.

According to this engineer, this will primarily affect NSTimers and polling operations. The reason I bring this up is that BLWebSocketsServer uses polling. Given that 10.9 is going to drop the hammer on apps that use polling, I figured it would be a good time to get this library off of it, if possible.

I understand your class pretty well, but I'm not familiar with libwebsockets and the associated API. So forgive me if this is a dumb question, but why is the polling necessary? You're just checking to see if "messagesCount" is greater than 0, then calling down to libwebsockets. Why not use KVO to simply observe changes to the "messagesCount" property? When it changes, check it, if it's greater than 0, fire the call to libwebsockets. Alternately, have the -enqueueMessage methods trigger a call to libwebsockets once they complete.

I thought of modifying your code to try this, but then I figured you must have had a good reason to fall back on polling, so I'm probably missing some idiosyncrasy with libsockets.

bdkjones avatar Jun 21 '13 05:06 bdkjones

Hi Brian,

Actually moving away from polling is one of my priorities, even more now that you tell me how the App Nap API could affect apps using polling. It was the first step to get something up and running wrapping libwebsockets and even if it works very well, polling is not efficient.

I have actually started the transition by moving away from an infinite run loop polling libwebsockets in a separate thread, and replacing it with GCD dispatch sources which will schedule a task in a queue every time the dispatch source sends a signal. I'm making the transition in the dispatch_sources branch, right now I use a dispatch timer as a dispatch source:

self.timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, self.networkQueue);

    dispatch_source_set_timer(self.timer,DISPATCH_TIME_NOW, pollingInterval*NSEC_PER_USEC, (pollingInterval/2)*NSEC_PER_USEC);

    dispatch_source_set_event_handler(self.timer, ^{
        @autoreleasepool {
            libwebsocket_service(self.context, 0);
        }
    });

So it is still polling as the dispatch source is a timer but now we "just" have to replace the timer dispatch source with, for instance, a file descriptor dispatch source to have an event based server. This is the next step on my Todo but don't hesitate if you want to give it a shot ;-)

benlodotcom avatar Jun 24 '13 22:06 benlodotcom

So BLWebSocketsServer DOES support OSX?

zakdances avatar Aug 24 '13 06:08 zakdances

I haven't tried it myself but it should with maybe small tweaks

benlodotcom avatar Oct 31 '13 06:10 benlodotcom