Use callbacks instead of delegate methods for Starscream
This commit is contained in:
parent
d811b194eb
commit
ef02247a0c
@ -280,7 +280,6 @@ public final class SocketEngine : NSObject, URLSessionDelegate, SocketEnginePoll
|
|||||||
}
|
}
|
||||||
|
|
||||||
private func createWebSocketAndConnect() {
|
private func createWebSocketAndConnect() {
|
||||||
ws?.delegate = nil // TODO this seems a bit defensive, is this really needed?
|
|
||||||
var req = URLRequest(url: urlWebSocketWithSid)
|
var req = URLRequest(url: urlWebSocketWithSid)
|
||||||
|
|
||||||
addHeaders(to: &req)
|
addHeaders(to: &req)
|
||||||
@ -288,10 +287,33 @@ public final class SocketEngine : NSObject, URLSessionDelegate, SocketEnginePoll
|
|||||||
ws = WebSocket(request: req)
|
ws = WebSocket(request: req)
|
||||||
ws?.callbackQueue = engineQueue
|
ws?.callbackQueue = engineQueue
|
||||||
ws?.enableCompression = compress
|
ws?.enableCompression = compress
|
||||||
ws?.delegate = self
|
|
||||||
ws?.disableSSLCertValidation = selfSigned
|
ws?.disableSSLCertValidation = selfSigned
|
||||||
ws?.security = security?.security
|
ws?.security = security?.security
|
||||||
|
|
||||||
|
ws?.onConnect = {[weak self] in
|
||||||
|
guard let this = self else { return }
|
||||||
|
|
||||||
|
this.websocketDidConnect()
|
||||||
|
}
|
||||||
|
|
||||||
|
ws?.onDisconnect = {[weak self] error in
|
||||||
|
guard let this = self else { return }
|
||||||
|
|
||||||
|
this.websocketDidDisconnect(error: error)
|
||||||
|
}
|
||||||
|
|
||||||
|
ws?.onData = {[weak self] data in
|
||||||
|
guard let this = self else { return }
|
||||||
|
|
||||||
|
this.parseEngineData(data)
|
||||||
|
}
|
||||||
|
|
||||||
|
ws?.onText = {[weak self] message in
|
||||||
|
guard let this = self else { return }
|
||||||
|
|
||||||
|
this.parseEngineMessage(message)
|
||||||
|
}
|
||||||
|
|
||||||
ws?.connect()
|
ws?.connect()
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -607,10 +629,9 @@ public final class SocketEngine : NSObject, URLSessionDelegate, SocketEnginePoll
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// MARK: Starscream delegate conformance
|
// MARK: WebSocket Methods
|
||||||
|
|
||||||
/// Delegate method for connection.
|
private func websocketDidConnect() {
|
||||||
public func websocketDidConnect(socket: WebSocketClient) {
|
|
||||||
if !forceWebsockets {
|
if !forceWebsockets {
|
||||||
probing = true
|
probing = true
|
||||||
probeWebSocket()
|
probeWebSocket()
|
||||||
@ -621,8 +642,7 @@ public final class SocketEngine : NSObject, URLSessionDelegate, SocketEnginePoll
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Delegate method for disconnection.
|
private func websocketDidDisconnect(error: Error?) {
|
||||||
public func websocketDidDisconnect(socket: WebSocketClient, error: Error?) {
|
|
||||||
probing = false
|
probing = false
|
||||||
|
|
||||||
if closed {
|
if closed {
|
||||||
|
|||||||
@ -27,7 +27,7 @@ import Foundation
|
|||||||
import Starscream
|
import Starscream
|
||||||
|
|
||||||
/// Protocol that is used to implement socket.io WebSocket support
|
/// Protocol that is used to implement socket.io WebSocket support
|
||||||
public protocol SocketEngineWebsocket : SocketEngineSpec, WebSocketDelegate {
|
public protocol SocketEngineWebsocket : SocketEngineSpec {
|
||||||
// MARK: Methods
|
// MARK: Methods
|
||||||
|
|
||||||
/// Sends an engine.io message through the WebSocket transport.
|
/// Sends an engine.io message through the WebSocket transport.
|
||||||
@ -66,16 +66,4 @@ extension SocketEngineWebsocket {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// MARK: Starscream delegate methods
|
|
||||||
|
|
||||||
/// Delegate method for when a message is received.
|
|
||||||
public func websocketDidReceiveMessage(socket: WebSocketClient, text: String) {
|
|
||||||
parseEngineMessage(text)
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Delegate method for when binary is received.
|
|
||||||
public func websocketDidReceiveData(socket: WebSocketClient, data: Data) {
|
|
||||||
parseEngineData(data)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user