Community
Participate
Working Groups
If the MQTT-JS client can't connect for any reason (bad path given, server down etc) the client throws an exception as it tries to disconnect an unconnected session. The line it throws the exception on is: delete this.socket; and here is the exception: WebSocket connection to 'ws://h2o.proatomic.com.au/mosquitto1' failed: WebSocket is closed before the connection is established. mqttws31.js:1307 Regards Simon Ratcliffe
Thanks Simon. cc'ing Nick as QA for verification as I know he has experience as a user of the JS client too.
I've tried a couple things, but not been able to cause the symptom you describe. Can you provide a minimal test case? Here's what I tried: <html> <head> <script src="mqttws31.js"></script> <script> var client = new Messaging.Client("NotARealHost",9999, "clientId"); client.onConnectionLost = function(err) { console.log(err);}; client.connect({ onSuccess:function() { console.log("connected");}, onFailure:function(err) { console.log(err);} }); </script> </head> <body> </body> </html>
Nicholas, I tried your test case except I changed the host and port to a valid webserver inside my network that doesn't support websockets. So initially I get a 404 error at line 912: this.socket = new WebSocket(wsurl, 'mqttv3.1'); this should probably be wrapped with a try/catch. After this I get the exception I mentioned before. I should mention I am using the latest version of Chrome as my browser. I have tested with the latest version of IE10 and FF and give differing results, but all return the initial script error for the 404. Hope this helps. Simon
Just a little more info. Using your test case, I got script errors (the 404) in both IE10 and FF, but not in Chrome.
Thanks Simon - I had only tried in Chrome. In Chrome: - If the server/port doesn't exist LOG: Starting... 127.0.0.1:5 LOG: Object {invocationContext: undefined, errorCode: 8, errorMessage: "AMQJS0008I Socket closed."} - If the server/port exists, but not a websocket handler LOG: Starting... ERROR: WebSocket connection to 'ws://127.0.0.1/mqtt' failed: Unexpected response code: 404 ERROR: WebSocket connection to 'ws://127.0.0.1/mqtt' failed: WebSocket is closed before the connection is established. LOG: Object {invocationContext: undefined, errorCode: 7, errorMessage: "AMQJS0007E Socket error:undefined."} In FF - If the server/port doesn't exist, or it does exist but not a websocket handler: LOG: Starting... ERROR: Firefox can't establish a connection to the server at ws://127.0.0.1oih/mqtt LOG: ({invocationContext:(void 0), errorCode:7, errorMessage:"AMQJS0007E Socket error:undefined."}) I don't have ready access to IE to test against. Issue #1: none of the 'ERROR' lines above should be exposed to the console - they need to be caught internally. Issue #2: in each of these cases, the subsequent errorMessage object passed back to the onFailure callback has 'Socket error:undefined'.
Happy to run IE10 test cases for you. In IE10: I had to change the onFailure function from: onFailure:function(err) { console.log(err);} to onFailure:function(err) { console.log(err.invocationContext+' '+err.errorCode+':'+err.errorMessage);} The original function in IE10 just outputs the following to the console: [object Object] - If the server/port doesn't exist SCRIPT12007: WebSocket Error: Network Error 12007, The server name or address could not be resolved undefined 7:AMQJS0007E Socket error:undefined. - If the server exists, but port is not listening SCRIPT12029: WebSocket Error: Network Error 12029, A connection with the server could not be established undefined 7:AMQJS0007E Socket error:undefined. - If the server/port exists, but not a websocket handler SCRIPT12008: WebSocket Error: Incorrect HTTP response. Status code 404, Not Found undefined 7:AMQJS0007E Socket error:undefined.
Looks like the WebSocket was being closed before the negotiation was finished. The documentation is not clear whether this is allowed, but I've put a guard round it anyway. Can you give this a spin, it wont fix the negotiation bug but at least the client wont complain.
I can confirm the commited fix removes the "WebSocket is closed before the connection is established" error message. But it doesn't touch the related issues identified in this bug. Andy: as you've closed this bug, would you like me to raise separate issues for those other items? Specifically: Issue #1: none of the 'ERROR' lines above should be exposed to the console - they need to be caught internally. Issue #2: in each of these cases, the subsequent errorMessage object passed back to the onFailure callback has 'Socket error:undefined'.
Nick, let's split those out as separate issues so we can track them separately. Thanks.
I don't think you need to complicate this that much. As I mentioned in comment #3 probably the best solution is a try/catch around the opening of the websocket. This should solve all the differing results that occur with the various browsers and reasons for the fault.
Simon is right here - the additional items are all related and could have been easily swept up together with a single fix. Without feedback from Andy B, I'll raise these items separately.