diff --git a/XCode/Sources/Socket.swift b/XCode/Sources/Socket.swift index 590360c7..142764af 100644 --- a/XCode/Sources/Socket.swift +++ b/XCode/Sources/Socket.swift @@ -24,8 +24,13 @@ public enum SocketError: Error { // swiftlint: disable identifier_name open class Socket: Hashable, Equatable { - let socketFileDescriptor: Int32 - private var shutdown = false + public let socketFileDescriptor: Int32 + private var shutdown = false { + didSet { + if shutdown { didClose?() } + } + } + public var didClose: (() -> ())? public init(socketFileDescriptor: Int32) { self.socketFileDescriptor = socketFileDescriptor diff --git a/XCode/Sources/WebSockets.swift b/XCode/Sources/WebSockets.swift index a2d46e30..6961ca0b 100644 --- a/XCode/Sources/WebSockets.swift +++ b/XCode/Sources/WebSockets.swift @@ -150,6 +150,7 @@ public class WebSocketSession: Hashable, Equatable { public enum WsError: Error { case unknownOpCode(String), unMaskedFrame(String), protocolError(String), invalidUTF8(String) } public enum OpCode: UInt8 { case `continue` = 0x00, close = 0x08, ping = 0x09, pong = 0x0A, text = 0x01, binary = 0x02 } public enum Control: Error { case close } + public var shouldCloseSocket = true public class Frame { public var opcode = OpCode.close @@ -167,8 +168,10 @@ public class WebSocketSession: Hashable, Equatable { } deinit { - writeCloseFrame() - socket.close() + if shouldCloseSocket { + writeCloseFrame() + socket.close() + } } public func writeText(_ text: String) {