Skip to content

Commit 6cd8f79

Browse files
committed
Support socket.io 3 + starscream 4
1 parent 6992ea5 commit 6cd8f79

25 files changed

+164
-579
lines changed

.travis.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
language: objective-c
22
xcode_project: Socket.IO-Client-Swift.xcodeproj # path to your xcodeproj folder
33
xcode_scheme: SocketIO-Mac
4-
osx_image: xcode11.2
4+
osx_image: xcode12.2
55
branches:
66
only:
77
- master

CHANGELOG.md

+5
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
# v16.0.0
2+
3+
- Removed Objective-C support. It's time for you to embrace Swift.
4+
- Socket.io 3 support.
5+
16
# v15.3.0
27

38
- Add `==` operators for `SocketAckStatus` and `String`

Cartfile

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
github "daltoniam/Starscream" ~> 3.1
1+
github "daltoniam/Starscream" ~> 4.0

Cartfile.resolved

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
github "daltoniam/Starscream" "3.1.0"
1+
github "daltoniam/Starscream" "4.0.4"

Package.resolved

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
"repositoryURL": "https://github.com/daltoniam/Starscream",
77
"state": {
88
"branch": null,
9-
"revision": "9c03ef715d1bc9334b446c90df53586dd38cf849",
10-
"version": "3.1.0"
9+
"revision": "df8d82047f6654d8e4b655d1b1525c64e1059d21",
10+
"version": "4.0.4"
1111
}
1212
}
1313
]

Package.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// swift-tools-version:5.0
1+
// swift-tools-version:5.3
22

33
import PackageDescription
44

@@ -8,7 +8,7 @@ let package = Package(
88
.library(name: "SocketIO", targets: ["SocketIO"])
99
],
1010
dependencies: [
11-
.package(url: "https://github.com/daltoniam/Starscream", .upToNextMinor(from: "3.1.0")),
11+
.package(url: "https://github.com/daltoniam/Starscream", .upToNextMinor(from: "4.0.0")),
1212
],
1313
targets: [
1414
.target(name: "SocketIO", dependencies: ["Starscream"]),

Socket.IO-Client-Swift.podspec

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
Pod::Spec.new do |s|
22
s.name = "Socket.IO-Client-Swift"
33
s.module_name = "SocketIO"
4-
s.version = "15.2.0"
4+
s.version = "16.0.0"
55
s.summary = "Socket.IO-client for iOS and OS X"
66
s.description = <<-DESC
77
Socket.IO-client for iOS and OS X.
88
Supports ws/wss/polling connections and binary.
9-
For socket.io 2.0+ and Swift.
9+
For socket.io 3.0+ and Swift.
1010
DESC
1111
s.homepage = "https://github.com/socketio/socket.io-client-swift"
1212
s.license = { :type => 'MIT' }
@@ -18,7 +18,7 @@ Pod::Spec.new do |s|
1818
s.requires_arc = true
1919
s.source = {
2020
:git => "https://github.com/socketio/socket.io-client-swift.git",
21-
:tag => 'v15.2.0',
21+
:tag => 'v16.0.0',
2222
:submodules => true
2323
}
2424

@@ -27,5 +27,5 @@ Pod::Spec.new do |s|
2727
'SWIFT_VERSION' => '5.0'
2828
}
2929
s.source_files = "Source/SocketIO/**/*.swift", "Source/SocketIO/*.swift"
30-
s.dependency "Starscream", "~> 3.1"
30+
s.dependency "Starscream", "~> 4.0"
3131
end

Source/SocketIO/Client/SocketIOClient.swift

+11-81
Original file line numberDiff line numberDiff line change
@@ -40,31 +40,21 @@ import Foundation
4040
///
4141
/// **NOTE**: The client is not thread/queue safe, all interaction with the socket should be done on the `manager.handleQueue`
4242
///
43-
open class SocketIOClient : NSObject, SocketIOClientSpec {
43+
open class SocketIOClient: NSObject, SocketIOClientSpec {
4444
// MARK: Properties
4545

4646
/// The namespace that this socket is currently connected to.
4747
///
4848
/// **Must** start with a `/`.
49-
@objc
5049
public let nsp: String
5150

52-
/// The session id of this client.
53-
@objc
54-
public var sid: String {
55-
guard let engine = manager?.engine else { return "" }
56-
57-
return nsp == "/" ? engine.sid : "\(nsp)#\(engine.sid)"
58-
}
59-
6051
/// A handler that will be called on any event.
6152
public private(set) var anyHandler: ((SocketAnyEvent) -> ())?
6253

6354
/// The array of handlers for this socket.
6455
public private(set) var handlers = [SocketEventHandler]()
6556

6657
/// The manager for this socket.
67-
@objc
6858
public private(set) weak var manager: SocketManagerSpec?
6959

7060
/// A view into this socket where emits do not check for binary data.
@@ -76,17 +66,18 @@ open class SocketIOClient : NSObject, SocketIOClientSpec {
7666
/// ```
7767
///
7868
/// **NOTE**: It is not safe to hold on to this view beyond the life of the socket.
79-
@objc
8069
public private(set) lazy var rawEmitView = SocketRawView(socket: self)
8170

8271
/// The status of this client.
83-
@objc
8472
public private(set) var status = SocketIOStatus.notConnected {
8573
didSet {
8674
handleClientEvent(.statusChange, data: [status, status.rawValue])
8775
}
8876
}
8977

78+
/// The id of this socket.io connect. This is different from the sid of the engine.io connection.
79+
public private(set) var sid: String?
80+
9081
let ackHandlers = SocketAckManager()
9182

9283
private(set) var currentAck = -1
@@ -99,7 +90,6 @@ open class SocketIOClient : NSObject, SocketIOClientSpec {
9990
///
10091
/// - parameter manager: The manager for this socket.
10192
/// - parameter nsp: The namespace of the socket.
102-
@objc
10393
public init(manager: SocketManagerSpec, nsp: String) {
10494
self.manager = manager
10595
self.nsp = nsp
@@ -117,7 +107,6 @@ open class SocketIOClient : NSObject, SocketIOClientSpec {
117107
/// Connect to the server. The same as calling `connect(timeoutAfter:withHandler:)` with a timeout of 0.
118108
///
119109
/// Only call after adding your event listeners, unless you know what you're doing.
120-
@objc
121110
open func connect() {
122111
connect(timeoutAfter: 0, withHandler: nil)
123112
}
@@ -129,7 +118,6 @@ open class SocketIOClient : NSObject, SocketIOClientSpec {
129118
/// - parameter timeoutAfter: The number of seconds after which if we are not connected we assume the connection
130119
/// has failed. Pass 0 to never timeout.
131120
/// - parameter handler: The handler to call when the client fails to connect.
132-
@objc
133121
open func connect(timeoutAfter: Double, withHandler handler: (() -> ())?) {
134122
assert(timeoutAfter >= 0, "Invalid timeout: \(timeoutAfter)")
135123

@@ -142,13 +130,6 @@ open class SocketIOClient : NSObject, SocketIOClientSpec {
142130

143131
joinNamespace()
144132

145-
if manager.status == .connected && nsp == "/" {
146-
// We might not get a connect event for the default nsp, fire immediately
147-
didConnect(toNamespace: nsp)
148-
149-
return
150-
}
151-
152133
guard timeoutAfter != 0 else { return }
153134

154135
manager.handleQueue.asyncAfter(deadline: DispatchTime.now() + timeoutAfter) {[weak self] in
@@ -171,14 +152,15 @@ open class SocketIOClient : NSObject, SocketIOClientSpec {
171152
/// then this is only called when the client connects to that namespace.
172153
///
173154
/// - parameter toNamespace: The namespace that was connected to.
174-
open func didConnect(toNamespace namespace: String) {
155+
open func didConnect(toNamespace namespace: String, payload: [String: Any]?) {
175156
guard status != .connected else { return }
176157

177158
DefaultSocketLogger.Logger.log("Socket connected", type: logType)
178159

179160
status = .connected
161+
sid = payload?["sid"] as? String
180162

181-
handleClientEvent(.connect, data: [namespace])
163+
handleClientEvent(.connect, data: payload == nil ? [namespace] : [namespace, payload!])
182164
}
183165

184166
/// Called when the client has disconnected from socket.io.
@@ -190,6 +172,7 @@ open class SocketIOClient : NSObject, SocketIOClientSpec {
190172
DefaultSocketLogger.Logger.log("Disconnected: \(reason)", type: logType)
191173

192174
status = .disconnected
175+
sid = ""
193176

194177
handleClientEvent(.disconnect, data: [reason])
195178
}
@@ -198,7 +181,6 @@ open class SocketIOClient : NSObject, SocketIOClientSpec {
198181
///
199182
/// This will cause the socket to leave the namespace it is associated to, as well as remove itself from the
200183
/// `manager`.
201-
@objc
202184
open func disconnect() {
203185
DefaultSocketLogger.Logger.log("Closing socket", type: logType)
204186

@@ -215,7 +197,7 @@ open class SocketIOClient : NSObject, SocketIOClientSpec {
215197
/// - parameter completion: Callback called on transport write completion.
216198
open func emit(_ event: String, _ items: SocketData..., completion: (() -> ())? = nil) {
217199
do {
218-
try emit(event, with: items.map({ try $0.socketRepresentation() }), completion: completion)
200+
emit([event] + (try items.map({ try $0.socketRepresentation() })), completion: completion)
219201
} catch {
220202
DefaultSocketLogger.Logger.error("Error creating socketRepresentation for emit: \(event), \(items)",
221203
type: logType)
@@ -224,25 +206,6 @@ open class SocketIOClient : NSObject, SocketIOClientSpec {
224206
}
225207
}
226208

227-
/// Same as emit, but meant for Objective-C
228-
///
229-
/// - parameter event: The event to send.
230-
/// - parameter items: The items to send with this event. Send an empty array to send no data.
231-
@objc
232-
open func emit(_ event: String, with items: [Any]) {
233-
emit([event] + items)
234-
}
235-
236-
/// Same as emit, but meant for Objective-C
237-
///
238-
/// - parameter event: The event to send.
239-
/// - parameter items: The items to send with this event. Send an empty array to send no data.
240-
/// - parameter completion: Callback called on transport write completion.
241-
@objc
242-
open func emit(_ event: String, with items: [Any], completion: (() -> ())? = nil) {
243-
emit([event] + items, completion: completion)
244-
}
245-
246209
/// Sends a message to the server, requesting an ack.
247210
///
248211
/// **NOTE**: It is up to the server send an ack back, just calling this method does not mean the server will ack.
@@ -264,7 +227,7 @@ open class SocketIOClient : NSObject, SocketIOClientSpec {
264227
/// - returns: An `OnAckCallback`. You must call the `timingOut(after:)` method before the event will be sent.
265228
open func emitWithAck(_ event: String, _ items: SocketData...) -> OnAckCallback {
266229
do {
267-
return emitWithAck(event, with: try items.map({ try $0.socketRepresentation() }))
230+
return createOnAck([event] + (try items.map({ try $0.socketRepresentation() })))
268231
} catch {
269232
DefaultSocketLogger.Logger.error("Error creating socketRepresentation for emit: \(event), \(items)",
270233
type: logType)
@@ -275,27 +238,6 @@ open class SocketIOClient : NSObject, SocketIOClientSpec {
275238
}
276239
}
277240

278-
/// Same as emitWithAck, but for Objective-C
279-
///
280-
/// **NOTE**: It is up to the server send an ack back, just calling this method does not mean the server will ack.
281-
/// Check that your server's api will ack the event being sent.
282-
///
283-
/// Example:
284-
///
285-
/// ```swift
286-
/// socket.emitWithAck("myEvent", with: [1]).timingOut(after: 1) {data in
287-
/// ...
288-
/// }
289-
/// ```
290-
///
291-
/// - parameter event: The event to send.
292-
/// - parameter items: The items to send with this event. Use `[]` to send nothing.
293-
/// - returns: An `OnAckCallback`. You must call the `timingOut(after:)` method before the event will be sent.
294-
@objc
295-
open func emitWithAck(_ event: String, with items: [Any]) -> OnAckCallback {
296-
return createOnAck([event] + items)
297-
}
298-
299241
func emit(_ data: [Any],
300242
ack: Int? = nil,
301243
binary: Bool = true,
@@ -338,7 +280,6 @@ open class SocketIOClient : NSObject, SocketIOClientSpec {
338280
///
339281
/// - parameter ack: The number for this ack.
340282
/// - parameter data: The data sent back with this ack.
341-
@objc
342283
open func handleAck(_ ack: Int, data: [Any]) {
343284
guard status == .connected else { return }
344285

@@ -361,7 +302,6 @@ open class SocketIOClient : NSObject, SocketIOClientSpec {
361302
/// - parameter data: The data that was sent with this event.
362303
/// - parameter isInternalMessage: Whether this event was sent internally. If `true` it is always sent to handlers.
363304
/// - parameter ack: If > 0 then this event expects to get an ack back from the client.
364-
@objc
365305
open func handleEvent(_ event: String, data: [Any], isInternalMessage: Bool, withAck ack: Int = -1) {
366306
guard status == .connected || isInternalMessage else { return }
367307

@@ -387,7 +327,7 @@ open class SocketIOClient : NSObject, SocketIOClientSpec {
387327
case .ack, .binaryAck:
388328
handleAck(packet.id, data: packet.data)
389329
case .connect:
390-
didConnect(toNamespace: nsp)
330+
didConnect(toNamespace: nsp, payload: packet.data.isEmpty ? nil : packet.data[0] as? [String: Any])
391331
case .disconnect:
392332
didDisconnect(reason: "Got Disconnect")
393333
case .error:
@@ -396,13 +336,11 @@ open class SocketIOClient : NSObject, SocketIOClientSpec {
396336
}
397337

398338
/// Call when you wish to leave a namespace and disconnect this socket.
399-
@objc
400339
open func leaveNamespace() {
401340
manager?.disconnectSocket(self)
402341
}
403342

404343
/// Joins `nsp`.
405-
@objc
406344
open func joinNamespace() {
407345
DefaultSocketLogger.Logger.log("Joining namespace \(nsp)", type: logType)
408346

@@ -423,7 +361,6 @@ open class SocketIOClient : NSObject, SocketIOClientSpec {
423361
/// If you wish to remove a specific event, call the `off(id:)` with the UUID received from its `on` call.
424362
///
425363
/// - parameter event: The event to remove handlers for.
426-
@objc
427364
open func off(_ event: String) {
428365
DefaultSocketLogger.Logger.log("Removing handler for event: \(event)", type: logType)
429366

@@ -435,7 +372,6 @@ open class SocketIOClient : NSObject, SocketIOClientSpec {
435372
/// If you want to remove all events for an event, call the off `off(_:)` method with the event name.
436373
///
437374
/// - parameter id: The UUID of the handler you wish to remove.
438-
@objc
439375
open func off(id: UUID) {
440376
DefaultSocketLogger.Logger.log("Removing handler with id: \(id)", type: logType)
441377

@@ -447,7 +383,6 @@ open class SocketIOClient : NSObject, SocketIOClientSpec {
447383
/// - parameter event: The event name for this handler.
448384
/// - parameter callback: The callback that will execute when this event is received.
449385
/// - returns: A unique id for the handler that can be used to remove it.
450-
@objc
451386
@discardableResult
452387
open func on(_ event: String, callback: @escaping NormalCallback) -> UUID {
453388
DefaultSocketLogger.Logger.log("Adding handler for event: \(event)", type: logType)
@@ -491,7 +426,6 @@ open class SocketIOClient : NSObject, SocketIOClientSpec {
491426
/// - parameter event: The event name for this handler.
492427
/// - parameter callback: The callback that will execute when this event is received.
493428
/// - returns: A unique id for the handler that can be used to remove it.
494-
@objc
495429
@discardableResult
496430
open func once(_ event: String, callback: @escaping NormalCallback) -> UUID {
497431
DefaultSocketLogger.Logger.log("Adding once handler for event: \(event)", type: logType)
@@ -512,20 +446,17 @@ open class SocketIOClient : NSObject, SocketIOClientSpec {
512446
/// Adds a handler that will be called on every event.
513447
///
514448
/// - parameter handler: The callback that will execute whenever an event is received.
515-
@objc
516449
open func onAny(_ handler: @escaping (SocketAnyEvent) -> ()) {
517450
anyHandler = handler
518451
}
519452

520453
/// Tries to reconnect to the server.
521-
@objc
522454
@available(*, unavailable, message: "Call the manager's reconnect method")
523455
open func reconnect() { }
524456

525457
/// Removes all handlers.
526458
///
527459
/// Can be used after disconnecting to break any potential remaining retain cycles.
528-
@objc
529460
open func removeAllHandlers() {
530461
handlers.removeAll(keepingCapacity: false)
531462
}
@@ -534,7 +465,6 @@ open class SocketIOClient : NSObject, SocketIOClientSpec {
534465
/// Called when the manager detects a broken connection, or when a manual reconnect is triggered.
535466
///
536467
/// - parameter reason: The reason this socket is reconnecting.
537-
@objc
538468
open func setReconnecting(reason: String) {
539469
status = .connecting
540470

0 commit comments

Comments
 (0)