-
• #27
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.:tcpClient.connect({port: 51325, host: "10.1.64.22"}, function(socket) { console.log("connected"); socket.on("close", function() { console.log("end"); }); });
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. -
• #29
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!
-
• #30
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 -
• #31
@Robin: You don't want to modify the library or module itself.
const MODULE = require('module'); MODULE.foo(); //is the same as require('module').foo();
You can use a variable if you want to code like this
MODULE = require('module1'); MODULE.foo(); MODULE = require('module2'); MODULE.foo();
Happy debugging.
-
• #32
Final update on this issue. I have it all working now - I can control my Allen & Heath Qu series mixer via MIDI over TCP using the Espruino Wifi. My high-level understanding was accurate, but you guys helped me figure out how to use the dgram and net libraries properly to make it work. Thanks @Gordon for steering me in the right direction, and @Robin and especially @maze1980 for helping me see the errors in my code. Many thanks.
-
• #33
tcpClient.write() and tcpClient.send() throw 'undefined'
Is it because of
let tcpClient = require('net');
? You'll have definedtcpClient
as a local variable so it won't be available in the global scope
Thr 2019.08.08
I'm guessing here, so if this sounds far fetched, it's okay to blast me ;-)
Could it be that the tcpClient instance creation L7 needs to be global, inserted between L1 and L2, rather than defined inside the udpClient bind object? e.g. msgs sent, but no object present to receive them?
Did you catch this post?
Not sure which time zone you are in, but it's late in the day by Gordon, and I'm sure packing/getting ready is on the agenda. I'm in CST 6hrs behind GMT