bump ws
This commit is contained in:
parent
0470647d87
commit
87e12fd284
@ -214,6 +214,7 @@ public final class SocketEngine: NSObject, SocketEngineSpec, WebSocketDelegate {
|
|||||||
let wsUrl = urlWebSocket + (sid == "" ? "" : "&sid=\(sid)")
|
let wsUrl = urlWebSocket + (sid == "" ? "" : "&sid=\(sid)")
|
||||||
|
|
||||||
ws = WebSocket(url: NSURL(string: wsUrl)!)
|
ws = WebSocket(url: NSURL(string: wsUrl)!)
|
||||||
|
|
||||||
if cookies != nil {
|
if cookies != nil {
|
||||||
let headers = NSHTTPCookie.requestHeaderFieldsWithCookies(cookies!)
|
let headers = NSHTTPCookie.requestHeaderFieldsWithCookies(cookies!)
|
||||||
for (key, value) in headers {
|
for (key, value) in headers {
|
||||||
@ -268,10 +269,8 @@ public final class SocketEngine: NSObject, SocketEngineSpec, WebSocketDelegate {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private func handleClose() {
|
private func handleClose(reason: String) {
|
||||||
if let client = client where polling == true {
|
client?.engineDidClose(reason)
|
||||||
client.engineDidClose("Disconnect")
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private func handleMessage(message: String) {
|
private func handleMessage(message: String) {
|
||||||
@ -419,7 +418,7 @@ public final class SocketEngine: NSObject, SocketEngineSpec, WebSocketDelegate {
|
|||||||
message.removeAtIndex(message.startIndex)
|
message.removeAtIndex(message.startIndex)
|
||||||
handleOpen(message)
|
handleOpen(message)
|
||||||
case .Close:
|
case .Close:
|
||||||
handleClose()
|
handleClose(message)
|
||||||
default:
|
default:
|
||||||
DefaultSocketLogger.Logger.log("Got unknown packet type", type: logType)
|
DefaultSocketLogger.Logger.log("Got unknown packet type", type: logType)
|
||||||
}
|
}
|
||||||
@ -540,11 +539,12 @@ extension SocketEngine {
|
|||||||
guard let this = self else {return}
|
guard let this = self else {return}
|
||||||
|
|
||||||
if err != nil || data == nil {
|
if err != nil || data == nil {
|
||||||
|
DefaultSocketLogger.Logger.error(err?.localizedDescription ?? "Error", type: this.logType)
|
||||||
|
|
||||||
if this.polling {
|
if this.polling {
|
||||||
this.handlePollingFailed(err?.localizedDescription ?? "Error")
|
this.handlePollingFailed(err?.localizedDescription ?? "Error")
|
||||||
} else {
|
|
||||||
DefaultSocketLogger.Logger.error(err?.localizedDescription ?? "Error", type: this.logType)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -602,12 +602,15 @@ extension SocketEngine {
|
|||||||
DefaultSocketLogger.Logger.log("POSTing: %@", type: logType, args: postStr)
|
DefaultSocketLogger.Logger.log("POSTing: %@", type: logType, args: postStr)
|
||||||
|
|
||||||
doRequest(req) {[weak self] data, res, err in
|
doRequest(req) {[weak self] data, res, err in
|
||||||
if let this = self {
|
guard let this = self else {return}
|
||||||
if err != nil && this.polling {
|
|
||||||
this.handlePollingFailed(err?.localizedDescription ?? "Error")
|
if err != nil {
|
||||||
return
|
|
||||||
} else if err != nil {
|
|
||||||
DefaultSocketLogger.Logger.error(err?.localizedDescription ?? "Error", type: this.logType)
|
DefaultSocketLogger.Logger.error(err?.localizedDescription ?? "Error", type: this.logType)
|
||||||
|
|
||||||
|
if this.polling {
|
||||||
|
this.handlePollingFailed(err?.localizedDescription ?? "Error")
|
||||||
|
}
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -621,7 +624,6 @@ extension SocketEngine {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
// We had packets waiting for send when we upgraded
|
// We had packets waiting for send when we upgraded
|
||||||
// Send them raw
|
// Send them raw
|
||||||
|
|||||||
@ -34,7 +34,7 @@ public class WebSocket : NSObject, NSStreamDelegate {
|
|||||||
//B-F reserved.
|
//B-F reserved.
|
||||||
}
|
}
|
||||||
|
|
||||||
enum CloseCode : UInt16 {
|
public enum CloseCode : UInt16 {
|
||||||
case Normal = 1000
|
case Normal = 1000
|
||||||
case GoingAway = 1001
|
case GoingAway = 1001
|
||||||
case ProtocolError = 1002
|
case ProtocolError = 1002
|
||||||
@ -47,6 +47,8 @@ public class WebSocket : NSObject, NSStreamDelegate {
|
|||||||
case MessageTooBig = 1009
|
case MessageTooBig = 1009
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static let ErrorDomain = "WebSocket"
|
||||||
|
|
||||||
enum InternalErrorCode : UInt16 {
|
enum InternalErrorCode : UInt16 {
|
||||||
// 0-999 WebSocket status codes not used
|
// 0-999 WebSocket status codes not used
|
||||||
case OutputStreamWriteError = 1
|
case OutputStreamWriteError = 1
|
||||||
@ -138,9 +140,29 @@ public class WebSocket : NSObject, NSStreamDelegate {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
///disconnect from the websocket server
|
/**
|
||||||
public func disconnect() {
|
Disconnect from the server. I send a Close control frame to the server, then expect the server to respond with a Close control frame and close the socket from its end. I notify my delegate once the socket has been closed.
|
||||||
|
|
||||||
|
If you supply a non-nil `forceTimeout`, I wait at most that long (in seconds) for the server to close the socket. After the timeout expires, I close the socket and notify my delegate.
|
||||||
|
|
||||||
|
If you supply a zero (or negative) `forceTimeout`, I immediately close the socket (without sending a Close control frame) and notify my delegate.
|
||||||
|
|
||||||
|
- Parameter forceTimeout: Maximum time to wait for the server to close the socket.
|
||||||
|
*/
|
||||||
|
public func disconnect(forceTimeout forceTimeout: NSTimeInterval? = nil) {
|
||||||
|
switch forceTimeout {
|
||||||
|
case .Some(let seconds) where seconds > 0:
|
||||||
|
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, Int64(seconds * Double(NSEC_PER_SEC))), queue, { [unowned self] in
|
||||||
|
self.disconnectStream(nil)
|
||||||
|
})
|
||||||
|
fallthrough
|
||||||
|
case .None:
|
||||||
writeError(CloseCode.Normal.rawValue)
|
writeError(CloseCode.Normal.rawValue)
|
||||||
|
|
||||||
|
default:
|
||||||
|
self.disconnectStream(nil)
|
||||||
|
break
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
///write a string to the websocket. This sends it as a text frame.
|
///write a string to the websocket. This sends it as a text frame.
|
||||||
@ -400,11 +422,12 @@ public class WebSocket : NSObject, NSStreamDelegate {
|
|||||||
}
|
}
|
||||||
if let cfHeaders = CFHTTPMessageCopyAllHeaderFields(response) {
|
if let cfHeaders = CFHTTPMessageCopyAllHeaderFields(response) {
|
||||||
let headers = cfHeaders.takeRetainedValue() as NSDictionary
|
let headers = cfHeaders.takeRetainedValue() as NSDictionary
|
||||||
let acceptKey = headers[headerWSAcceptName] as! NSString
|
if let acceptKey = headers[headerWSAcceptName] as? NSString {
|
||||||
if acceptKey.length > 0 {
|
if acceptKey.length > 0 {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -632,7 +655,7 @@ public class WebSocket : NSObject, NSStreamDelegate {
|
|||||||
private func errorWithDetail(detail: String, code: UInt16) -> NSError {
|
private func errorWithDetail(detail: String, code: UInt16) -> NSError {
|
||||||
var details = Dictionary<String,String>()
|
var details = Dictionary<String,String>()
|
||||||
details[NSLocalizedDescriptionKey] = detail
|
details[NSLocalizedDescriptionKey] = detail
|
||||||
return NSError(domain: "Websocket", code: Int(code), userInfo: details)
|
return NSError(domain: WebSocket.ErrorDomain, code: Int(code), userInfo: details)
|
||||||
}
|
}
|
||||||
|
|
||||||
///write a an error to the socket
|
///write a an error to the socket
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user