Skip to content

Commit

Permalink
Improve code
Browse files Browse the repository at this point in the history
  • Loading branch information
hhhello0507 committed Jul 7, 2024
1 parent ab6ec82 commit 3eee0d6
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 111 deletions.
Binary file not shown.
127 changes: 57 additions & 70 deletions Sources/ApeunStompKit/ApeunStomp.swift
Original file line number Diff line number Diff line change
@@ -1,17 +1,6 @@
import SocketRocket
import Combine

//@objc
//public protocol ApeunStompDelegate {
// func stompClient(client: ApeunStomp!, didReceiveMessageWithJSONBody jsonBody: AnyObject?, akaStringBody stringBody: String?, withHeader header: ApeunStomp.StompHeaders?, withDestination destination: String)
//
// func stompClientDidDisconnect(client: ApeunStomp!)
// func stompClientDidConnect(client: ApeunStomp!)
// func serverDidSendReceipt(client: ApeunStomp!, withReceiptId receiptId: String)
// func serverDidSendError(client: ApeunStomp!, withErrorMessage description: String, detailedErrorMessage message: String?)
// func serverDidSendPing()
//}

public enum ApeunStompEvent {
case stompClient(jsonBody: AnyObject?, stringBody: String?, header: ApeunStomp.StompHeaders?, destination: String)
case stompClientDidDisconnect
Expand All @@ -21,15 +10,13 @@ public enum ApeunStompEvent {
case serverDidSendPing
}

@objcMembers
public class ApeunStomp: NSObject {

public typealias StompHeaders = [String: String]
private var subscriptions = Set<AnyCancellable>()

// MARK: - Parameters
private var request: URLRequest
// weak var delegate: ApeunStompDelegate?
private var connectionHeaders: StompHeaders?

var socket: SRWebSocket?
Expand All @@ -42,11 +29,9 @@ public class ApeunStomp: NSObject {

public init(
request: URLRequest,
// delegate: ApeunStompDelegate,
connectionHeaders: StompHeaders? = nil
) {
self.request = request
// self.delegate = delegate
self.connectionHeaders = connectionHeaders
}

Expand All @@ -61,10 +46,7 @@ public class ApeunStomp: NSObject {
}
}

private func closeSocket(){
// guard let delegate else {
// return
// }
private func closeSocket() {
subject.send(.stompClientDidDisconnect)
if socket != nil {
// Close the socket
Expand All @@ -81,19 +63,19 @@ public class ApeunStomp: NSObject {
}
// Support for Spring Boot 2.1.x
if connectionHeaders == nil {
connectionHeaders = [StompCommands.commandHeaderAcceptVersion:"1.1,1.2"]
connectionHeaders = [StompCommands.commandHeaderAcceptVersion.rawValue:"1.1,1.2"]
} else {
connectionHeaders?[StompCommands.commandHeaderAcceptVersion] = "1.1,1.2"
connectionHeaders?[StompCommands.commandHeaderAcceptVersion.rawValue] = "1.1,1.2"
}
// at the moment only anonymous logins
self.sendFrame(command: StompCommands.commandConnect, header: connectionHeaders)
self.sendFrame(command: .commandConnect, header: connectionHeaders)
}

// MARK: - Send
public func sendJSONForDict(dict: Encodable, to destination: String) {
do {
let json = try String(decoding: JSONEncoder().encode(dict), as: UTF8.self)
let header = [StompCommands.commandHeaderContentType:"application/json;charset=UTF-8"]
let header = [StompCommands.commandHeaderContentType.rawValue:"application/json;charset=UTF-8"]
sendMessage(message: json, to: destination, withHeaders: header, withReceipt: nil)
} catch {
print("error serializing JSON: \(error)")
Expand All @@ -110,31 +92,31 @@ public class ApeunStomp: NSObject {

// Setting up the receipt.
if let receipt = receipt {
headers[StompCommands.commandHeaderReceipt] = receipt
headers[StompCommands.commandHeaderReceipt.rawValue] = receipt
}

// Setting up header destination.
headers[StompCommands.commandHeaderDestination] = destination
headers[StompCommands.commandHeaderDestination.rawValue] = destination

// Setting up the content length.
let contentLength = message.utf8.count
headers[StompCommands.commandHeaderContentLength] = "\(contentLength)"
headers[StompCommands.commandHeaderContentLength.rawValue] = "\(contentLength)"

// Setting up content type as plain text.
if headers[StompCommands.commandHeaderContentType] == nil {
headers[StompCommands.commandHeaderContentType] = "text/plain"
if headers[StompCommands.commandHeaderContentType.rawValue] == nil {
headers[StompCommands.commandHeaderContentType.rawValue] = "text/plain"
}
sendFrame(
body: message,
command: StompCommands.commandSend,
command: .commandSend,
header: headers
)
}


private func sendFrame(
body: String? = nil,
command: String?,
command: StompCommands?,
header: StompHeaders?
) {
guard socket?.readyState == .OPEN else {
Expand All @@ -143,7 +125,7 @@ public class ApeunStomp: NSObject {
}
var frameString = ""
if let command {
frameString = "\(command)\n"
frameString = "\(command.rawValue)\n"
}

header?.forEach{ key, value in
Expand All @@ -158,7 +140,8 @@ public class ApeunStomp: NSObject {
frameString += "\n"
}

frameString += StompCommands.controlChar
// frameString += StompCommands.controlChar.rawValue
frameString += String(format: "%C", arguments: [0x00])

print(frameString)

Expand Down Expand Up @@ -192,28 +175,28 @@ public class ApeunStomp: NSObject {
}

// MARK: - Receive
private func receiveFrame(command: String, headers: StompHeaders, body: String?) {
if command == StompCommands.responseFrameConnected {
private func receiveFrame(command: StompCommands, headers: StompHeaders, body: String?) {
if command == .responseFrameConnected {
// Connected
if let sessId = headers[StompCommands.responseHeaderSession] {
if let sessId = headers[StompCommands.responseHeaderSession.rawValue] {
sessionId = sessId
}
subject.send(.stompClientDidConnect)
} else if command == StompCommands.responseFrameMessage { // Message comes to this part
} else if command == .responseFrameMessage { // Message comes to this part
// Response
subject.send(.stompClient(jsonBody: self.dictForJSONString(jsonStr: body), stringBody: body, header: headers, destination: self.destinationFromHeader(header: headers)))
} else if command == StompCommands.responseFrameReceipt { //
} else if command == .responseFrameReceipt { //
// Receipt
if let receiptId = headers[StompCommands.responseHeaderReceiptId] {
if let receiptId = headers[StompCommands.responseHeaderReceiptId.rawValue] {
subject.send(.serverDidSendReceipt(receiptId: receiptId))
}
} else if command.count == 0 {
} else if command.rawValue.count == 0 {
// Pong from the server
try? socket?.send(string: StompCommands.commandPing)
try? socket?.send(string: StompCommands.commandPing.rawValue)
subject.send(.serverDidSendPing)
} else if command == StompCommands.responseFrameError {
} else if command == .responseFrameError {
// Error
if let msg = headers[StompCommands.responseHeaderErrorMessage] {
if let msg = headers[StompCommands.responseHeaderErrorMessage.rawValue] {
subject.send(.serverDidSendError(description: msg, message: body))
}
}
Expand All @@ -226,29 +209,33 @@ public class ApeunStomp: NSObject {
}

public func subscribeToDestination(destination: String, ackMode: StompAckMode) {
var ack = ""
switch ackMode {
let ack = switch ackMode {
case StompAckMode.ClientMode:
ack = StompCommands.ackClient
break
StompCommands.ackClient
case StompAckMode.ClientIndividualMode:
ack = StompCommands.ackClientIndividual
break
StompCommands.ackClientIndividual
default:
ack = StompCommands.ackAuto
break
StompCommands.ackAuto
}
var headers = [StompCommands.commandHeaderDestination: destination, StompCommands.commandHeaderAck: ack, StompCommands.commandHeaderDestinationId: ""]
var headers = [
StompCommands.commandHeaderDestination.rawValue: destination,
StompCommands.commandHeaderAck.rawValue: ack.rawValue,
StompCommands.commandHeaderId.rawValue: ""
]
if destination != "" {
headers = [StompCommands.commandHeaderDestination: destination, StompCommands.commandHeaderAck: ack, StompCommands.commandHeaderDestinationId: destination]
headers = [
StompCommands.commandHeaderDestination.rawValue: destination,
StompCommands.commandHeaderAck.rawValue: ack.rawValue,
StompCommands.commandHeaderId.rawValue: destination
]
}
self.sendFrame(body: nil, command: StompCommands.commandSubscribe, header: headers)
}

public func subscribeWithHeader(destination: String, withHeader header: StompHeaders) {
var headerToSend = header
headerToSend[StompCommands.commandHeaderDestination] = destination
sendFrame(body: nil, command: StompCommands.commandSubscribe, header: headerToSend)
headerToSend[StompCommands.commandHeaderDestination.rawValue] = destination
sendFrame(body: nil, command: .commandSubscribe, header: headerToSend)
}

/*
Expand All @@ -257,47 +244,47 @@ public class ApeunStomp: NSObject {
public func unsubscribe(destination: String) {
connection = false
var headerToSend: StompHeaders = [:]
headerToSend[StompCommands.commandHeaderDestinationId] = destination
sendFrame(body: nil, command: StompCommands.commandUnsubscribe, header: headerToSend)
headerToSend[StompCommands.commandHeaderId.rawValue] = destination
sendFrame(body: nil, command: .commandUnsubscribe, header: headerToSend)
}

public func begin(transactionId: String) {
var headerToSend: StompHeaders = [:]
headerToSend[StompCommands.commandHeaderTransaction] = transactionId
sendFrame(command: StompCommands.commandBegin, header: headerToSend)
headerToSend[StompCommands.commandHeaderTransaction.rawValue] = transactionId
sendFrame(command: .commandBegin, header: headerToSend)
}

public func commit(transactionId: String) {
var headerToSend: StompHeaders = [:]
headerToSend[StompCommands.commandHeaderTransaction] = transactionId
sendFrame(command: StompCommands.commandCommit, header: headerToSend)
headerToSend[StompCommands.commandHeaderTransaction.rawValue] = transactionId
sendFrame(command: .commandCommit, header: headerToSend)
}

public func abort(transactionId: String) {
var headerToSend: StompHeaders = [:]
headerToSend[StompCommands.commandHeaderTransaction] = transactionId
sendFrame(command: StompCommands.commandAbort, header: headerToSend)
headerToSend[StompCommands.commandHeaderTransaction.rawValue] = transactionId
sendFrame(command: .commandAbort, header: headerToSend)
}

public func ack(messageId: String) {
var headerToSend: StompHeaders = [:]
headerToSend[StompCommands.commandHeaderMessageId] = messageId
sendFrame(command: StompCommands.commandAck, header: headerToSend)
headerToSend[StompCommands.commandHeaderId.rawValue] = messageId
sendFrame(command: .commandAck, header: headerToSend)
}

public func ack(messageId: String, withSubscription subscription: String) {
var headerToSend: StompHeaders = [:]
headerToSend[StompCommands.commandHeaderMessageId] = messageId
headerToSend[StompCommands.commandHeaderSubscription] = subscription
sendFrame(command: StompCommands.commandAck, header: headerToSend)
headerToSend[StompCommands.commandHeaderId.rawValue] = messageId
headerToSend[StompCommands.commandHeaderSubscription.rawValue] = subscription
sendFrame(command: .commandAck, header: headerToSend)
}

// MARK: - Disconnect
public func disconnect() {
connection = false
var headerToSend: StompHeaders = [:]
headerToSend[StompCommands.commandDisconnect] = String(Int(NSDate().timeIntervalSince1970))
sendFrame(command: StompCommands.commandDisconnect, header: headerToSend)
headerToSend[StompCommands.commandDisconnect.rawValue] = String(Int(NSDate().timeIntervalSince1970))
sendFrame(command: .commandDisconnect, header: headerToSend)
// Close the socket to allow recreation
self.closeSocket()
}
Expand Down Expand Up @@ -377,7 +364,7 @@ extension ApeunStomp: SRWebSocketDelegate {
body = body.replacingOccurrences(of: "\0", with: "")
}

receiveFrame(command: command, headers: headers, body: body)
receiveFrame(command: StompCommands(rawValue: command) ?? .ackAuto, headers: headers, body: body)
}
}

Expand Down
70 changes: 33 additions & 37 deletions Sources/ApeunStompKit/StompCommand.swift
Original file line number Diff line number Diff line change
@@ -1,46 +1,42 @@
public struct StompCommands {
}

public extension StompCommands {
public enum StompCommands: String, RawRepresentable {

// Basic Commands
static let commandConnect = "CONNECT"
static let commandSend = "SEND"
static let commandSubscribe = "SUBSCRIBE"
static let commandUnsubscribe = "UNSUBSCRIBE"
static let commandBegin = "BEGIN"
static let commandCommit = "COMMIT"
static let commandAbort = "ABORT"
static let commandAck = "ACK"
static let commandDisconnect = "DISCONNECT"
static let commandPing = "\n"
case commandConnect = "CONNECT"
case commandSend = "SEND"
case commandSubscribe = "SUBSCRIBE"
case commandUnsubscribe = "UNSUBSCRIBE"
case commandBegin = "BEGIN"
case commandCommit = "COMMIT"
case commandAbort = "ABORT"
case commandAck = "ACK"
case commandDisconnect = "DISCONNECT"
case commandPing = "\n"

static let controlChar = String(format: "%C", arguments: [0x00])
case controlChar = ""

// Ack Mode
static let ackClientIndividual = "client-individual"
static let ackClient = "client"
static let ackAuto = "auto"
case ackClientIndividual = "client-individual"
case ackClient = "client"
case ackAuto = "auto"
// Header Commands
static let commandHeaderReceipt = "receipt"
static let commandHeaderDestination = "destination"
static let commandHeaderDestinationId = "id"
static let commandHeaderContentLength = "content-length"
static let commandHeaderContentType = "content-type"
static let commandHeaderAck = "ack"
static let commandHeaderTransaction = "transaction"
static let commandHeaderMessageId = "id"
static let commandHeaderSubscription = "subscription"
static let commandHeaderDisconnected = "disconnected"
static let commandHeaderHeartBeat = "heart-beat"
static let commandHeaderAcceptVersion = "accept-version"
case commandHeaderReceipt = "receipt"
case commandHeaderDestination = "destination"
case commandHeaderId = "id"
case commandHeaderContentLength = "content-length"
case commandHeaderContentType = "content-type"
case commandHeaderAck = "ack"
case commandHeaderTransaction = "transaction"
case commandHeaderSubscription = "subscription"
case commandHeaderDisconnected = "disconnected"
case commandHeaderHeartBeat = "heart-beat"
case commandHeaderAcceptVersion = "accept-version"
// Header Response Keys
static let responseHeaderSession = "session"
static let responseHeaderReceiptId = "receipt-id"
static let responseHeaderErrorMessage = "message"
case responseHeaderSession = "session"
case responseHeaderReceiptId = "receipt-id"
case responseHeaderErrorMessage = "message"
// Frame Response Keys
static let responseFrameConnected = "CONNECTED"
static let responseFrameMessage = "MESSAGE"
static let responseFrameReceipt = "RECEIPT"
static let responseFrameError = "ERROR"
case responseFrameConnected = "CONNECTED"
case responseFrameMessage = "MESSAGE"
case responseFrameReceipt = "RECEIPT"
case responseFrameError = "ERROR"
}
Loading

0 comments on commit 3eee0d6

Please sign in to comment.