Propagate http header callback from websocket back to client.

This commit is contained in:
Valentin Slawicek 2018-10-17 21:38:19 +02:00
parent 75057023cb
commit 5a6a4f02d9
4 changed files with 38 additions and 1 deletions

View File

@ -344,4 +344,16 @@ public enum SocketClientEvent : String {
/// } /// }
/// ``` /// ```
case statusChange case statusChange
/// Emitted when when upgrading the http connection to a websocket connection.
///
/// Usage:
///
/// ```swift
/// socket.on(clientEvent: .websocketUpgrade) {data, ack in
/// let headers = (data as [Any])[0]
/// // Some header logic
/// }
/// ```
case websocketUpgrade
} }

View File

@ -313,6 +313,12 @@ open class SocketEngine : NSObject, URLSessionDelegate, SocketEnginePollable, So
this.parseEngineMessage(message) this.parseEngineMessage(message)
} }
ws?.onHttpResponseHeaders = {[weak self] headers in
guard let this = self else { return }
this.client?.engineDidWebsocketUpgrade(headers: headers)
}
ws?.connect() ws?.connect()
} }
@ -666,7 +672,9 @@ open class SocketEngine : NSObject, URLSessionDelegate, SocketEnginePollable, So
connected = false connected = false
polling = true polling = true
if let reason = error?.localizedDescription { if let error = error as? WSError {
didError(reason: "\(error.message). code=\(error.code), type=\(error.type)")
} else if let reason = error?.localizedDescription {
didError(reason: reason) didError(reason: reason)
} else { } else {
client?.engineDidClose(reason: "Socket Disconnected") client?.engineDidClose(reason: "Socket Disconnected")

View File

@ -59,4 +59,9 @@ import Foundation
/// ///
/// - parameter data: The data the engine received. /// - parameter data: The data the engine received.
func parseEngineBinaryData(_ data: Data) func parseEngineBinaryData(_ data: Data)
/// Called when when upgrading the http connection to a websocket connection.
///
/// - parameter headers: The http headers.
func engineDidWebsocketUpgrade(headers: [String: String])
} }

View File

@ -377,6 +377,18 @@ open class SocketManager : NSObject, SocketManagerSpec, SocketParsable, SocketDa
} }
} }
/// Called when when upgrading the http connection to a websocket connection.
///
/// - parameter headers: The http headers.
open func engineDidWebsocketUpgrade(headers: [String: String]) {
handleQueue.async {
self._engineDidWebsocketUpgrade(headers: headers)
}
}
private func _engineDidWebsocketUpgrade(headers: [String: String]) {
emitAll(clientEvent: .websocketUpgrade, data: [headers])
}
/// Called when the engine has a message that must be parsed. /// Called when the engine has a message that must be parsed.
/// ///
/// - parameter msg: The message that needs parsing. /// - parameter msg: The message that needs parsing.