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

Commit 1ef5786

Browse files
Added iOS 8 support, tvOS - 9.0 and macOS - 10.9. Created new tests
1 parent 90cc20d commit 1ef5786

File tree

8 files changed

+252
-65
lines changed

8 files changed

+252
-65
lines changed

ClusterWS-Client-Swift.podspec

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Pod::Spec.new do |s|
22
s.name = 'ClusterWS-Client-Swift'
3-
s.version = '2.0.7'
3+
s.version = '2.0.8'
44
s.summary = 'Swift Client for ClusterWS'
55
s.description = 'Swift Client for ClusterWS - lightweight, fast and powerful framework for building horizontally & vertically scalable WebSocket applications in Node.js'
66
s.homepage = 'https://github.com/ClusterWS/ClusterWS-Client-Swift'
@@ -10,9 +10,9 @@ Pod::Spec.new do |s|
1010
s.requires_arc = true
1111
s.documentation_url = 'https://github.com/ClusterWS/ClusterWS-Client-Swift/wiki'
1212

13-
s.ios.deployment_target = '10.0'
14-
s.tvos.deployment_target = "10.0"
15-
s.osx.deployment_target = "10.12"
13+
s.ios.deployment_target = '8.0'
14+
s.tvos.deployment_target = "9.0"
15+
s.osx.deployment_target = "10.9"
1616

1717
s.source_files = 'Sources'
1818
s.libraries = 'z'

Sources/CWSParser.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ extension CWSParser {
8080
}
8181
}
8282

83+
//MARK: Private methods
8384
extension CWSParser {
8485
private func convertToJSON(text: String) -> [String: Any]? {
8586
if let data = text.data(using: .utf8) {

Sources/CWSPing.swift

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,8 @@ extension CWSPing {
2525
self.mPingTimer = nil
2626
}
2727

28-
open func stop() {
29-
self.mMissedPing = 0
30-
self.resetPingTimer()
31-
}
32-
33-
open func start(interval: TimeInterval, socket: ClusterWS) {
34-
self.mPingTimer = Timer.scheduledTimer(withTimeInterval: interval / 1000, repeats: true, block: { (timer) in
28+
@objc private func executionBlock(_ socket: ClusterWS) {
29+
func block(_ socket: ClusterWS) {
3530
if self.mMissedPing < 3 {
3631
self.mMissedPing += 1
3732
} else {
@@ -40,6 +35,35 @@ extension CWSPing {
4035
self.resetPingTimer()
4136
}
4237
}
43-
})
38+
}
39+
40+
guard let userInfoWS = self.mPingTimer?.userInfo as? ClusterWS else {
41+
block(socket)
42+
return
43+
}
44+
45+
block(userInfoWS)
46+
}
47+
48+
open func stop() {
49+
self.mMissedPing = 0
50+
self.resetPingTimer()
51+
}
52+
53+
open func start(interval: TimeInterval, socket: ClusterWS) {
54+
if #available(iOS 10.0, *, OSX 10.12, tvOS 10.0, *) {
55+
self.mPingTimer = Timer.scheduledTimer(withTimeInterval: interval / 1000,
56+
repeats: true,
57+
block: { (timer) in
58+
self.executionBlock(socket)
59+
})
60+
} else {
61+
self.mPingTimer = Timer(timeInterval: interval,
62+
target: self,
63+
selector: #selector(executionBlock(_:)),
64+
userInfo: socket,
65+
repeats: true)
66+
self.mPingTimer?.fire()
67+
}
4468
}
4569
}

Sources/CWSReconnection.swift

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -59,19 +59,19 @@ extension CWSReconnection {
5959
let max = UInt32(self.mReconnectionIntervalMax * 1000)
6060
let min = UInt32(self.mReconnectionIntervalMin * 1000)
6161
let randomNumber = arc4random_uniform(max-min)+min
62-
self.mReconnectionTimer = Timer.scheduledTimer(withTimeInterval: Double(randomNumber / 1000),
63-
repeats: false,
64-
block: { (timer) in
65-
if self.mSocket.getState() == .closed {
66-
self.mCurrentReconnectionAttempted += 1
67-
if self.mReconnectionAttempts != 0 && self.mCurrentReconnectionAttempted >= self.mReconnectionAttempts {
68-
self.resetTimer()
69-
} else {
70-
self.resetTimer()
71-
self.mSocket.connect()
72-
}
73-
}
74-
})
62+
if #available(iOS 10.0, OSX 10.12, tvOS 10.0, *) {
63+
self.mReconnectionTimer = Timer.scheduledTimer(withTimeInterval: Double(randomNumber / 1000),
64+
repeats: false,
65+
block: { (timer) in
66+
self.reconnectionBlock()
67+
})
68+
} else {
69+
self.mReconnectionTimer = Timer(timeInterval: Double(randomNumber / 1000),
70+
target: self, selector: #selector(reconnectionBlock),
71+
userInfo: nil,
72+
repeats: false)
73+
self.mReconnectionTimer?.fire()
74+
}
7575
}
7676
}
7777

@@ -81,4 +81,16 @@ extension CWSReconnection {
8181
let channels = self.mSocket.getChannels()
8282
channels.forEach { _ = self.mSocket.subscribe($0.mChannelName) }
8383
}
84+
85+
@objc private func reconnectionBlock() {
86+
if self.mSocket.getState() == .closed {
87+
self.mCurrentReconnectionAttempted += 1
88+
if self.mReconnectionAttempts != 0 && self.mCurrentReconnectionAttempted >= self.mReconnectionAttempts {
89+
self.resetTimer()
90+
} else {
91+
self.resetTimer()
92+
self.mSocket.connect()
93+
}
94+
}
95+
}
8496
}

Sources/ClusterWS.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import Foundation
99

1010
// MARK: Properties & Initialization
11-
open class ClusterWS {
11+
open class ClusterWS: NSObject {
1212
public var delegate: CWSDelegate?
1313
private let mEmitter: CWSEmitter
1414
private let mPingHandler: CWSPing

Tests/CWSChannelTests.swift

Lines changed: 114 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,19 @@ class CWSChannelTests: XCTestCase {
2121
override func tearDown() {
2222
// Put teardown code here. This method is called after the invocation of each test method in the class.
2323
super.tearDown()
24+
self.webSocket.disconnect()
2425
}
2526

2627
func testGetChannel() {
2728
self.webSocket.connect()
2829
let connectionExpectation = expectation(description: "connection expectation")
29-
Timer.scheduledTimer(withTimeInterval: 1.9, repeats: false) { (_) in
30+
Timer.scheduledTimer(withTimeInterval: 1.0, repeats: true) { (timer) in
3031
if self.webSocket.getState() == .open {
3132
connectionExpectation.fulfill()
33+
timer.invalidate()
3234
}
3335
}
34-
wait(for: [connectionExpectation], timeout: 2.0)
36+
wait(for: [connectionExpectation], timeout: 5.0)
3537
let channelName = "test channel"
3638
let subscribedChannel = self.webSocket.subscribe(channelName)
3739
let recievedChannel = self.webSocket.getChannel(by: channelName)
@@ -41,50 +43,148 @@ class CWSChannelTests: XCTestCase {
4143
func testGetAllChannels() {
4244
self.webSocket.connect()
4345
let connectionExpectation = expectation(description: "connection expectation")
44-
Timer.scheduledTimer(withTimeInterval: 1.9, repeats: false) { (_) in
46+
Timer.scheduledTimer(withTimeInterval: 1.0, repeats: true) { (timer) in
4547
if self.webSocket.getState() == .open {
4648
connectionExpectation.fulfill()
49+
timer.invalidate()
4750
}
4851
}
49-
wait(for: [connectionExpectation], timeout: 2.0)
52+
wait(for: [connectionExpectation], timeout: 5.0)
5053
let channels = ["first channel", "second channel", "third channel"]
5154
_ = channels.map { self.webSocket.subscribe($0) }
5255
let recievedChannels = self.webSocket.getChannels().map { $0.mChannelName }
5356
XCTAssertEqual(channels, recievedChannels)
5457
}
5558

56-
func testPublishAndWatch() {
59+
func testPublishWatchString() {
5760
self.webSocket.connect()
5861
let connectionExpectation = expectation(description: "connection expectation")
59-
Timer.scheduledTimer(withTimeInterval: 1.9, repeats: false) { (_) in
62+
Timer.scheduledTimer(withTimeInterval: 1.0, repeats: true) { (timer) in
6063
if self.webSocket.getState() == .open {
6164
connectionExpectation.fulfill()
65+
timer.invalidate()
6266
}
6367
}
64-
wait(for: [connectionExpectation], timeout: 2.0)
68+
wait(for: [connectionExpectation], timeout: 5.0)
6569
let channelName = "test channel"
66-
let currentData = "test string"
67-
_ = self.webSocket.subscribe(channelName).publish(data: currentData).watch { (data) in
68-
guard let recievedData = data as? String else {
70+
let currentString = "test string"
71+
_ = self.webSocket.subscribe(channelName).publish(data: currentString).watch { (data) in
72+
guard let recievedString = data as? String else {
6973
return XCTFail()
7074
}
71-
XCTAssertEqual(recievedData, currentData)
75+
XCTAssertEqual(recievedString, currentString)
76+
}
77+
}
78+
79+
func testPublishWatchInt() {
80+
self.webSocket.connect()
81+
let connectionExpectation = expectation(description: "connection expectation")
82+
Timer.scheduledTimer(withTimeInterval: 1.0, repeats: true) { (timer) in
83+
if self.webSocket.getState() == .open {
84+
connectionExpectation.fulfill()
85+
timer.invalidate()
86+
}
87+
}
88+
wait(for: [connectionExpectation], timeout: 5.0)
89+
let channelName = "test channel"
90+
let currentInt = 30
91+
_ = self.webSocket.subscribe(channelName).publish(data: currentInt).watch { (data) in
92+
guard let recievedInt = data as? String else {
93+
return XCTFail()
94+
}
95+
XCTAssertEqual(currentInt, Int(recievedInt))
96+
}
97+
}
98+
99+
func testPublishWatchDictionary() {
100+
self.webSocket.connect()
101+
let connectionExpectation = expectation(description: "connection expectation")
102+
Timer.scheduledTimer(withTimeInterval: 1.0, repeats: true) { (timer) in
103+
if self.webSocket.getState() == .open {
104+
connectionExpectation.fulfill()
105+
timer.invalidate()
106+
}
107+
}
108+
wait(for: [connectionExpectation], timeout: 5.0)
109+
let channelName = "test channel"
110+
let key = "id"
111+
let value = 0
112+
let currentDictionary = [key: value]
113+
_ = self.webSocket.subscribe(channelName).publish(data: currentDictionary).watch { (data) in
114+
guard let recievedDictionaryString = data as? String else {
115+
return XCTFail()
116+
}
117+
if !recievedDictionaryString.contains(key) && !recievedDictionaryString.contains(String(value)) {
118+
return XCTFail()
119+
}
120+
}
121+
}
122+
123+
func testPublishWatchArray() {
124+
self.webSocket.connect()
125+
let connectionExpectation = expectation(description: "connection expectation")
126+
Timer.scheduledTimer(withTimeInterval: 1.0, repeats: true) { (timer) in
127+
if self.webSocket.getState() == .open {
128+
connectionExpectation.fulfill()
129+
timer.invalidate()
130+
}
131+
}
132+
wait(for: [connectionExpectation], timeout: 5.0)
133+
134+
let channelName = "test channel"
135+
let value1 = 30
136+
let value2 = "test"
137+
let currentArray = [value1, value2] as [Any]
138+
_ = self.webSocket.subscribe(channelName).publish(data: currentArray).watch { (data) in
139+
guard let recievedArrayString = data as? String else {
140+
return XCTFail()
141+
}
142+
if !recievedArrayString.contains(String(value1)) && !recievedArrayString.contains(value2) {
143+
return XCTFail()
144+
}
145+
}
146+
}
147+
148+
func testPublishWatchBoolean() {
149+
self.webSocket.connect()
150+
let connectionExpectation = expectation(description: "connection expectation")
151+
Timer.scheduledTimer(withTimeInterval: 1.0, repeats: true) { (timer) in
152+
if self.webSocket.getState() == .open {
153+
connectionExpectation.fulfill()
154+
timer.invalidate()
155+
}
156+
}
157+
wait(for: [connectionExpectation], timeout: 5.0)
158+
159+
let channelName = "test channel"
160+
let currentBoolean = false
161+
_ = self.webSocket.subscribe(channelName).publish(data: currentBoolean).watch { (data) in
162+
guard let recievedBooleanStringNumber = data as? String else {
163+
return XCTFail()
164+
}
165+
if recievedBooleanStringNumber == "0" || recievedBooleanStringNumber == "1" {
166+
XCTAssertEqual(Int(recievedBooleanStringNumber), currentBoolean.hashValue)
167+
} else {
168+
XCTFail()
169+
}
72170
}
73171
}
74172

75173
func testUnsubscribe() {
76174
self.webSocket.connect()
77175
let connectionExpectation = expectation(description: "connection expectation")
78-
Timer.scheduledTimer(withTimeInterval: 1.9, repeats: false) { (_) in
176+
Timer.scheduledTimer(withTimeInterval: 1.0, repeats: true) { (timer) in
79177
if self.webSocket.getState() == .open {
80178
connectionExpectation.fulfill()
179+
timer.invalidate()
81180
}
82181
}
83-
wait(for: [connectionExpectation], timeout: 2.0)
182+
wait(for: [connectionExpectation], timeout: 5.0)
183+
84184
let channelName = "test channel"
85185
let subscribedChannel = self.webSocket.subscribe(channelName)
86186
subscribedChannel.unsubscribe()
87-
let recievedChannel = self.webSocket.getChannel(by: channelName)
187+
let recievedChannel = self.webSocket?.getChannel(by: channelName)
88188
XCTAssertEqual(nil, recievedChannel)
89189
}
90190
}

0 commit comments

Comments
 (0)