-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
OleksandrZhurba
authored and
OleksandrZhurba
committed
Aug 11, 2020
1 parent
f72283d
commit 53bb0d0
Showing
34 changed files
with
1,701 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
# StompClient | ||
--- | ||
StompClien it's implementation [STOMP](https://stomp.github.io) on native [WebSocket](https://developer.apple.com/documentation/foundation/urlsessionwebsockettask) started support from iOS 13 | ||
|
||
## Requirements | ||
--- | ||
- iOS 13.0+ | ||
- macOS 10.15+ | ||
- Mac Catalyst 13.0+ | ||
- tvOS 13.0+ | ||
- watchOS 6.0+ | ||
|
||
## Example using StompClient | ||
--- | ||
```swift | ||
let request = URLRequest(url: "wss://,ws://") | ||
let webSocket = WebSocket(request: request) | ||
let heartBeat = HeartBeat(clientHeartBeating: "2000,5000") | ||
let stompClient = StompClient(webSocket: webSocket, heartBeat: heartBeat) | ||
|
||
stompClient.stompDelegate = self | ||
stompClient.openWebSocketConnection() | ||
``` | ||
|
||
```request``` request must contain ws:// or wss:// | ||
|
||
```webSocket``` webSocket connection for sending STOMP frames | ||
|
||
```heartBeat``` heartBeat optinal parameter for reciving pong frame and sending ping frame, init with string "2000,5000" where 2000 (in milliseconds) time to sending ping frame to server, 5000 wating handling pong frame from server, for more detail visit [Heart-beating](https://stomp.github.io/stomp-specification-1.2.html#Heart-beating) | ||
|
||
```stompDelegate``` implement handling webSocket connection/disconnection, resiving frames ```CONNECTED, MESSAGE, RECEIPT, ERROR, \n``` | ||
|
||
```openWebSocketConnection()``` open webSocket connection, handle opening webSocket ```func handleWebSocketConnect(session: URLSession, webSocketTask: URLSessionWebSocketTask, didOpenWithProtocol: String?)``` then you can use ```sendFrame(frame: StompFrameProtocol)``` | ||
|
||
```closeWebSocketConnection()``` disconnect webSocket connection, handle disconnect webSocket ```func handleWebSocketDisconnect(session: URLSession, webSocketTask: URLSessionWebSocketTask, closeCode: URLSessionWebSocketTask.CloseCode, reason: Data?)``` | ||
|
||
```sendFrame(frame: StompFrameProtocol)``` send STOMP frame via websocket, for sending custom frame (if needed) just subsribe object to `StompFrameProtocol` |
Large diffs are not rendered by default.
Oops, something went wrong.
7 changes: 7 additions & 0 deletions
7
SwiftStompClient.xcodeproj/project.xcworkspace/contents.xcworkspacedata
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
8 changes: 8 additions & 0 deletions
8
SwiftStompClient.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> | ||
<plist version="1.0"> | ||
<dict> | ||
<key>IDEDidComputeMac32BitWarning</key> | ||
<true/> | ||
</dict> | ||
</plist> |
Binary file added
BIN
+40.2 KB
...project.xcworkspace/xcuserdata/oleksandrzhurba.xcuserdatad/UserInterfaceState.xcuserstate
Binary file not shown.
19 changes: 19 additions & 0 deletions
19
...lient.xcodeproj/xcuserdata/oleksandrzhurba.xcuserdatad/xcschemes/xcschememanagement.plist
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> | ||
<plist version="1.0"> | ||
<dict> | ||
<key>SchemeUserState</key> | ||
<dict> | ||
<key>SwiftStompClient.xcscheme_^#shared#^_</key> | ||
<dict> | ||
<key>orderHint</key> | ||
<integer>0</integer> | ||
</dict> | ||
<key>SwiftStompClientExample.xcscheme_^#shared#^_</key> | ||
<dict> | ||
<key>orderHint</key> | ||
<integer>1</integer> | ||
</dict> | ||
</dict> | ||
</dict> | ||
</plist> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
public struct AbortStompFrame: StompFrameProtocol { | ||
public var command: String { | ||
return BasicCommands.abort | ||
} | ||
public var headers: [String : String] | ||
public var body: String? | ||
|
||
///STOMP frame ABORT with | ||
/// - Parameters: | ||
/// - transactionId: transaction Id | ||
public init(transactionId: String) { | ||
let abortHeaders = [HeaderCommands.transaction : transactionId] | ||
self.headers = abortHeaders | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
///Server mode AckMode | ||
public enum AckMode: String, CaseIterable { | ||
case clientIndividual = "client-individual" | ||
case client = "client" | ||
case auto = "auto" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
public struct AckStompFrame: StompFrameProtocol { | ||
public var command: String { | ||
return BasicCommands.ack | ||
} | ||
public var headers: [String : String] | ||
public var body: String? | ||
|
||
///STOMP frame ACK with | ||
/// - Parameters: | ||
/// - messageId: message Id | ||
/// - subscription: optional value subscription | ||
public init(messageId: String, subscription: String?) { | ||
var ackHeaders = [HeaderCommands.headerMessageId : messageId] | ||
if let subscription = subscription { | ||
ackHeaders[HeaderCommands.subscription] = subscription | ||
} | ||
self.headers = ackHeaders | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
///Basic client frames | ||
public struct BasicCommands { | ||
static let connect = "CONNECT" | ||
static let send = "SEND" | ||
static let subscribe = "SUBSCRIBE" | ||
static let unsubscribe = "UNSUBSCRIBE" | ||
static let begin = "BEGIN" | ||
static let commit = "COMMIT" | ||
static let abort = "ABORT" | ||
static let ack = "ACK" | ||
static let disconnect = "DISCONNECT" | ||
static let ping = "\n" | ||
|
||
static let controlChar = "\u{0000}" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
public struct BeginStompFrame: StompFrameProtocol { | ||
public var command: String { | ||
return BasicCommands.begin | ||
} | ||
public var headers: [String : String] | ||
public var body: String? | ||
|
||
///STOMP frame BEGIN with | ||
/// - Parameters: | ||
/// - transactionId: transaction Id | ||
public init(transactionId: String) { | ||
let beginHeaders = [HeaderCommands.transaction : transactionId] | ||
self.headers = beginHeaders | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
public struct CommitStompFrame: StompFrameProtocol { | ||
public var command: String { | ||
return BasicCommands.commit | ||
} | ||
public var headers: [String : String] | ||
public var body: String? | ||
|
||
///STOMP frame COMMIT with | ||
/// - Parameters: | ||
/// - transactionId: transaction Id | ||
public init(transactionId: String) { | ||
let commitHeaders = [HeaderCommands.transaction : transactionId] | ||
self.headers = commitHeaders | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
public struct ConnectStompFrame: StompFrameProtocol { | ||
public var command: String { | ||
return BasicCommands.connect | ||
} | ||
public var headers: [String : String] | ||
public var body: String? | ||
|
||
public var clientHeartBeat: String? | ||
|
||
///STOMP frame CONNECT with | ||
/// - Parameters: | ||
/// - acceptVersion: accept STOMP version | ||
/// - login: login | ||
/// - passcode: passcode | ||
/// - host: host | ||
/// - heartBeat: optional value heart-beat | ||
public init(acceptVersion: String, login: String, passcode: String, host: String, heartBeat client: String? = nil) { | ||
var connectionHeaders = [HeaderCommands.acceptVersion : acceptVersion, | ||
HeaderCommands.login : login, | ||
HeaderCommands.passcode : passcode, | ||
HeaderCommands.host : host] | ||
if let clientHeartBeat = client { | ||
self.clientHeartBeat = clientHeartBeat | ||
connectionHeaders[HeaderCommands.heartBeat] = clientHeartBeat | ||
} | ||
self.headers = connectionHeaders | ||
} | ||
|
||
///STOMP frame CONNECT with | ||
/// - Parameters: | ||
/// - acceptVersion: accept STOMP version | ||
/// - heartBeat: optional value heart-beat | ||
public init(acceptVersion: String, heartBeat client: String? = nil) { | ||
var connectionHeaders = [HeaderCommands.acceptVersion : acceptVersion] | ||
|
||
if let clientHeartBeat = client { | ||
self.clientHeartBeat = clientHeartBeat | ||
connectionHeaders[HeaderCommands.heartBeat] = clientHeartBeat | ||
} | ||
self.headers = connectionHeaders | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import Foundation | ||
|
||
public struct DisconnectStompFrame: StompFrameProtocol { | ||
public var command: String { | ||
return BasicCommands.disconnect | ||
} | ||
public var headers: [String : String] | ||
public var body: String? | ||
|
||
///STOMP frame DISCONNECT with disconnected time in | ||
public init() { | ||
let disconnectHeaders = [HeaderCommands.disconnected : "\(Int(Date().timeIntervalSince1970))"] | ||
self.headers = disconnectHeaders | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
///Headers commands | ||
public struct HeaderCommands { | ||
static let receipt = "receipt" | ||
static let destination = "destination" | ||
static let destinationId = "id" | ||
static let headerMessageId = "id" | ||
static let contentLength = "content-length" | ||
static let contentType = "content-type" | ||
static let ack = "ack" | ||
static let transaction = "transaction" | ||
static let subscription = "subscription" | ||
static let disconnected = "disconnected" | ||
static let heartBeat = "heart-beat" | ||
static let acceptVersion = "accept-version" | ||
static let login = "login" | ||
static let passcode = "passcode" | ||
static let host = "host" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
import Foundation | ||
|
||
public class HeartBeat { | ||
private var timerPing = DispatchSource.makeTimerSource() | ||
private var timerPong: DispatchSourceTimer? | ||
|
||
private let leeway = DispatchTimeInterval.never | ||
|
||
private var clientHeartBeating: String | ||
|
||
private var pingTime: Double = 0.0 | ||
private var pongTime: Double = 0.0 | ||
|
||
public var pingAction: (() -> Void)? | ||
public var pongAction: (() -> Void)? | ||
|
||
public init(clientHeartBeating: String) { | ||
self.clientHeartBeating = clientHeartBeating | ||
} | ||
|
||
public func handleFrames(headers: [String : String], frame: SwiftStompClient.FrameResponseKeys) { | ||
switch frame { | ||
case .connected where headers.contains(where: { $0.key == HeaderCommands.heartBeat }): | ||
heartBeating(client: clientHeartBeating, server: headers[HeaderCommands.heartBeat]!) | ||
case .message: | ||
resetListeningServerBeating() | ||
case .receipt: | ||
resetListeningServerBeating() | ||
case .ping: | ||
resetListeningServerBeating() | ||
default: | ||
return | ||
} | ||
} | ||
|
||
///reset timer for sending DISCONNECT stomp frame | ||
private func resetListeningServerBeating() { | ||
timerPong?.setEventHandler(handler: nil) | ||
timerPong?.cancel() | ||
timerPong = nil | ||
pongAction = nil | ||
// | ||
startListeningServerBeating() | ||
} | ||
|
||
private func startListeningServerBeating() { | ||
timerPong = DispatchSource.makeTimerSource() | ||
timerPong?.schedule(deadline: .now() + pongTime, repeating: pongTime, leeway: leeway) | ||
timerPong?.setEventHandler(handler: pongAction) | ||
timerPong?.resume() | ||
} | ||
|
||
private func stomAllTimers() { | ||
timerPong?.setEventHandler(handler: nil) | ||
timerPong?.cancel() | ||
timerPong = nil | ||
pongAction = nil | ||
// | ||
timerPing.setEventHandler(handler: nil) | ||
timerPing.cancel() | ||
pingAction = nil | ||
} | ||
|
||
///Heart-beart implemetation looks like "5000,5000" were first part in client time beating, second part server time beating(miliseconds) | ||
/// - Parameters: | ||
/// - client: client beating | ||
/// - server: server beating | ||
private func heartBeating(client: String, server: String) { | ||
var client = String(client) | ||
var server = String(server) | ||
|
||
guard let commaClientIndex = client.firstIndex(of: ","), | ||
let commaServerIndex = server.firstIndex(of: ",") else { | ||
print("Seams like doesn't contain comma") | ||
return | ||
} | ||
client.remove(at: commaClientIndex) | ||
server.remove(at: commaServerIndex) | ||
|
||
let cx = Int(client[..<commaClientIndex]) ?? 0 | ||
let cy = Int(client[commaClientIndex...]) ?? 0 | ||
let sx = Int(server[..<commaServerIndex]) ?? 0 | ||
let sy = Int(server[commaServerIndex...]) ?? 0 | ||
|
||
//in seconds | ||
pingTime = ceil(Double(max(cx, sy) / 1000)) | ||
pongTime = ceil(Double(max(sx, cy) / 1000)) | ||
|
||
if pingTime > 0 { | ||
timerPing.schedule(deadline: .now(), repeating: pingTime, leeway: leeway) | ||
timerPing.setEventHandler(handler: pingAction) | ||
timerPing.activate() | ||
} | ||
|
||
if pongTime > 0 { | ||
startListeningServerBeating() | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> | ||
<plist version="1.0"> | ||
<dict> | ||
<key>CFBundleDevelopmentRegion</key> | ||
<string>$(DEVELOPMENT_LANGUAGE)</string> | ||
<key>CFBundleExecutable</key> | ||
<string>$(EXECUTABLE_NAME)</string> | ||
<key>CFBundleIdentifier</key> | ||
<string>$(PRODUCT_BUNDLE_IDENTIFIER)</string> | ||
<key>CFBundleInfoDictionaryVersion</key> | ||
<string>6.0</string> | ||
<key>CFBundleName</key> | ||
<string>$(PRODUCT_NAME)</string> | ||
<key>CFBundlePackageType</key> | ||
<string>$(PRODUCT_BUNDLE_PACKAGE_TYPE)</string> | ||
<key>CFBundleShortVersionString</key> | ||
<string>$(MARKETING_VERSION)</string> | ||
<key>CFBundleVersion</key> | ||
<string>$(CURRENT_PROJECT_VERSION)</string> | ||
</dict> | ||
</plist> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
public struct PingStompFrame: StompFrameProtocol { | ||
///STOMP frame PING with empty headers | ||
|
||
public var command: String { | ||
return BasicCommands.ping | ||
} | ||
public var headers: [String : String] = [String : String]() | ||
public var body: String? | ||
} |
Oops, something went wrong.