MQTTnet icon indicating copy to clipboard operation
MQTTnet copied to clipboard

Need to connect to web socket server with cookie

Open djarvis opened this issue 7 years ago • 6 comments

I need to connect to a websocket server with the outgoing initial HTTP request containing a cookie. Is this possible? I didn't see any options in MqttClientOptionsBuilder.

djarvis avatar Dec 12 '18 18:12 djarvis

I believe this is related to #496.

chkr1011 avatar Dec 13 '18 16:12 chkr1011

I was able to do this by creating a MqttClientWebSocketOptions class and attaching a CookieContainer, then assigning this to ChannelOptions of a manually created MqttClientOptions class.

In other words this is possible without using the builder, but it'd be nice if there was an override that included a cookie container on WithWebSocketServer() extension.

djarvis avatar Dec 13 '18 17:12 djarvis

Not sure, if cookies are passed through request headers, but while adding the option for headers, I will also add an overload for the CookieContainer. As @djarvis says, a MqttClientWebSocketOptions class could be passed in the builder the same way as is done with MqttClientOptionsBuilderTlsParameters.

All while leaving the default call with only the url for backwards compatibility

cquesnel avatar Dec 13 '18 17:12 cquesnel

I just tested this and I do get cookies back to the client from the connect call, and when I set them they are also recognized on the server.

I am doing this because, in scaled Azure instances, I wish to target specific instances of my MQTT broker behind the load balancer. I can tell which instance the client is talking to by observing the ARRAffinity cookie in my cookie collection, and I can also target a specific instance to connect with by setting the cookie.

It's all wonderfully awesome.

djarvis avatar Dec 13 '18 18:12 djarvis

@djarvis I've just created a pull request allowing to pass the cookies and request headers if you want to check it out

cquesnel avatar Dec 13 '18 18:12 cquesnel

I assume that this is done. Please re-open this issue or comment if you have any concerns, @djarvis.

SeppPenner avatar Jun 25 '19 08:06 SeppPenner