let client = new WebsocketClient();
client.on('connect', (connection) => {
});
Or in short:
let myObj={data{tests{id:[]}}};
let result=getFullPathFunc(myOfj,'id');
console.log(result)
//exptected: myObj.data.tests.id[0]
So I want to see the full path by using the connection method or event.
Like : console.log(getFullPathFunc(client,‘connection’);
Expected: client.websockets…bla.bla.bla.connection(bla(typeof)).
If there is such or another way to make a things clear. It’s taking time to read and inspect about hundred of objects listed by console.log(client). Sure I can go to sources and try to find it there. But again it’s time killer action. Thanks.
I’m sorry, it’s unclear what exactly you’re trying to do.
I can see that you’ve made a websocket client, and when a connection is made, you have a callback function that receives that connection object. Please expand on what you want to do with the connection object. It sounds like you want to get access a deeply nested property and log it, is that correct?
Yes, you right, because the connection prop inside a client object, I want to get an access outside of client scope.
Like:
let client = new WebsocketClient();
client.on('connect', (connection) => {
});
//usually the code in such cases I saw:
client.clients.forEach(function each(connection) {
//got access
}
//it's no problem with ws server, cause it has a *clients* constructor enum, but client (as I inspected in client object inside) does not contain a *clients* enum.
I’m sorry but it’s still not clear what your goal and/or problem is.
let client = new WebsocketClient();
client.on('connect', (connection) => {
// what do you want to do here?
});
This code makes new client. Then it sets a “connect” event listener, so that when a “connect” event happens it will execute the callback. Right now, your callback has nothing in it.
What do you want to happen when a connection is made?
Reiterating that it’s a bit unclear what exactly you want. But if you literally mean you want to log a string path to a key in a deeply-nested object then you use depth-first or breadth-first search to locate the key, building up a string as you traverse.
So for example:
given a function printPath that takes as parameters an input object, a target key, and an array of keys initialised with a root value, eg
if a key is equal to targetKey, concatenate the key into the value of path. Then return the value of path joined by “.”
if the value of inputObj[key] is an object, then return printPath, passing in inputObj[key], targetKey and the value of path with key concatenated to it.
if neither of the above are true, return/throw some error, key isn’t there
This will give you a string. It’s useful for logging but that’s about it (you would have to eval it to use it, which is not something you want to do)
Assuming Dan’s on the right track, the most helpful thing you could do is give us an example input (the deeply nested object), and your expected output (what you want to see logged).
This is the working code from ws server side for ping-pong. I want to do the same for client side. Logic here is if ping sent will not came back as pong to server - the connection will be dropped.
The problem with client in scope.:
let client = new WebsocketClient();
client.on('connect', (connection) => {
});
const pingPongHandler=(connection)=> {
//the same logic as on server.
connection.close;
//but the client.connection in not available to me.
}
As I replied to @DanCouper, I need also an expertise check, if JS or Node does not have some “magic” like ws.clients.forEach() which is amazingly handy.
There is several reasons, first for some reason if connection disappears the code inside client “connection” scope stopping work. Another one, I want to be a like a “Pro”, learning myself to effectively handling scopes. Because IMHO, the scopes are just the thing that must have.
Could somebody advise if I am on right way, my plan is to create(learn first) a class with constructor for enumerator.
So this class will accept some parameter outside, then enumerate it. So I can get an access with let myCopyClass=new Original.Class; then myCopyClass.clients.forEach()…
Thank you All for your helpful advises. In case give please heads up, if some concept available.
If you are trying to access a deeply nested property or you want test if a deeply nested property exist inside your object you can take a look at this:
I am still at a loss as what you’re trying to do, why you want a string representing a path through an object (or if you even do)
You haven’t explained what you’re actually trying to do: I realise this may be difficult if you’re writing in a second language, but you’ve got to try to explain what you’re trying to do, in simple terms.
At the minute there seems to be no relationship between the object keys thing and the websocket stuff you posted.
There is also a possiblity, reading back what you wrote a few times, that you’re misunderstanding how stuff works. Eg
I have literally no idea what you’re trying to do or why here