Skip to content

Commit

Permalink
Remove Starscream Dependency and bump minimum support platform (#11)
Browse files Browse the repository at this point in the history
* Remove Starscream

* Bump swift-tools-version to 5.5
  • Loading branch information
grdsdev authored Sep 13, 2022
1 parent 3e4d8a4 commit 0b985c6
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 129 deletions.
25 changes: 0 additions & 25 deletions Package.resolved

This file was deleted.

14 changes: 9 additions & 5 deletions Package.swift
Original file line number Diff line number Diff line change
@@ -1,23 +1,27 @@
// swift-tools-version:5.3
// swift-tools-version:5.5
// The swift-tools-version declares the minimum version of Swift required to build this package.

import PackageDescription

let package = Package(
name: "Realtime",
platforms: [
.iOS(.v13),
.macCatalyst(.v13),
.macOS(.v10_15),
.watchOS(.v6),
.tvOS(.v13),
],
products: [
.library(
name: "Realtime",
targets: ["Realtime"]
)
],
dependencies: [
.package(url: "https://github.com/daltoniam/Starscream.git", from: "3.0.0")
],
targets: [
.target(
name: "Realtime",
dependencies: ["Starscream"]
dependencies: []
),
.testTarget(
name: "RealtimeTests",
Expand Down
6 changes: 1 addition & 5 deletions Sources/Realtime/RealtimeClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -163,11 +163,7 @@ public class RealtimeClient: TransportDelegate {
endPointUrl = RealtimeClient.buildEndpointUrl(
endpoint: endPoint,
params: params)
if #available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, *) {
transport = URLSessionTransport(url: endPointUrl)
} else {
transport = StarscreamTransport(url: endPointUrl)
}
transport = URLSessionTransport(url: endPointUrl)
self.params = params
self.endPoint = endPoint

Expand Down
94 changes: 0 additions & 94 deletions Sources/Realtime/Transport.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
// THE SOFTWARE.

import Foundation
import Starscream

#if canImport(FoundationNetworking)
import FoundationNetworking
Expand Down Expand Up @@ -279,96 +278,3 @@ public class URLSessionTransport: NSObject, Transport, URLSessionWebSocketDelega
delegate?.onClose(code: RealtimeClient.CloseCode.abnormal.rawValue)
}
}

public class StarscreamTransport: NSObject, Transport, WebSocketDelegate {
/// The URL to connect to
private let url: URL

/// The underlying Starscream WebSocket that data is transfered over.
private var socket: WebSocket?

/**
Initializes a `Transport` layer built using Starscream's WebSocket, which is available for
previous iOS targets back to iOS 10.
If you are targeting iOS 13 or later, then you do not need to use this transport layer unless
you specifically prefer using Starscream as the underlying Socket connection.
Example:
```swift
let url = URL("wss://example.com/socket")
let transport: Transport = StarscreamTransport(url: url)
```
- parameter url: URL to connect to
*/
public init(url: URL) {
self.url = url
super.init()
}

// MARK: - Transport

public var readyState: TransportReadyState = .closed
public var delegate: TransportDelegate?

public func connect() {
// Set the trasport state as connecting
readyState = .connecting

let socket = WebSocket(url: url)
socket.delegate = self
socket.connect()

self.socket = socket
}

public func disconnect(code: Int, reason _: String?) {
/*
TODO:
1. Provide a "strict" mode that fails if an invalid close code is given
2. If strict mode is disabled, default to CloseCode.invalid
3. Provide default .normalClosure function
*/
guard
let closeCode = CloseCode(rawValue: UInt16(code))
else {
fatalError("Could not create a CloseCode with invalid code: [\(code)].")
}

readyState = .closing
socket?.disconnect(closeCode: closeCode.rawValue)
}

public func send(data: Data) {
socket?.write(data: data)
}

// MARK: - WebSocketDelegate

public func websocketDidConnect(socket _: WebSocketClient) {
readyState = .open
delegate?.onOpen()
}

public func websocketDidDisconnect(socket _: WebSocketClient, error: Error?) {
let closeCode = (error as? WSError)?.code ?? RealtimeClient.CloseCode.abnormal.rawValue
// Set the state of the Transport to closed
readyState = .closed

// Inform the Transport's delegate that an error occurred.
if let safeError = error { delegate?.onError(error: safeError) }

// An abnormal error is results in an abnormal closure, such as internet getting dropped
// so inform the delegate that the Transport has closed abnormally. This will kick off
// the reconnect logic.
delegate?.onClose(code: closeCode)
}

public func websocketDidReceiveMessage(socket _: WebSocketClient, text: String) {
delegate?.onMessage(message: text)
}

public func websocketDidReceiveData(socket _: WebSocketClient, data _: Data) { /* no-op */ }
}

0 comments on commit 0b985c6

Please sign in to comment.