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

Cannot connect to MQTT over Websocket with Xamarin #612

Closed
VircomSheep opened this issue May 6, 2019 · 5 comments
Closed

Cannot connect to MQTT over Websocket with Xamarin #612

VircomSheep opened this issue May 6, 2019 · 5 comments

Comments

@VircomSheep
Copy link

VircomSheep commented May 6, 2019

Hi,

I tried to connect to the AWS iot core over Websockets in my Xamarin App and it fails.

This is how I connect:

            var optionsBuilder = new MqttClientOptionsBuilder();
            WebsocketUrlSigner signer = new WebsocketUrlSigner();
            var credentials = CognitoSession.Current.CurrentCredentials.GetCredentials();            
            Credentials c = new Credentials()
            {
                AccessKeyId = credentials.AccessKey,
                SecretKey = credentials.SecretKey,
                SessionToken = credentials.Token
            };
            var url = signer.GetWebsocketUrl(c);

            optionsBuilder.WithWebSocketServer(url);
            optionsBuilder.WithClientId(clientId);  
            optionsBuilder.WithProtocolVersion(MqttProtocolVersion.V311);
            

            MqttClientOptionsBuilderTlsParameters tlsParams = new MqttClientOptionsBuilderTlsParameters()
            {
                UseTls = true,
                SslProtocol = System.Security.Authentication.SslProtocols.Tls12
            };
            optionsBuilder.WithTls(tlsParams);

            options = optionsBuilder.Build();

The url is a signed url with SigV4 with the cognito AccessKey, SecretKey and Token.

When I call client.ConnectAsync(options) I get the following Exception:

{MQTTnet.Exceptions.MqttCommunicationException}:
Message: "Unable to connect to the remote server"
Source: mscorlib
StackTrace:
at MQTTnet.Adapter.MqttChannelAdapter.WrapException (System.Exception exception) [0x0005a] in <0cba824eb1f047f28db40a990343853f>:0
at MQTTnet.Adapter.MqttChannelAdapter+d__20.MoveNext () [0x000bf] in <0cba824eb1f047f28db40a990343853f>:0
--- End of stack trace from previous location where exception was thrown ---
at MQTTnet.Client.MqttClient+d__30.MoveNext () [0x00152] in <0cba824eb1f047f28db40a990343853f>:0
--- End of stack trace from previous location where exception was thrown ---
at MQTTnet.Client.MqttClient+d__30.MoveNext () [0x00356] in <0cba824eb1f047f28db40a990343853f>:0
--- End of stack trace from previous location where exception was thrown ---

In the inner exception there is a WebSocketErrorCode and it says "System.Net.WebSockets.WebSocketError.Success"

In a little Windows Forms test application with the same parameters it works.

Do I miss something in the parameters for Xamarin?

Any help appreciated.

@prokis65
Copy link

prokis65 commented May 6, 2019

Hi,
Cristian built a test adapter factory supporting WebSocket4Net.
I didn't test it a lot but it worked.
I'm realy interested in AWS IOT and it is a pity that there is still an issue with it.

#211

Regards

@dealproc
Copy link

I know I had been working with this library and webSockets using the server pieces of the library on the other end. give me some time, and i'll look up how I wrote the connection. I was using username/password authentication, which may make a difference.

@chkr1011
Copy link
Collaborator

I released a new extension nuget which allows using WebSocket4Net. All you need to do is download the new nuget and call UseWebSocket4Net() on the MqttFactory instance before creating the client. Please let me know if this works with AWS/Xamarin.

@prokis65
Copy link

I updated MQTTnet to 3.0.2.
Which extention nuget should I install to call UseWebSocket4Net() ?

@chkr1011
Copy link
Collaborator

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants