Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add custom headers support to CONNECT frame #65

Merged
merged 2 commits into from Sep 4, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,10 @@ class StompClient(
* @throws ConnectionTimeout if this method takes longer than the configured
* [timeout][StompConfig.connectionTimeoutMillis] (as a whole for both WS connect and STOMP connect)
*/
suspend fun connect(url: String, login: String? = null, passcode: String? = null): StompSession {
suspend fun connect(url: String, login: String? = null, passcode: String? = null, customHeaders: Map<String, String> = emptyMap()): StompSession {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now that I think of it, I might add custom headers for the websocket connection too later.
To avoid a backwards-incompatible change in the future, could you please rename this param customStompConnectHeaders.
Also please add a relevant sentence in the doc to explain what the param does.

Copy link
Author

@ghost ghost Sep 4, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for your answer.
Learning every day 👍

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well thank you for the contribution! 😄

val session = withTimeoutOrNull(config.connectionTimeoutMillis) {
val wsSession = webSocketConnect(url)
wsSession.stompConnect(url, login, passcode)
wsSession.stompConnect(url, login, passcode, customHeaders)
}
return session ?: throw ConnectionTimeout(url, config.connectionTimeoutMillis)
}
Expand All @@ -59,13 +59,14 @@ class StompClient(
}
}

private suspend fun WebSocketSession.stompConnect(url: String, login: String?, passcode: String?): StompSession {
private suspend fun WebSocketSession.stompConnect(url: String, login: String?, passcode: String?, customHeaders: Map<String, String> = emptyMap()): StompSession {
try {
val connectHeaders = StompConnectHeaders(
host = extractHost(url),
login = login,
passcode = passcode,
heartBeat = config.heartBeat
heartBeat = config.heartBeat,
customHeaders = customHeaders
)
val stompSession = BaseStompSession(config, StompSocket(this, config, coroutineContext))
stompSession.connect(connectHeaders)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,14 +72,16 @@ data class StompConnectHeaders(private val rawHeaders: StompHeaders) : StompHead
acceptVersion: List<String> = listOf("1.2"),
login: String? = null,
passcode: String? = null,
heartBeat: HeartBeat? = null
heartBeat: HeartBeat? = null,
customHeaders: Map<String, String> = emptyMap()
) : this(
headersOf(
HOST to host,
ACCEPT_VERSION to acceptVersion.joinToString(","),
LOGIN to login,
PASSCODE to passcode,
HEART_BEAT to heartBeat?.formatAsHeaderValue()
HEART_BEAT to heartBeat?.formatAsHeaderValue(),
customHeaders = customHeaders
)
)
}
Expand Down