Skip to content

Android #1511

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open

Android #1511

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 29 additions & 13 deletions Package.resolved
Original file line number Diff line number Diff line change
@@ -1,16 +1,32 @@
{
"object": {
"pins": [
{
"package": "Starscream",
"repositoryURL": "https://github.com/daltoniam/Starscream",
"state": {
"branch": null,
"revision": "c6bfd1af48efcc9a9ad203665db12375ba6b145a",
"version": "4.0.8"
}
"pins" : [
{
"identity" : "starscream",
"kind" : "remoteSourceControl",
"location" : "https://github.com/theleftbit/Starscream.git",
"state" : {
"branch" : "android",
"revision" : "fd27e744f358663eb8802692269000f45a630138"
}
]
},
"version": 1
},
{
"identity" : "swift-asn1",
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-asn1.git",
"state" : {
"revision" : "7faebca1ea4f9aaf0cda1cef7c43aecd2311ddf6",
"version" : "1.3.0"
}
},
{
"identity" : "swift-crypto",
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-crypto.git",
"state" : {
"revision" : "ff0f781cf7c6a22d52957e50b104f5768b50c779",
"version" : "3.10.0"
}
}
],
"version" : 2
}
13 changes: 10 additions & 3 deletions Package.swift
Original file line number Diff line number Diff line change
@@ -1,17 +1,24 @@
// swift-tools-version:5.4
// swift-tools-version:6.0

import PackageDescription

let package = Package(
name: "SocketIO",
platforms: [
.iOS(.v13),
.macOS(.v10_15),
.tvOS(.v13),
.watchOS(.v6)
],
products: [
.library(name: "SocketIO", targets: ["SocketIO"])
],
dependencies: [
.package(url: "https://github.com/daltoniam/Starscream", .upToNextMajor(from: "4.0.8")),
.package(url: "https://github.com/theleftbit/Starscream.git", branch: "android"),
],
targets: [
.target(name: "SocketIO", dependencies: ["Starscream"]),
.testTarget(name: "TestSocketIO", dependencies: ["SocketIO"]),
]
],
swiftLanguageModes: [.v6]
)
7 changes: 2 additions & 5 deletions Source/SocketIO/Ack/SocketAckEmitter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ public final class SocketAckEmitter: NSObject {
/// ```
///
/// **NOTE**: It is not safe to hold on to this view beyond the life of the socket.
@objc
public private(set) lazy var rawEmitView = SocketRawAckView(socket: socket, ackNum: ackNum)

// MARK: Properties
Expand Down Expand Up @@ -83,7 +82,6 @@ public final class SocketAckEmitter: NSObject {
/// Call to ack receiving this event.
///
/// - parameter items: An array of items to send when acking. Use `[]` to send nothing.
@objc
public func with(_ items: [Any]) {
guard ackNum != -1 else { return }

Expand Down Expand Up @@ -127,19 +125,18 @@ public final class OnAckCallback: NSObject {
/// - parameter seconds: The number of seconds before this emit times out if an ack hasn't been received.
/// - parameter callback: The callback called when an ack is received, or when a timeout happens.
/// To check for timeout, use `SocketAckStatus`'s `noAck` case.
@objc
public func timingOut(after seconds: Double, callback: @escaping AckCallback) {
guard let socket = self.socket, ackNumber != -1 else { return }

socket.ackHandlers.addAck(ackNumber, callback: callback)
socket.emit(items, ack: ackNumber, binary: binary)

guard seconds != 0 else { return }

let _ackNumber = ackNumber
socket.manager?.handleQueue.asyncAfter(deadline: DispatchTime.now() + seconds) {[weak socket] in
guard let socket = socket else { return }

socket.ackHandlers.timeoutAck(self.ackNumber)
socket.ackHandlers.timeoutAck(_ackNumber)
}
}

Expand Down
2 changes: 0 additions & 2 deletions Source/SocketIO/Client/SocketAnyEvent.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,9 @@ public final class SocketAnyEvent : NSObject {
// MARK: Properties

/// The event name.
@objc
public let event: String

/// The data items for this event.
@objc
public let items: [Any]?

/// The description of this event.
Expand Down
26 changes: 16 additions & 10 deletions Source/SocketIO/Client/SocketIOClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ import Foundation
///
/// **NOTE**: The client is not thread/queue safe, all interaction with the socket should be done on the `manager.handleQueue`
///
open class SocketIOClient: NSObject, SocketIOClientSpec {
open class SocketIOClient: NSObject, SocketIOClientSpec, @unchecked Sendable {
// MARK: Properties

/// The namespace that this socket is currently connected to.
Expand Down Expand Up @@ -122,7 +122,7 @@ open class SocketIOClient: NSObject, SocketIOClientSpec {
/// - parameter timeoutAfter: The number of seconds after which if we are not connected we assume the connection
/// has failed. Pass 0 to never timeout.
/// - parameter handler: The handler to call when the client fails to connect.
open func connect(withPayload payload: [String: Any]? = nil, timeoutAfter: Double, withHandler handler: (() -> ())?) {
open func connect(withPayload payload: [String: Any]? = nil, timeoutAfter: Double, withHandler handler: (@Sendable () -> ())?) {
assert(timeoutAfter >= 0, "Invalid timeout: \(timeoutAfter)")

guard let manager = self.manager, status != .connected else {
Expand Down Expand Up @@ -212,7 +212,7 @@ open class SocketIOClient: NSObject, SocketIOClientSpec {
/// - parameter event: The event to send.
/// - parameter items: The items to send with this event. May be left out.
/// - parameter completion: Callback called on transport write completion.
open func emit(_ event: String, _ items: SocketData..., completion: (() -> ())? = nil) {
open func emit(_ event: String, _ items: SocketData..., completion: (@Sendable() -> ())? = nil) {
emit(event, with: items, completion: completion)
}

Expand All @@ -224,7 +224,7 @@ open class SocketIOClient: NSObject, SocketIOClientSpec {
/// - parameter event: The event to send.
/// - parameter items: The items to send with this event. May be left out.
/// - parameter completion: Callback called on transport write completion.
open func emit(_ event: String, with items: [SocketData], completion: (() -> ())?) {
open func emit(_ event: String, with items: [SocketData], completion: (@Sendable () -> ())?) {

do {
emit([event] + (try items.map({ try $0.socketRepresentation() })), completion: completion)
Expand Down Expand Up @@ -296,15 +296,21 @@ open class SocketIOClient: NSObject, SocketIOClientSpec {
ack: Int? = nil,
binary: Bool = true,
isAck: Bool = false,
completion: (() -> ())? = nil
completion: (@Sendable () -> ())? = nil
) {
// wrap the completion handler so it always runs async via handlerQueue
let wrappedCompletion: (() -> ())? = (completion == nil) ? nil : {[weak self] in
guard let this = self else { return }
this.manager?.handleQueue.async {
completion!()
let wrappedCompletion: (@Sendable () -> ())? = {
if let completion {
return completion
} else {
return { [weak self] in
guard let this = self else { return }
this.manager?.handleQueue.async {
completion!()
}
}
}
}
}()

guard status == .connected else {
wrappedCompletion?()
Expand Down
3 changes: 3 additions & 0 deletions Source/SocketIO/Client/SocketIOClientOption.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@

import Foundation
import Starscream
#if os(Android)
import FoundationNetworking
#endif

/// The socket.io version being used.
public enum SocketIOVersion: Int {
Expand Down
6 changes: 3 additions & 3 deletions Source/SocketIO/Client/SocketIOClientSpec.swift
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public protocol SocketIOClientSpec : AnyObject {
/// - parameter timeoutAfter: The number of seconds after which if we are not connected we assume the connection
/// has failed. Pass 0 to never timeout.
/// - parameter handler: The handler to call when the client fails to connect.
func connect(withPayload payload: [String: Any]?, timeoutAfter: Double, withHandler handler: (() -> ())?)
func connect(withPayload payload: [String: Any]?, timeoutAfter: Double, withHandler handler: (@Sendable() -> ())?)

/// Called when the client connects to a namespace. If the client was created with a namespace upfront,
/// then this is only called when the client connects to that namespace.
Expand Down Expand Up @@ -106,7 +106,7 @@ public protocol SocketIOClientSpec : AnyObject {
/// - parameter event: The event to send.
/// - parameter items: The items to send with this event. May be left out.
/// - parameter completion: Callback called on transport write completion.
func emit(_ event: String, _ items: SocketData..., completion: (() -> ())?)
func emit(_ event: String, _ items: SocketData..., completion: (@Sendable () -> ())?)

/// Send an event to the server, with optional data items and optional write completion handler.
///
Expand All @@ -116,7 +116,7 @@ public protocol SocketIOClientSpec : AnyObject {
/// - parameter event: The event to send.
/// - parameter items: The items to send with this event. May be left out.
/// - parameter completion: Callback called on transport write completion.
func emit(_ event: String, with items: [SocketData], completion: (() -> ())?)
func emit(_ event: String, with items: [SocketData], completion: (@Sendable () -> ())?)

/// Call when you wish to tell the server that you've received the event for `ack`.
///
Expand Down
1 change: 0 additions & 1 deletion Source/SocketIO/Client/SocketIOStatus.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
import Foundation

/// Represents state of a manager or client.
@objc
public enum SocketIOStatus : Int, CustomStringConvertible {
// MARK: Cases

Expand Down
3 changes: 0 additions & 3 deletions Source/SocketIO/Client/SocketRawView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ public final class SocketRawView : NSObject {
///
/// - parameter event: The event to send.
/// - parameter items: The items to send with this event. Send an empty array to send no data.
@objc
public func emit(_ event: String, with items: [Any]) {
socket.emit([event] + items, binary: false)
}
Expand Down Expand Up @@ -113,7 +112,6 @@ public final class SocketRawView : NSObject {
/// - parameter event: The event to send.
/// - parameter items: The items to send with this event. Use `[]` to send nothing.
/// - returns: An `OnAckCallback`. You must call the `timingOut(after:)` method before the event will be sent.
@objc
public func emitWithAck(_ event: String, with items: [Any]) -> OnAckCallback {
return socket.createOnAck([event] + items, binary: false)
}
Expand Down Expand Up @@ -154,7 +152,6 @@ public final class SocketRawAckView : NSObject {
/// Call to ack receiving this event.
///
/// - parameter items: An array of items to send when acking. Use `[]` to send nothing.
@objc
public func with(_ items: [Any]) {
guard ackNum != -1 else { return }

Expand Down
11 changes: 9 additions & 2 deletions Source/SocketIO/Engine/SocketEngine.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,14 @@
import Dispatch
import Foundation
import Starscream
#if os(Android)
import FoundationNetworking
#endif

/// The class that handles the engine.io protocol and transports.
/// See `SocketEnginePollable` and `SocketEngineWebsocket` for transport specific methods.
open class SocketEngine: NSObject, WebSocketDelegate, URLSessionDelegate,
SocketEnginePollable, SocketEngineWebsocket, ConfigSettable {
SocketEnginePollable, SocketEngineWebsocket, ConfigSettable, @unchecked Sendable {


// MARK: Properties
Expand Down Expand Up @@ -563,7 +566,11 @@ open class SocketEngine: NSObject, WebSocketDelegate, URLSessionDelegate,
polling = true
probing = false
invalidated = false
#if os(Android)
session = FoundationNetworking.URLSession(configuration: .default, delegate: sessionDelegate, delegateQueue: queue)
#else
session = Foundation.URLSession(configuration: .default, delegate: sessionDelegate, delegateQueue: queue)
#endif
sid = ""
waitingForPoll = false
waitingForPost = false
Expand Down Expand Up @@ -656,7 +663,7 @@ open class SocketEngine: NSObject, WebSocketDelegate, URLSessionDelegate,
/// - parameter type: The type of this message.
/// - parameter data: Any data that this message has.
/// - parameter completion: Callback called on transport write completion.
open func write(_ msg: String, withType type: SocketEnginePacketType, withData data: [Data], completion: (() -> ())? = nil) {
open func write(_ msg: String, withType type: SocketEnginePacketType, withData data: [Data], completion: (@Sendable () -> ())? = nil) {
engineQueue.async {
guard self.connected else {
completion?()
Expand Down
5 changes: 4 additions & 1 deletion Source/SocketIO/Engine/SocketEngineClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,12 @@
//

import Foundation
#if os(Android)
import FoundationNetworking
#endif

/// Declares that a type will be a delegate to an engine.
@objc public protocol SocketEngineClient {
public protocol SocketEngineClient: AnyObject {
// MARK: Methods

/// Called when the engine errors.
Expand Down
2 changes: 1 addition & 1 deletion Source/SocketIO/Engine/SocketEnginePacketType.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
import Foundation

/// Represents the type of engine.io packet types.
@objc public enum SocketEnginePacketType: Int {
public enum SocketEnginePacketType: Int, Sendable {
/// Open message.
case open

Expand Down
9 changes: 6 additions & 3 deletions Source/SocketIO/Engine/SocketEnginePollable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@
// THE SOFTWARE.

import Foundation
#if os(Android)
import FoundationNetworking
#endif

/// Protocol that is used to implement socket.io polling support
public protocol SocketEnginePollable: SocketEngineSpec {
Expand Down Expand Up @@ -65,7 +68,7 @@ public protocol SocketEnginePollable: SocketEngineSpec {
/// - parameter message: The message to send.
/// - parameter withType: The type of message to send.
/// - parameter withData: The data associated with this message.
func sendPollMessage(_ message: String, withType type: SocketEnginePacketType, withData datas: [Data], completion: (() -> ())?)
func sendPollMessage(_ message: String, withType type: SocketEnginePacketType, withData datas: [Data], completion: (@Sendable () -> ())?)

/// Call to stop polling and invalidate the URLSession.
func stopPolling()
Expand Down Expand Up @@ -116,7 +119,7 @@ extension SocketEnginePollable {
doLongPoll(for: req)
}

func doRequest(for req: URLRequest, callbackWith callback: @escaping (Data?, URLResponse?, Error?) -> ()) {
func doRequest(for req: URLRequest, callbackWith callback: @escaping (@Sendable(Data?, URLResponse?, Error?) -> ())) {
guard polling && !closed && !invalidated && !fastUpgrade else { return }

DefaultSocketLogger.Logger.log("Doing polling \(req.httpMethod ?? "") \(req)", type: "SocketEnginePolling")
Expand Down Expand Up @@ -237,7 +240,7 @@ extension SocketEnginePollable {
/// - parameter withType: The type of message to send.
/// - parameter withData: The data associated with this message.
/// - parameter completion: Callback called on transport write completion.
public func sendPollMessage(_ message: String, withType type: SocketEnginePacketType, withData datas: [Data], completion: (() -> ())? = nil) {
public func sendPollMessage(_ message: String, withType type: SocketEnginePacketType, withData datas: [Data], completion: (@Sendable () -> ())? = nil) {
DefaultSocketLogger.Logger.log("Sending poll: \(message) as type: \(type.rawValue)", type: "SocketEnginePolling")

postWait.append((String(type.rawValue) + message, completion))
Expand Down
9 changes: 6 additions & 3 deletions Source/SocketIO/Engine/SocketEngineSpec.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,12 @@

import Foundation
import Starscream
#if os(Android)
import FoundationNetworking
#endif

/// Specifies a SocketEngine.
public protocol SocketEngineSpec: AnyObject {
public protocol SocketEngineSpec: AnyObject, Sendable {
// MARK: Properties

/// The client for this engine.
Expand Down Expand Up @@ -141,7 +144,7 @@ public protocol SocketEngineSpec: AnyObject {
/// - parameter type: The type of this message.
/// - parameter data: Any data that this message has.
/// - parameter completion: Callback called on transport write completion.
func write(_ msg: String, withType type: SocketEnginePacketType, withData data: [Data], completion: (() -> ())?)
func write(_ msg: String, withType type: SocketEnginePacketType, withData data: [Data], completion: (@Sendable () -> ())?)
}

extension SocketEngineSpec {
Expand Down Expand Up @@ -203,7 +206,7 @@ extension SocketEngineSpec {
}

/// Send an engine message (4)
func send(_ msg: String, withData datas: [Data], completion: (() -> ())? = nil) {
func send(_ msg: String, withData datas: [Data], completion: (@Sendable () -> ())? = nil) {
write(msg, withType: .message, withData: datas, completion: completion)
}
}
Loading