From 6b891a7ed41905533a1a5b23ede73c079e77dc51 Mon Sep 17 00:00:00 2001 From: Bao Nguyen Date: Mon, 19 Aug 2019 09:06:52 +0700 Subject: [PATCH] Refactor code and fix support spring 2.1.x Refactor code for the same logic when connect with the socket --- Example/StompClientLib.swift | 12 +++++------- StompClientLib/Classes/StompClientLib.swift | 15 +++++++-------- 2 files changed, 12 insertions(+), 15 deletions(-) diff --git a/Example/StompClientLib.swift b/Example/StompClientLib.swift index ff55f38..3d1cbe4 100755 --- a/Example/StompClientLib.swift +++ b/Example/StompClientLib.swift @@ -90,16 +90,12 @@ public class StompClientLib: NSObject, SRWebSocketDelegate { } } - public func openSocketWithURLRequest(request: NSURLRequest, delegate: StompClientLibDelegate) { + public func openSocketWithURLRequest(request: NSURLRequest, delegate: StompClientLibDelegate, connectionHeaders: [String: String]? = nil) { + self.connectionHeaders = connectionHeaders 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 } @@ -136,8 +132,10 @@ public class StompClientLib: NSObject, SRWebSocketDelegate { private func connect() { if socket?.readyState == .OPEN { // Support for Spring Boot 2.1.x - if (connectionHeaders == nil) { + if connectionHeaders == nil { connectionHeaders = [StompCommands.commandHeaderAcceptVersion:"1.1,1.2"] + } else { + connectionHeaders?[StompCommands.commandHeaderAcceptVersion] = "1.1,1.2" } // at the moment only anonymous logins self.sendFrame(command: StompCommands.commandConnect, header: connectionHeaders, body: nil) diff --git a/StompClientLib/Classes/StompClientLib.swift b/StompClientLib/Classes/StompClientLib.swift index a442075..8ab52c2 100755 --- a/StompClientLib/Classes/StompClientLib.swift +++ b/StompClientLib/Classes/StompClientLib.swift @@ -90,16 +90,12 @@ public class StompClientLib: NSObject, SRWebSocketDelegate { } } - public func openSocketWithURLRequest(request: NSURLRequest, delegate: StompClientLibDelegate) { + public func openSocketWithURLRequest(request: NSURLRequest, delegate: StompClientLibDelegate, connectionHeaders: [String: String]? = nil) { + self.connectionHeaders = connectionHeaders 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 } @@ -136,8 +132,10 @@ public class StompClientLib: NSObject, SRWebSocketDelegate { private func connect() { if socket?.readyState == .OPEN { // Support for Spring Boot 2.1.x - if (connectionHeaders == nil) { - connectionHeaders = [StompCommands.commandHeaderAcceptVersion:"1.1,1.2"] + if connectionHeaders == nil { + connectionHeaders = [StompCommands.commandHeaderAcceptVersion:"1.1,1.2"] + } else { + connectionHeaders?[StompCommands.commandHeaderAcceptVersion] = "1.1,1.2" } // at the moment only anonymous logins self.sendFrame(command: StompCommands.commandConnect, header: connectionHeaders, body: nil) @@ -496,3 +494,4 @@ public class StompClientLib: NSObject, SRWebSocketDelegate { } } } +