diff --git a/Socket.IO-Client-Swift.xcodeproj/project.pbxproj b/Socket.IO-Client-Swift.xcodeproj/project.pbxproj index 3ff316f..7d543f1 100644 --- a/Socket.IO-Client-Swift.xcodeproj/project.pbxproj +++ b/Socket.IO-Client-Swift.xcodeproj/project.pbxproj @@ -122,6 +122,9 @@ 74B4AD231D09A6190062A523 /* SSLSecurity.swift in Sources */ = {isa = PBXBuildFile; fileRef = 74B4AD201D09A6190062A523 /* SSLSecurity.swift */; }; 74B4AD241D09A6450062A523 /* WebSocket.swift in Sources */ = {isa = PBXBuildFile; fileRef = 74B4AD1C1D09A5D80062A523 /* WebSocket.swift */; }; 74B4AD251D09A6490062A523 /* WebSocket.swift in Sources */ = {isa = PBXBuildFile; fileRef = 74B4AD1C1D09A5D80062A523 /* WebSocket.swift */; }; + 74BC45AB1D0C6675008CC431 /* SocketClientManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = 74BC45AA1D0C6675008CC431 /* SocketClientManager.swift */; }; + 74BC45AC1D0C6675008CC431 /* SocketClientManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = 74BC45AA1D0C6675008CC431 /* SocketClientManager.swift */; }; + 74BC45AD1D0C6675008CC431 /* SocketClientManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = 74BC45AA1D0C6675008CC431 /* SocketClientManager.swift */; }; 74F124F01BC574CF002966F4 /* SocketBasicPacketTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = 74F124EF1BC574CF002966F4 /* SocketBasicPacketTest.swift */; }; 74F124F11BC574CF002966F4 /* SocketBasicPacketTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = 74F124EF1BC574CF002966F4 /* SocketBasicPacketTest.swift */; }; CEBA56961CDA0B7700BA0389 /* NSCharacterSet.swift in Sources */ = {isa = PBXBuildFile; fileRef = CEBA56951CDA0B7700BA0389 /* NSCharacterSet.swift */; }; @@ -195,6 +198,7 @@ 74ABF7761C3991C10078C657 /* SocketIOClientSpec.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = SocketIOClientSpec.swift; path = Source/SocketIOClientSpec.swift; sourceTree = ""; }; 74B4AD1C1D09A5D80062A523 /* WebSocket.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = WebSocket.swift; path = Source/WebSocket/WebSocket.swift; sourceTree = ""; }; 74B4AD201D09A6190062A523 /* SSLSecurity.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = SSLSecurity.swift; path = Source/WebSocket/SSLSecurity.swift; sourceTree = ""; }; + 74BC45AA1D0C6675008CC431 /* SocketClientManager.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = SocketClientManager.swift; path = Source/SocketClientManager.swift; sourceTree = ""; }; 74F124EF1BC574CF002966F4 /* SocketBasicPacketTest.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SocketBasicPacketTest.swift; sourceTree = ""; }; CEBA56951CDA0B7700BA0389 /* NSCharacterSet.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = NSCharacterSet.swift; path = Source/NSCharacterSet.swift; sourceTree = ""; }; CEBA56991CDA0B8200BA0389 /* String.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = String.swift; path = Source/String.swift; sourceTree = ""; }; @@ -336,6 +340,7 @@ 74171E501C10CD240062D398 /* SocketAckEmitter.swift */, 74171E511C10CD240062D398 /* SocketAckManager.swift */, 74171E521C10CD240062D398 /* SocketAnyEvent.swift */, + 74BC45AA1D0C6675008CC431 /* SocketClientManager.swift */, 74171E531C10CD240062D398 /* SocketEngine.swift */, 74171E541C10CD240062D398 /* SocketEngineClient.swift */, 74171E551C10CD240062D398 /* SocketEnginePacketType.swift */, @@ -632,6 +637,7 @@ 74171E811C10CD240062D398 /* SocketEnginePacketType.swift in Sources */, 74171E6F1C10CD240062D398 /* SocketAnyEvent.swift in Sources */, 74171E9F1C10CD240062D398 /* SocketIOClientOption.swift in Sources */, + 74BC45AB1D0C6675008CC431 /* SocketClientManager.swift in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; @@ -689,6 +695,7 @@ 74171E831C10CD240062D398 /* SocketEnginePacketType.swift in Sources */, 74171E711C10CD240062D398 /* SocketAnyEvent.swift in Sources */, 74171EA11C10CD240062D398 /* SocketIOClientOption.swift in Sources */, + 74BC45AC1D0C6675008CC431 /* SocketClientManager.swift in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; @@ -734,6 +741,7 @@ 74171E851C10CD240062D398 /* SocketEnginePacketType.swift in Sources */, 74171E731C10CD240062D398 /* SocketAnyEvent.swift in Sources */, 74171EA31C10CD240062D398 /* SocketIOClientOption.swift in Sources */, + 74BC45AD1D0C6675008CC431 /* SocketClientManager.swift in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; diff --git a/SocketIO-MacTests/SocketObjectiveCTest.m b/SocketIO-MacTests/SocketObjectiveCTest.m index f3f70e3..20ee20f 100644 --- a/SocketIO-MacTests/SocketObjectiveCTest.m +++ b/SocketIO-MacTests/SocketObjectiveCTest.m @@ -44,4 +44,10 @@ [self.socket off:@"test"]; } +- (void)testSocketManager { + SocketClientManager* manager = [SocketClientManager sharedManager]; + [manager addSocket:self.socket labeledAs:@"test"]; + [manager removeSocketWithLabel:@"test"]; +} + @end diff --git a/SocketIO-MacTests/SocketSideEffectTest.swift b/SocketIO-MacTests/SocketSideEffectTest.swift index 9c5b503..bae130b 100644 --- a/SocketIO-MacTests/SocketSideEffectTest.swift +++ b/SocketIO-MacTests/SocketSideEffectTest.swift @@ -153,4 +153,16 @@ class SocketSideEffectTest: XCTestCase { socket.parseBinaryData(data2) waitForExpectationsWithTimeout(3, handler: nil) } + + func testSocketManager() { + let manager = SocketClientManager.sharedManager + manager["test"] = socket + + XCTAssert(manager["test"] === socket, "failed to get socket") + + manager["test"] = nil + + XCTAssert(manager["test"] == nil, "socket not removed") + + } } diff --git a/Source/SocketClientManager.swift b/Source/SocketClientManager.swift new file mode 100644 index 0000000..3cf1c34 --- /dev/null +++ b/Source/SocketClientManager.swift @@ -0,0 +1,82 @@ +// +// SocketClientManager.swift +// Socket.IO-Client-Swift +// +// Created by Erik Little on 6/11/16. +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +// THE SOFTWARE. + +import Foundation + +/** + Experimental socket manager. + + API subject to change. + + Can be used to persist sockets across ViewControllers. + + Sockets are strongly stored, so be sure to remove them once they are no + longer needed. + + Example usage: + ``` + let manager = SocketClientManager.sharedManager + manager["room1"] = socket1 + manager["room2"] = socket2 + manager.removeSocket(socket: socket2) + manager["room1"]?.emit("hello") + ``` + */ +public final class SocketClientManager : NSObject { + public static let sharedManager = SocketClientManager() + + private var sockets = [String: SocketIOClient]() + + public subscript(string: String) -> SocketIOClient? { + get { + return sockets[string] + } + + set(socket) { + sockets[string] = socket + } + } + + public func addSocket(socket: SocketIOClient, labeledAs label: String) { + sockets[label] = socket + } + + public func removeSocket(withLabel label: String) -> SocketIOClient? { + return sockets.removeValueForKey(label) + } + + public func removeSocket(socket socket: SocketIOClient) -> SocketIOClient? { + var returnSocket: SocketIOClient? + + for (label, dictSocket) in sockets where dictSocket === socket { + returnSocket = sockets.removeValueForKey(label) + } + + return returnSocket + } + + public func removeSockets() { + sockets.removeAll() + } +} diff --git a/Source/SocketIOClient.swift b/Source/SocketIOClient.swift index 70c6f22..30f110e 100644 --- a/Source/SocketIOClient.swift +++ b/Source/SocketIOClient.swift @@ -274,7 +274,7 @@ public final class SocketIOClient : NSObject, SocketEngineClient, SocketParsable } else if !reconnecting { reconnecting = true tryReconnectWithReason(reason) - } + } } /// error