Bug 408078 - Client throws exception on connect error
Summary: Client throws exception on connect error
Status: RESOLVED FIXED
Alias: None
Product: z_Archived
Classification: Eclipse Foundation
Component: Paho (show other bugs)
Version: unspecified   Edit
Hardware: All All
: P3 normal (vote)
Target Milestone: ---   Edit
Assignee: Andrew Banks CLA
QA Contact: Nick O'Leary CLA
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2013-05-15 01:03 EDT by Simon Racliffe CLA
Modified: 2016-02-05 11:15 EST (History)
3 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Simon Racliffe CLA 2013-05-15 01:03:29 EDT
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
Comment 1 Andy Piper CLA 2013-05-15 07:19:55 EDT
Thanks Simon. cc'ing Nick as QA for verification as I know he has experience as a user of the JS client too.
Comment 2 Nick O'Leary CLA 2013-05-15 17:03:20 EDT
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>
Comment 3 Simon Racliffe CLA 2013-05-15 21:48:35 EDT
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
Comment 4 Simon Racliffe CLA 2013-05-15 21:55:31 EDT
Just a little more info.  Using your test case, I got script errors (the 404) in both IE10 and FF, but not in Chrome.
Comment 5 Nick O'Leary CLA 2013-05-16 04:25:18 EDT
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'.
Comment 6 Simon Racliffe CLA 2013-05-16 04:55:00 EDT
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.
Comment 7 Andrew Banks CLA 2013-05-16 10:33:48 EDT
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.
Comment 8 Nick O'Leary CLA 2013-05-16 11:30:18 EDT
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'.
Comment 9 Andy Piper CLA 2013-05-17 06:37:18 EDT
Nick, let's split those out as separate issues so we can track them separately. Thanks.
Comment 10 Simon Racliffe CLA 2013-05-17 06:44:57 EDT
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.
Comment 11 Nick O'Leary CLA 2013-05-17 08:04:40 EDT
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.