Skip to content

Commit

Permalink
Merge pull request #259 from robinkunde/master
Browse files Browse the repository at this point in the history
Improve clarity of asyncAfter code and other timeout
  • Loading branch information
daltoniam authored Sep 30, 2016
2 parents 9d32864 + 24e32a7 commit b0fa08c
Showing 1 changed file with 3 additions and 2 deletions.
5 changes: 3 additions & 2 deletions Source/WebSocket.swift
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,8 @@ public class WebSocket : NSObject, StreamDelegate {
public func disconnect(forceTimeout: TimeInterval? = nil, closeCode: UInt16 = CloseCode.normal.rawValue) {
switch forceTimeout {
case .some(let seconds) where seconds > 0:
callbackQueue.asyncAfter(deadline: DispatchTime.now() + Double(Int64(seconds * Double(NSEC_PER_SEC))) / Double(NSEC_PER_SEC)) { [weak self] in
let milliseconds = Int(seconds * 1_000)
callbackQueue.asyncAfter(deadline: .now() + .milliseconds(milliseconds)) { [weak self] in
self?.disconnectStream(nil)
}
fallthrough
Expand Down Expand Up @@ -365,7 +366,7 @@ public class WebSocket : NSObject, StreamDelegate {
self.mutex.unlock()

let bytes = UnsafeRawPointer((data as NSData).bytes).assumingMemoryBound(to: UInt8.self)
var out = timeout * 1000000 // wait 5 seconds before giving up
var out = timeout * 1_000_000 // wait 5 seconds before giving up
writeQueue.addOperation { [weak self] in
while !outStream.hasSpaceAvailable {
usleep(100) // wait until the socket is ready
Expand Down

0 comments on commit b0fa08c

Please sign in to comment.