Skip to content

Commit

Permalink
Fix for spring boot 2.1.x
Browse files Browse the repository at this point in the history
  • Loading branch information
demetrio812 committed Feb 13, 2019
1 parent 7fb1c42 commit 6c924ee
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 49 deletions.
2 changes: 2 additions & 0 deletions Example/StompClientLib.swift
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,8 @@ public class StompClientLib: NSObject, SRWebSocketDelegate {
private func connect() {
if socket?.readyState == .OPEN {
// at the moment only anonymous logins
connectionHeaders = [StompCommands.commandHeaderAcceptVersion:"1.1,1.2"]

self.sendFrame(command: StompCommands.commandConnect, header: connectionHeaders, body: nil)
} else {
self.openSocket()
Expand Down
100 changes: 51 additions & 49 deletions StompClientLib/Classes/StompClientLib.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ struct StompCommands {
static let commandAck = "ACK"
static let commandDisconnect = "DISCONNECT"
static let commandPing = "\n"

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

// Ack Mode
static let ackClient = "client"
static let ackAuto = "auto"
Expand Down Expand Up @@ -60,7 +60,7 @@ public enum StompAckMode {
@objc
public protocol StompClientLibDelegate {
func stompClient(client: StompClientLib!, didReceiveMessageWithJSONBody jsonBody: String?, withHeader header:[String:String]?, withDestination destination: String)

func stompClientDidDisconnect(client: StompClientLib!)
func stompClientDidConnect(client: StompClientLib!)
func serverDidSendReceipt(client: StompClientLib!, withReceiptId receiptId: String)
Expand All @@ -77,7 +77,7 @@ public class StompClientLib: NSObject, SRWebSocketDelegate {
public var connection: Bool = false
public var certificateCheckEnabled = true
private var urlRequest: NSURLRequest?

public func sendJSONForDict(dict: AnyObject, toDestination destination: String) {
do {
let theJSONData = try JSONSerialization.data(withJSONObject: dict, options: JSONSerialization.WritingOptions())
Expand All @@ -89,33 +89,33 @@ public class StompClientLib: NSObject, SRWebSocketDelegate {
print("error serializing JSON: \(error)")
}
}

public func openSocketWithURLRequest(request: NSURLRequest, delegate: StompClientLibDelegate) {
self.delegate = delegate
self.urlRequest = request
// Opening the socket
openSocket()
}

public func openSocketWithURLRequest(request: NSURLRequest, delegate: StompClientLibDelegate, connectionHeaders: [String: String]?) {
self.connectionHeaders = connectionHeaders
openSocketWithURLRequest(request: request, delegate: delegate)
self.connection = true
}

private func openSocket() {
if socket == nil || socket?.readyState == .CLOSED {
if certificateCheckEnabled == true {
self.socket = SRWebSocket(urlRequest: urlRequest! as URLRequest)
} else {
self.socket = SRWebSocket(urlRequest: urlRequest! as URLRequest, protocols: [], allowsUntrustedSSLCertificates: true)
}

socket!.delegate = self
socket!.open()
}
}

private func closeSocket(){
if let delegate = delegate {
DispatchQueue.main.async(execute: {
Expand All @@ -136,25 +136,27 @@ public class StompClientLib: NSObject, SRWebSocketDelegate {
private func connect() {
if socket?.readyState == .OPEN {
// at the moment only anonymous logins
connectionHeaders = [StompCommands.commandHeaderAcceptVersion:"1.1,1.2"]

self.sendFrame(command: StompCommands.commandConnect, header: connectionHeaders, body: nil)
} else {
self.openSocket()
}
}

public func webSocket(_ webSocket: SRWebSocket!, didReceiveMessage message: Any!) {

func processString(string: String) {
var contents = string.components(separatedBy: "\n")
if contents.first == "" {
contents.removeFirst()
}

if let command = contents.first {
var headers = [String: String]()
var body = ""
var hasHeaders = false

contents.removeFirst()
for line in contents {
if hasHeaders == true {
Expand All @@ -170,16 +172,16 @@ public class StompClientLib: NSObject, SRWebSocketDelegate {
}
}
}

// Remove the garbage from body
if body.hasSuffix("\0") {
body = body.replacingOccurrences(of: "\0", with: "")
}

receiveFrame(command: command, headers: headers, body: body)
}
}

if let strData = message as? NSData {
if let msg = String(data: strData as Data, encoding: String.Encoding.utf8) {
processString(string: msg)
Expand All @@ -188,22 +190,22 @@ public class StompClientLib: NSObject, SRWebSocketDelegate {
processString(string: str)
}
}

public func webSocketDidOpen(_ webSocket: SRWebSocket!) {
print("WebSocket is connected")
connect()
}

public func webSocket(_ webSocket: SRWebSocket!, didFailWithError error: Error!) {
print("didFailWithError: \(String(describing: error))")

if let delegate = delegate {
DispatchQueue.main.async(execute: {
delegate.serverDidSendError(client: self, withErrorMessage: error.localizedDescription, detailedErrorMessage: error as? String)
})
}
}

public func webSocket(_ webSocket: SRWebSocket!, didCloseWithCode code: Int, reason: String!, wasClean: Bool) {
print("didCloseWithCode \(code), reason: \(String(describing: reason))")
if let delegate = delegate {
Expand All @@ -212,18 +214,18 @@ public class StompClientLib: NSObject, SRWebSocketDelegate {
})
}
}

public func webSocket(_ webSocket: SRWebSocket!, didReceivePong pongPayload: Data!) {
print("didReceivePong")
}

private func sendFrame(command: String?, header: [String: String]?, body: AnyObject?) {
if socket?.readyState == .OPEN {
var frameString = ""
if command != nil {
frameString = command! + "\n"
}

if let header = header {
for (key, value) in header {
frameString += key
Expand All @@ -232,20 +234,20 @@ public class StompClientLib: NSObject, SRWebSocketDelegate {
frameString += "\n"
}
}

if let body = body as? String {
frameString += "\n"
frameString += body
} else if let _ = body as? NSData {

}

if body == nil {
frameString += "\n"
}

frameString += StompCommands.controlChar

if socket?.readyState == .OPEN {
socket?.send(frameString)
} else {
Expand All @@ -257,7 +259,7 @@ public class StompClientLib: NSObject, SRWebSocketDelegate {
}
}
}

private func destinationFromHeader(header: [String: String]) -> String {
for (key, _) in header {
if key == "destination" {
Expand All @@ -267,7 +269,7 @@ public class StompClientLib: NSObject, SRWebSocketDelegate {
}
return ""
}

/*private func dictForJSONString(jsonStr: String?) -> AnyObject? {
if let jsonStr = jsonStr {
do {
Expand All @@ -281,14 +283,14 @@ public class StompClientLib: NSObject, SRWebSocketDelegate {
}
return nil
}*/

private func receiveFrame(command: String, headers: [String: String], body: String?) {
if command == StompCommands.responseFrameConnected {
// Connected
if let sessId = headers[StompCommands.responseHeaderSession] {
sessionId = sessId
}

if let delegate = delegate {
DispatchQueue.main.async(execute: {
delegate.stompClientDidConnect(client: self)
Expand Down Expand Up @@ -329,46 +331,46 @@ public class StompClientLib: NSObject, SRWebSocketDelegate {
}
}
}

public func sendMessage(message: String, toDestination destination: String, withHeaders headers: [String: String]?, withReceipt receipt: String?) {
var headersToSend = [String: String]()
if let headers = headers {
headersToSend = headers
}

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

headersToSend[StompCommands.commandHeaderDestination] = destination

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

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

/*
Main Connection Check Method
*/
public func isConnected() -> Bool{
return connection
}

/*
Main Subscribe Method with topic name
*/
public func subscribe(destination: String) {
connection = true
subscribeToDestination(destination: destination, ackMode: .AutoMode)
}

public func subscribeToDestination(destination: String, ackMode: StompAckMode) {
var ack = ""
switch ackMode {
Expand All @@ -385,13 +387,13 @@ public class StompClientLib: NSObject, SRWebSocketDelegate {
}
self.sendFrame(command: StompCommands.commandSubscribe, header: headers, body: nil)
}

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

/*
Main Unsubscribe Method with topic name
*/
Expand All @@ -401,38 +403,38 @@ public class StompClientLib: NSObject, SRWebSocketDelegate {
headerToSend[StompCommands.commandHeaderDestinationId] = destination
sendFrame(command: StompCommands.commandUnsubscribe, header: headerToSend, body: nil)
}

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

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

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

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

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

/*
Main Disconnection Method to close the socket
*/
Expand All @@ -451,7 +453,7 @@ public class StompClientLib: NSObject, SRWebSocketDelegate {
if #available(iOS 10.0, *) {
Timer.scheduledTimer(withTimeInterval: time, repeats: true, block: { _ in
self.reconnectLogic(request: request, delegate: delegate
, connectionHeaders: connectionHeaders)
, connectionHeaders: connectionHeaders)
})
} else {
// Fallback on earlier versions
Expand Down

0 comments on commit 6c924ee

Please sign in to comment.