diff --git a/Sources/CWSDelegate.swift b/Sources/CWSDelegate.swift index 9533314..dddd9e9 100644 --- a/Sources/CWSDelegate.swift +++ b/Sources/CWSDelegate.swift @@ -26,16 +26,3 @@ import Foundation @objc optional func encode(message: Any?) -> Any? } - -extension CWSDelegate { - - /// Custom decode that user can implement - func decode(message: Any?) -> Any? { - return nil - } - - /// Custom encode that user can implement - func encode(message: Any?) -> Any? { - return nil - } -} diff --git a/Sources/CWSParser.swift b/Sources/CWSParser.swift index e7fcdba..4253496 100644 --- a/Sources/CWSParser.swift +++ b/Sources/CWSParser.swift @@ -117,20 +117,20 @@ extension CWSParser { private func handleP(with data: [Any]) { let channelName = String(describing: data[1]) - let decodedMessage = self.mSocket.delegate?.decode(message: data[2]) ?? data[2] + let decodedMessage = self.mSocket.delegate?.decode?(message: data[2]) ?? data[2] self.mSocket.getChannel(by: channelName)?.onMessage(data: decodedMessage) } private func handleE(with data: [Any]) { let event = String(describing: data[1]) - let decodedMessage = self.mSocket.delegate?.decode(message: data[2]) ?? data[2] + let decodedMessage = self.mSocket.delegate?.decode?(message: data[2]) ?? data[2] self.mSocket.emit(event: event, data: decodedMessage) } private func handleS(with data: [Any]) { switch String(describing: data[1]) { case "c": - let decodedMessage = self.mSocket.delegate?.decode(message: data[2]) ?? data[2] + let decodedMessage = self.mSocket.delegate?.decode?(message: data[2]) ?? data[2] guard let pingJSON = decodedMessage as? [String: Any] else { self.mSocket.delegate?.onError(error: CWSError.pingJSONCastError(decodedMessage)) return diff --git a/Sources/ClusterWS.swift b/Sources/ClusterWS.swift index 9e60f0e..835ce16 100644 --- a/Sources/ClusterWS.swift +++ b/Sources/ClusterWS.swift @@ -156,17 +156,17 @@ extension ClusterWS { extension ClusterWS { open func send(event: String, data: Any? = nil, type: MessageType) { - let customEncodedData = self.delegate?.encode(message: data) + let customEncodedData = self.delegate?.encode?(message: data) ?? data if self.mUseBinary { guard let encodedData = self.mParser.encode(event: event, - data: customEncodedData ?? data, + data: customEncodedData, type: type)?.data(using: .utf8) else { self.delegate?.onError(error: CWSError.JSONStringifyError(data)) return } self.mWebSocket?.send(encodedData) } else { - guard let anyData = self.mParser.encode(event: event, data: customEncodedData ?? data, type: type) else { + guard let anyData = self.mParser.encode(event: event, data: customEncodedData, type: type) else { self.delegate?.onError(error: CWSError.JSONStringifyError(data)) return } diff --git a/Sources/WebSocket.swift b/Sources/WebSocket.swift index dd1bc81..1635852 100644 --- a/Sources/WebSocket.swift +++ b/Sources/WebSocket.swift @@ -1024,7 +1024,7 @@ private class InnerWebSocket: Hashable { security = .none } - var path = CFURLCopyPath(req.url! as CFURL!) as String + var path = CFURLCopyPath(req.url! as CFURL?) as String if path == "" { path = "/" } @@ -1058,7 +1058,7 @@ private class InnerWebSocket: Hashable { var (rdo, wro) : (InputStream?, OutputStream?) var readStream: Unmanaged? var writeStream: Unmanaged? - CFStreamCreatePairWithSocketToHost(nil, addr[0] as CFString!, UInt32(Int(addr[1])!), &readStream, &writeStream); + CFStreamCreatePairWithSocketToHost(nil, addr[0] as CFString?, UInt32(Int(addr[1])!), &readStream, &writeStream); rdo = readStream!.takeRetainedValue() wro = writeStream!.takeRetainedValue() (rd, wr) = (rdo!, wro!) diff --git a/Tests/CWSChannelTests.swift b/Tests/CWSChannelTests.swift index ff8bc89..8103d14 100644 --- a/Tests/CWSChannelTests.swift +++ b/Tests/CWSChannelTests.swift @@ -9,13 +9,14 @@ import XCTest import ClusterWS_Client_Swift +//@testable import ClusterWS class CWSChannelTests: XCTestCase { var webSocket: ClusterWS! override func setUp() { super.setUp() // Put setup code here. This method is called before the invocation of each test method in the class. - self.webSocket = ClusterWS(url: "wss://localhost:8080") + self.webSocket = ClusterWS(url: "ws://localhost:8080") self.webSocket.setReconnection(autoReconnect: true) } diff --git a/Tests/CWSReconnectionTests.swift b/Tests/CWSReconnectionTests.swift index e9668f9..8acc4b1 100644 --- a/Tests/CWSReconnectionTests.swift +++ b/Tests/CWSReconnectionTests.swift @@ -9,12 +9,14 @@ import XCTest import ClusterWS_Client_Swift +//@testable import ClusterWS extension CWSReconnectionTests: CWSDelegate { + func onConnect() { print("Connected") } - func onDisconnect(code: Int?, reason: String?) { + func onDisconnect(code: Int, reason: String) { print("Disconnected") self.currentAttamts += 1 } @@ -45,12 +47,12 @@ class CWSReconnectionTests: XCTestCase { } private func initSocketWithWrongUrl() { - self.webSocket = ClusterWS(url: "wss://localhost:0000") + self.webSocket = ClusterWS(url: "ws://localhost:0000") self.webSocket.delegate = self } private func initSocketWithRightUrl() { - self.webSocket = ClusterWS(url: "wss://localhost:8080") + self.webSocket = ClusterWS(url: "ws://localhost:8080") self.webSocket.delegate = self } diff --git a/Tests/ClusterWSTests.swift b/Tests/ClusterWSTests.swift index 0156aa0..7c6e08f 100755 --- a/Tests/ClusterWSTests.swift +++ b/Tests/ClusterWSTests.swift @@ -7,8 +7,9 @@ // import XCTest +import ClusterWS_Client_Swift -@testable import ClusterWS +//@testable import ClusterWS class ClusterWSTests: XCTestCase { var webSocket: ClusterWS!