adamvr/MQTT.js — pull request #143 by mcollina (2013-11-22 12:07:12+00:00)
Title: Race condition between clearReconnect and setupStream in MQTTClient.

A very strange race condition between `_clearReconnect` and `_setupStream` has been highlighted by @ldstein in https://github.com/mcollina/mosca/issues/68#issuecomment-29064007, regarding MQTT over Websocket support.

From that post:
> Ok, after I restart Mosca, I see multiple 'streamConnected' and no 'clientConnected' messages appearing in Chrome's console. One of the surplus connections is then 'closed', resulting in _setupStream being run again, resulting in an infinite loop.

It seems that the `'close'` event sometimes is not fired. Instead, we can move the `_clearReconnect` behavior directly inside `_setupStream`.