Skip to content

Commit ed3d490

Browse files
committed
bump websocket version
1 parent 4ee196c commit ed3d490

File tree

3 files changed

+53
-29
lines changed

3 files changed

+53
-29
lines changed

SocketIO-MacTests/SocketParserTest.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ class SocketParserTest: XCTestCase {
2525
"0/swift": ("/swift", [], [], -1),
2626
"1/swift": ("/swift", [], [], -1),
2727
"4\"ERROR\"": ("/", ["ERROR"], [], -1),
28+
"4{\"test\":2}": ("/", [["test": 2]], [], -1),
2829
"41": ("/", [1], [], -1)]
2930

3031
func testDisconnect() {
@@ -87,6 +88,11 @@ class SocketParserTest: XCTestCase {
8788
validateParseResult(message)
8889
}
8990

91+
func testErrorTypeDictionary() {
92+
let message = "4{\"test\":2}"
93+
validateParseResult(message)
94+
}
95+
9096
func testErrorTypeInt() {
9197
let message = "41"
9298
validateParseResult(message)

Source/SocketParsable.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ extension SocketParsable {
116116

117117
switch parseData(noPlaceholders) {
118118
case let .Left(err):
119-
// If first you don't succeed, try again
119+
// Errors aren't always enclosed in an array
120120
if case let .Right(data) = parseData("\([noPlaceholders as AnyObject])") {
121121
return .Right(SocketPacket(type: type, data: data, id: Int(idString) ?? -1,
122122
nsp: namespace, placeholders: placeholders))

Source/WebSocket.swift

Lines changed: 46 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -180,30 +180,42 @@ public class WebSocket : NSObject, NSStreamDelegate {
180180
}
181181
}
182182

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) {
185191
guard isConnected else { return }
186-
dequeueWrite(str.dataUsingEncoding(NSUTF8StringEncoding)!, code: .TextFrame)
192+
dequeueWrite(str.dataUsingEncoding(NSUTF8StringEncoding)!, code: .TextFrame, writeCompletion: completion)
187193
}
188194

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) {
191203
guard isConnected else { return }
192-
dequeueWrite(data, code: .BinaryFrame)
204+
dequeueWrite(data, code: .BinaryFrame, writeCompletion: completion)
193205
}
194206

195207
//write a ping to the websocket. This sends it as a control frame.
196208
//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) {
198210
guard isConnected else { return }
199-
dequeueWrite(data, code: .Ping)
211+
dequeueWrite(data, code: .Ping, writeCompletion: completion)
200212
}
201213

202214
//private method that starts the connection
203215
private func createHTTPRequest() {
204216

205217
let urlRequest = CFHTTPMessageCreateRequest(kCFAllocatorDefault, "GET",
206-
url, kCFHTTPVersion1_1).takeRetainedValue()
218+
url, kCFHTTPVersion1_1).takeRetainedValue()
207219

208220
var port = url.port
209221
if port == nil {
@@ -283,18 +295,18 @@ public class WebSocket : NSObject, NSStreamDelegate {
283295
if let cipherSuites = self.enabledSSLCipherSuites {
284296
if let sslContextIn = CFReadStreamCopyProperty(inputStream, kCFStreamPropertySSLContext) as! SSLContextRef?,
285297
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+
}
298310
}
299311
}
300312
CFReadStreamSetDispatchQueue(inStream, WebSocket.sharedWorkQueue)
@@ -428,7 +440,7 @@ public class WebSocket : NSObject, NSStreamDelegate {
428440
}
429441
case -1:
430442
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
432444
default:
433445
doDisconnect(errorWithDetail("Invalid HTTP upgrade", code: UInt16(code)))
434446
}
@@ -547,10 +559,10 @@ public class WebSocket : NSObject, NSStreamDelegate {
547559
let isControlFrame = (receivedOpcode == .ConnectionClose || receivedOpcode == .Ping)
548560
if !isControlFrame && (receivedOpcode != .BinaryFrame && receivedOpcode != .ContinueFrame &&
549561
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
554566
}
555567
if isControlFrame && isFin == 0 {
556568
let errCode = CloseCode.ProtocolError.rawValue
@@ -603,7 +615,7 @@ public class WebSocket : NSObject, NSStreamDelegate {
603615
if dataLength > UInt64(bufferLen) {
604616
len = UInt64(bufferLen-offset)
605617
}
606-
var data: NSData!
618+
let data: NSData
607619
if len < 0 {
608620
len = 0
609621
data = NSData()
@@ -739,7 +751,7 @@ public class WebSocket : NSObject, NSStreamDelegate {
739751
dequeueWrite(NSData(bytes: buffer, length: sizeof(UInt16)), code: .ConnectionClose)
740752
}
741753
///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) {
743755
writeQueue.addOperationWithBlock { [weak self] in
744756
//stream isn't ready, let's wait
745757
guard let s = self else { return }
@@ -788,6 +800,12 @@ public class WebSocket : NSObject, NSStreamDelegate {
788800
total += len
789801
}
790802
if total >= offset {
803+
if let queue = self?.queue, callback = writeCompletion {
804+
dispatch_async(queue) {
805+
callback()
806+
}
807+
}
808+
791809
break
792810
}
793811
}

0 commit comments

Comments
 (0)