Skip to content

Commit

Permalink
feat: Upgrade Starscream to 4.0.4
Browse files Browse the repository at this point in the history
  • Loading branch information
lawmicha committed Feb 2, 2022
1 parent 708c9a4 commit d951799
Show file tree
Hide file tree
Showing 34 changed files with 2,865 additions and 2,112 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ credentials-mc.json

amplify
build/
.build/
dist/
node_modules/
aws-exports.js
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,55 @@ import Starscream

/// Extension to handle delegate callback from Starscream
extension StarscreamAdapter: Starscream.WebSocketDelegate {
public func didReceive(event: WebSocketEvent, client: WebSocket) {
switch event {
case .connected:
websocketDidConnect(socket: client)
case .disconnected(let reason, let code):
AppSyncLogger.verbose("[StarscreamAdapter] disconnected: reason=\(reason); code=\(code)")
websocketDidDisconnect(socket: client, error: nil)
case .text(let string):
websocketDidReceiveMessage(socket: client, text: string)
case .binary(let data):
websocketDidReceiveData(socket: client, data: data)
case .ping:
AppSyncLogger.verbose("[StarscreamAdapter] ping")
case .pong:
AppSyncLogger.verbose("[StarscreamAdapter] pong")
case .viabilityChanged(let viability):
AppSyncLogger.verbose("[StarscreamAdapter] viabilityChanged: \(viability)")
case .reconnectSuggested(let suggestion):
AppSyncLogger.verbose("[StarscreamAdapter] reconnectSuggested: \(suggestion)")
case .cancelled:
websocketDidDisconnect(socket: client, error: nil)
case .error(let error):
websocketDidDisconnect(socket: client, error: error)
}
}

public func websocketDidConnect(socket: WebSocketClient) {
private func websocketDidConnect(socket: WebSocketClient) {
AppSyncLogger.verbose("[StarscreamAdapter] websocketDidConnect: websocket has been connected.")
delegate?.websocketDidConnect(provider: self)
serialQueue.async {
self._isConnected = true
self.delegate?.websocketDidConnect(provider: self)
}
}

public func websocketDidDisconnect(socket: WebSocketClient, error: Error?) {
private func websocketDidDisconnect(socket: WebSocketClient, error: Error?) {
AppSyncLogger.verbose("[StarscreamAdapter] websocketDidDisconnect: \(error?.localizedDescription ?? "No error")")
delegate?.websocketDidDisconnect(provider: self, error: error)
serialQueue.async {
self._isConnected = false
self.delegate?.websocketDidDisconnect(provider: self, error: error)
}
}

public func websocketDidReceiveMessage(socket: WebSocketClient, text: String) {
private func websocketDidReceiveMessage(socket: WebSocketClient, text: String) {
AppSyncLogger.verbose("[StarscreamAdapter] websocketDidReceiveMessage: - \(text)")
let data = text.data(using: .utf8) ?? Data()
delegate?.websocketDidReceiveData(provider: self, data: data)
}

public func websocketDidReceiveData(socket: WebSocketClient, data: Data) {
private func websocketDidReceiveData(socket: WebSocketClient, data: Data) {
AppSyncLogger.verbose("[StarscreamAdapter] WebsocketDidReceiveData - \(data)")
delegate?.websocketDidReceiveData(provider: self, data: data)
}
Expand Down
39 changes: 26 additions & 13 deletions AppSyncRealTimeClient/Websocket/Starscream/StarscreamAdapter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,41 @@ import Foundation
import Starscream

public class StarscreamAdapter: AppSyncWebsocketProvider {
public init() {
// Do nothing
}

private let serialQueue = DispatchQueue(label: "com.amazonaws.StarscreamAdapter.serialQueue")
let serialQueue: DispatchQueue
private let callbackQueue: DispatchQueue

var socket: WebSocket?
weak var delegate: AppSyncWebsocketDelegate?

// swiftlint:disable:next identifier_name
var _isConnected: Bool
public var isConnected: Bool {
serialQueue.sync {
_isConnected
}
}

public init() {
let serialQueue = DispatchQueue(label: "com.amazonaws.StarscreamAdapter.serialQueue")
let callbackQueue = DispatchQueue(
label: "com.amazonaws.StarscreamAdapter.callBack",
target: serialQueue
)
self._isConnected = false
self.serialQueue = serialQueue
self.callbackQueue = callbackQueue
}

public func connect(url: URL, protocols: [String], delegate: AppSyncWebsocketDelegate?) {
serialQueue.async {
AppSyncLogger.verbose("[StarscreamAdapter] connect. Connecting to url")
self.socket = WebSocket(url: url, protocols: protocols)
var urlRequest = URLRequest(url: url)
let protocolHeaderValue = protocols.joined(separator: ", ")
urlRequest.setValue(protocolHeaderValue, forHTTPHeaderField: "Sec-WebSocket-Protocol")
self.socket = WebSocket(request: urlRequest)
self.delegate = delegate
self.socket?.delegate = self
self.socket?.callbackQueue = DispatchQueue(label: "com.amazonaws.StarscreamAdapter.callBack")
self.socket?.callbackQueue = self.callbackQueue
self.socket?.connect()
}
}
Expand All @@ -43,10 +62,4 @@ public class StarscreamAdapter: AppSyncWebsocketProvider {
self.socket?.write(string: message)
}
}

public var isConnected: Bool {
serialQueue.sync {
return socket?.isConnected ?? false
}
}
}
2 changes: 1 addition & 1 deletion Cartfile
Original file line number Diff line number Diff line change
@@ -1 +1 @@
github "daltoniam/starscream" ~> 3.1.1
github "daltoniam/starscream" ~> 4.0.4
13 changes: 2 additions & 11 deletions Package.resolved
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,8 @@
"repositoryURL": "https://github.com/daltoniam/Starscream",
"state": {
"branch": null,
"revision": "e6b65c6d9077ea48b4a7bdda8994a1d3c6969c8d",
"version": "3.1.1"
}
},
{
"package": "swift-nio-zlib-support",
"repositoryURL": "https://github.com/apple/swift-nio-zlib-support.git",
"state": {
"branch": null,
"revision": "37760e9a52030bb9011972c5213c3350fa9d41fd",
"version": "1.0.0"
"revision": "df8d82047f6654d8e4b655d1b1525c64e1059d21",
"version": "4.0.4"
}
}
]
Expand Down
2 changes: 1 addition & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ let package = Package(
targets: ["AppSyncRealTimeClient"]),
],
dependencies: [
.package(url: "https://github.com/daltoniam/Starscream", .upToNextMinor(from: "3.1.1"))
.package(url: "https://github.com/daltoniam/Starscream", .upToNextMinor(from: "4.0.4"))
],
targets: [
.target(
Expand Down
2 changes: 1 addition & 1 deletion Podfile
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ target 'AppSyncRealTimeClient' do
# Pods for AppSyncRealTimeClient

# If you update this dependency version, be sure to update the Cartfile also
pod "Starscream", "~> 3.1.0"
pod "Starscream", "~> 4.0.4"

include_build_tools!

Expand Down
8 changes: 4 additions & 4 deletions Podfile.lock
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
PODS:
- Starscream (3.1.1)
- Starscream (4.0.4)
- SwiftFormat/CLI (0.48.17)
- SwiftLint (0.45.0)

DEPENDENCIES:
- Starscream (~> 3.1.0)
- Starscream (~> 4.0.4)
- SwiftFormat/CLI
- SwiftLint

Expand All @@ -15,10 +15,10 @@ SPEC REPOS:
- SwiftLint

SPEC CHECKSUMS:
Starscream: 4bb2f9942274833f7b4d296a55504dcfc7edb7b0
Starscream: 5178aed56b316f13fa3bc55694e583d35dd414d9
SwiftFormat: 0a9044eb365d74d4a0a2cefa5fe44a4cbef382a7
SwiftLint: e5c7f1fba68eccfc51509d5b2ce1699f5502e0c7

PODFILE CHECKSUM: 52bc6ec211be54e08e048379812e7a696613d5bf
PODFILE CHECKSUM: faccccbac411bb105d282d128939114db76875b9

COCOAPODS: 1.11.2
8 changes: 4 additions & 4 deletions Pods/Manifest.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit d951799

Please sign in to comment.