No, you're not doing anything wrong. It's an unfortunate side-effect of the current network abstraction layer... On CC3000, WIZnet, and Linux, the 'connect' function is synchronous - but when using AT commands with ESP8266 it's async.
As a result, the connect function completes immediately and then if the connection fails the error code is reported by send/recv functions - but the 'connected' callback has already been called.
It's particularly annoying in bare TCP/IP sockets, but the majority of people just use HTTP. In that case, the connected handler gets called only after the headers have been received - so in reality there is no real difference in the way it works at all.
You're also getting two disconnected handlers though? I'll check up on that - I've seen that before and wasn't sure about it. It must be some kind of regression.
Espruino is a JavaScript interpreter for low-power Microcontrollers. This site is both a support community for Espruino and a place to share what you are working on.
No, you're not doing anything wrong. It's an unfortunate side-effect of the current network abstraction layer... On CC3000, WIZnet, and Linux, the 'connect' function is synchronous - but when using AT commands with ESP8266 it's async.
As a result, the connect function completes immediately and then if the connection fails the error code is reported by send/recv functions - but the 'connected' callback has already been called.
It's particularly annoying in bare TCP/IP sockets, but the majority of people just use HTTP. In that case, the
connected
handler gets called only after the headers have been received - so in reality there is no real difference in the way it works at all.You're also getting two
disconnected
handlers though? I'll check up on that - I've seen that before and wasn't sure about it. It must be some kind of regression.