@@ -180,30 +180,42 @@ public class WebSocket : NSObject, NSStreamDelegate {
180
180
}
181
181
}
182
182
183
- ///write a string to the websocket. This sends it as a text frame.
184
- public func writeString( str: String ) {
183
+ /**
184
+ Write a string to the websocket. This sends it as a text frame.
185
+
186
+ If you supply a non-nil completion block, I will perform it when the write completes.
187
+ - parameter str: The string to write.
188
+ - parameter completion: The (optional) completion handler.
189
+ */
190
+ public func writeString( str: String , completion: ( ( ) -> ( ) ) ? = nil ) {
185
191
guard isConnected else { return }
186
- dequeueWrite ( str. dataUsingEncoding ( NSUTF8StringEncoding) !, code: . TextFrame)
192
+ dequeueWrite ( str. dataUsingEncoding ( NSUTF8StringEncoding) !, code: . TextFrame, writeCompletion : completion )
187
193
}
188
194
189
- ///write binary data to the websocket. This sends it as a binary frame.
190
- public func writeData( data: NSData ) {
195
+ /**
196
+ Write binary data to the websocket. This sends it as a binary frame.
197
+
198
+ If you supply a non-nil completion block, I will perform it when the write completes.
199
+ - parameter data: The data to write.
200
+ - parameter completion: The (optional) completion handler.
201
+ */
202
+ public func writeData( data: NSData , completion: ( ( ) -> ( ) ) ? = nil ) {
191
203
guard isConnected else { return }
192
- dequeueWrite ( data, code: . BinaryFrame)
204
+ dequeueWrite ( data, code: . BinaryFrame, writeCompletion : completion )
193
205
}
194
206
195
207
//write a ping to the websocket. This sends it as a control frame.
196
208
//yodel a sound to the planet. This sends it as an astroid. http://youtu.be/Eu5ZJELRiJ8?t=42s
197
- public func writePing( data: NSData ) {
209
+ public func writePing( data: NSData , completion : ( ( ) -> ( ) ) ? = nil ) {
198
210
guard isConnected else { return }
199
- dequeueWrite ( data, code: . Ping)
211
+ dequeueWrite ( data, code: . Ping, writeCompletion : completion )
200
212
}
201
213
202
214
//private method that starts the connection
203
215
private func createHTTPRequest( ) {
204
216
205
217
let urlRequest = CFHTTPMessageCreateRequest ( kCFAllocatorDefault, " GET " ,
206
- url, kCFHTTPVersion1_1) . takeRetainedValue ( )
218
+ url, kCFHTTPVersion1_1) . takeRetainedValue ( )
207
219
208
220
var port = url. port
209
221
if port == nil {
@@ -283,18 +295,18 @@ public class WebSocket : NSObject, NSStreamDelegate {
283
295
if let cipherSuites = self . enabledSSLCipherSuites {
284
296
if let sslContextIn = CFReadStreamCopyProperty ( inputStream, kCFStreamPropertySSLContext) as! SSLContextRef ? ,
285
297
sslContextOut = CFWriteStreamCopyProperty ( outputStream, kCFStreamPropertySSLContext) as! SSLContextRef ? {
286
- let resIn = SSLSetEnabledCiphers ( sslContextIn, cipherSuites, cipherSuites. count)
287
- let resOut = SSLSetEnabledCiphers ( sslContextOut, cipherSuites, cipherSuites. count)
288
- if resIn != errSecSuccess {
289
- let error = self . errorWithDetail ( " Error setting ingoing cypher suites " , code: UInt16 ( resIn) )
290
- disconnectStream ( error)
291
- return
292
- }
293
- if resOut != errSecSuccess {
294
- let error = self . errorWithDetail ( " Error setting outgoing cypher suites " , code: UInt16 ( resOut) )
295
- disconnectStream ( error)
296
- return
297
- }
298
+ let resIn = SSLSetEnabledCiphers ( sslContextIn, cipherSuites, cipherSuites. count)
299
+ let resOut = SSLSetEnabledCiphers ( sslContextOut, cipherSuites, cipherSuites. count)
300
+ if resIn != errSecSuccess {
301
+ let error = self . errorWithDetail ( " Error setting ingoing cypher suites " , code: UInt16 ( resIn) )
302
+ disconnectStream ( error)
303
+ return
304
+ }
305
+ if resOut != errSecSuccess {
306
+ let error = self . errorWithDetail ( " Error setting outgoing cypher suites " , code: UInt16 ( resOut) )
307
+ disconnectStream ( error)
308
+ return
309
+ }
298
310
}
299
311
}
300
312
CFReadStreamSetDispatchQueue( inStream, WebSocket . sharedWorkQueue)
@@ -428,7 +440,7 @@ public class WebSocket : NSObject, NSStreamDelegate {
428
440
}
429
441
case - 1 :
430
442
fragBuffer = NSData ( bytes: buffer, length: bufferLen)
431
- break //do nothing, we are going to collect more data
443
+ break //do nothing, we are going to collect more data
432
444
default :
433
445
doDisconnect ( errorWithDetail ( " Invalid HTTP upgrade " , code: UInt16 ( code) ) )
434
446
}
@@ -547,10 +559,10 @@ public class WebSocket : NSObject, NSStreamDelegate {
547
559
let isControlFrame = ( receivedOpcode == . ConnectionClose || receivedOpcode == . Ping)
548
560
if !isControlFrame && ( receivedOpcode != . BinaryFrame && receivedOpcode != . ContinueFrame &&
549
561
receivedOpcode != . TextFrame && receivedOpcode != . Pong) {
550
- let errCode = CloseCode . ProtocolError. rawValue
551
- doDisconnect ( errorWithDetail ( " unknown opcode: \( receivedOpcode) " , code: errCode) )
552
- writeError ( errCode)
553
- return
562
+ let errCode = CloseCode . ProtocolError. rawValue
563
+ doDisconnect ( errorWithDetail ( " unknown opcode: \( receivedOpcode) " , code: errCode) )
564
+ writeError ( errCode)
565
+ return
554
566
}
555
567
if isControlFrame && isFin == 0 {
556
568
let errCode = CloseCode . ProtocolError. rawValue
@@ -603,7 +615,7 @@ public class WebSocket : NSObject, NSStreamDelegate {
603
615
if dataLength > UInt64 ( bufferLen) {
604
616
len = UInt64 ( bufferLen- offset)
605
617
}
606
- var data : NSData !
618
+ let data : NSData
607
619
if len < 0 {
608
620
len = 0
609
621
data = NSData ( )
@@ -739,7 +751,7 @@ public class WebSocket : NSObject, NSStreamDelegate {
739
751
dequeueWrite ( NSData ( bytes: buffer, length: sizeof ( UInt16) ) , code: . ConnectionClose)
740
752
}
741
753
///used to write things to the stream
742
- private func dequeueWrite( data: NSData, code: OpCode) {
754
+ private func dequeueWrite( data: NSData, code: OpCode, writeCompletion : ( ( ) - > ( ) ) ? = nil ) {
743
755
writeQueue . addOperationWithBlock { [ weak self] in
744
756
//stream isn't ready, let's wait
745
757
guard let s = self else { return }
@@ -788,6 +800,12 @@ public class WebSocket : NSObject, NSStreamDelegate {
788
800
total += len
789
801
}
790
802
if total >= offset {
803
+ if let queue = self ? . queue, callback = writeCompletion {
804
+ dispatch_async ( queue) {
805
+ callback ( )
806
+ }
807
+ }
808
+
791
809
break
792
810
}
793
811
}
0 commit comments