Skip to content
This repository has been archived by the owner on Nov 2, 2020. It is now read-only.

Commit

Permalink
Fixed warnings and optional delegate methods
Browse files Browse the repository at this point in the history
  • Loading branch information
romanbaitaliuk committed May 31, 2018
1 parent 43615e5 commit a2e6ad7
Show file tree
Hide file tree
Showing 7 changed files with 17 additions and 26 deletions.
13 changes: 0 additions & 13 deletions Sources/CWSDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
6 changes: 3 additions & 3 deletions Sources/CWSParser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions Sources/ClusterWS.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
4 changes: 2 additions & 2 deletions Sources/WebSocket.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 = "/"
}
Expand Down Expand Up @@ -1058,7 +1058,7 @@ private class InnerWebSocket: Hashable {
var (rdo, wro) : (InputStream?, OutputStream?)
var readStream: Unmanaged<CFReadStream>?
var writeStream: Unmanaged<CFWriteStream>?
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!)
Expand Down
3 changes: 2 additions & 1 deletion Tests/CWSChannelTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down
8 changes: 5 additions & 3 deletions Tests/CWSReconnectionTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}

Expand Down
3 changes: 2 additions & 1 deletion Tests/ClusterWSTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@
//

import XCTest
import ClusterWS_Client_Swift

@testable import ClusterWS
//@testable import ClusterWS
class ClusterWSTests: XCTestCase {
var webSocket: ClusterWS!

Expand Down

0 comments on commit a2e6ad7

Please sign in to comment.