-
holy. freaking. crap. I didn't know there was an argument to the connect callback - the sample code I found didn't have an arg. And yes, putting them inside the callback and using 'socket' made it work. I am now seeing the 0xEF packets being sent. Solved all of the confusion.
As for the UDP code, it does work, but I don't claim to have written it correctly, I'll change it to match the link you provided.
So thanks a million for the corrections!
-
Thr 2019.08.08
@indianajones I think there is a word missing from that first sentence. Shouldn't it have read
" holy. freaking. crap. . . Batman! " ?
at least that's how I thought I've heard that!!
Nice observation maze1980 on the inclusion of the on() method inside the callback.
As I had no experience with sockcts and Espruino, I could only guess, but I'm glad you now have a solution.
I don't want to get into a discussion that hijacks this thread, but feel the following statement will cause confusion that are new to Espruino:
While we are all learning the nuaunces of Espruino Javascript, @maze1980 what justification are you using to suggest changing the var declaration to "const NET = require('net');. It's just a shortcut to the net library"?
A year ago, I was educated on the syntax to create a local constant copy of a module.How to define and reference an array of CONSTANTS in deployed module
A special syntax for the exports statement inside the module is required. In the past, I have looked up each module to see if there are any comment tid-bits:
The 'net' module, sadly is not deployed in this manner, as it appears to be part of the build, but did find this one that is quite similar.
http://www.espruino.com/modules/WebServer.js
Note that syntax is not used as the last exports line here either.
SIDEBAR: Gordon showed me this little trick to find comments in the source. Using the hardware reference documentationhttps://www.espruino.com/Reference#l_netÂ_connect
there is a right facing arrow at the right of the heading 'net.connect' and clicking on it takes one to the source:
https://github.com/espruino/Espruino/blob/master/libs/network/jswrap_net.c#L367
Additionally, looking over the examples show that each had a connection object assigned to a var declaration.
Assigning as a constant, although may work under some module designs, prevents making a modification to the underlying copy of that module. What if one would want to change the port designation or the baud rate or something of the sort?
While I'm not an expert on all the nuances with Espruino, as there is a connection() and on() function along with a few other supporting module functions, I don't believe assigning this particular module to a constant assignment is a smart idea nor was it the authors intended design.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/const
Let for block scope - maybe depends
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/let
It should be var
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/var
https://www.espruino.com/Reference#l_net_connect
You must place the
.on
functions inside of the connect callback using the returned socket, e.g.:How and if your UDP code really works is also questionable, it should be similar:
https://www.espruino.com/Reference#l_dgram_createSocket
While the example on https://www.espruino.com/Internet is exactly as you did it just doesn't correspond to the documentation.
And I'd change
let tcpClient = require('net');
toconst NET = require('net');
. It's just a shortcut to the net library, not a tcpClient.