0

When I require information from my server I send a packet via a Worker, specifying inside the packet the name of the function to be called when it is returned. The worker sends the packet to the server via a websocket. I use websockets because traffic may be initiated at any time from either side of the connection. When the worker receives a packet from the server it calls the function named in the packet. Example:

function trial() {
    var p = new Packet();
    var c1 = new Command("getGridColumnOptions");
    p.addCommand(c1);
    p.addreturnRoutine("trial_return");
    sendMessage(p);
}

function trial_return(packet) {
    console.log(JSON.stringify(packet));
}

The worker invokes the return function using:

window[packet.returnRoutine](packet);

This has all worked really well until now, but I now have the need to make a call to the server from within a class and to return to a method within that same class.

class Tab {
    constructor() {
    }
    trial() {
            var p = new Packet();
            var c1 = new Command("getGridColumnOptions");
            p.addCommand(c1);
            p.addreturnRoutine("trial_return");
            sendMessage(p);
    }

    trial_return(packet) {
            console.log(JSON.stringify(packet));
    }
}

The worker gets the return but cannot invoke the return method inside the instantiated class. I get

TypeError: window[json.returnRoutine] is not a function

I know this is because I have not qualified the method name with the parent object reference, but I don’t know how to do this, and it will not have been originally instantiated as a global variable.

Any help very gratefully received!

Heretic Monkey
  • 11,687
  • 7
  • 53
  • 122
Richard
  • 11
  • 5
  • Why does the server need to know the name of the function? That's what events or Promises are for. – Andreas Sep 03 '21 at 12:56
  • 1
    I would say you've got a design/security problem you need to think about. I would never trust something at the other end of a web socket to tell me what to execute. And I certainly wouldn't do it without whitelisting or validating it first. Also, now that you have a class, your server's going to have to know the variable name of the instantiated class... just bad mojo all around. I'd rethink this. – Heretic Monkey Sep 03 '21 at 12:57
  • But if you insist on going down this line, there are many duplicate questions asking how to execute functions based on a dotted string: https://stackoverflow.com/questions/4244896/dynamically-access-object-property-using-variable is probably the canonical. – Heretic Monkey Sep 03 '21 at 13:00
  • @Andreas I don't think websockets support the use of promises. Also the exchange can be initiated from either side of the socket. – Richard Sep 07 '21 at 16:45
  • @HereticMonkey Monkey' I understand your security concerns but I have simplified my code for this exercise. In real life I am keeping a map of random keys against return functions. Also the client will not accept an incoming websocket connection - it is alwasy initiated by the client using using ssl. I have various other certicates being exchanged too. – Richard Sep 07 '21 at 16:47
  • _"I don't think websockets support the use of promises"_ - Websockets "post" messages and the receiving end reacts on those messages. Just like events... – Andreas Sep 07 '21 at 16:47
  • @Andreas But I may be posting loads potentially before I get my first response – Richard Sep 07 '21 at 16:48
  • @HereticMonkey I have resolved the problem by creating a temporary global variable (pointer to the variable) and sending (via maps again) the name of it in the packet. When it returns this allows me to invoke the function through reflection where I delete the variable pointer. – Richard Sep 07 '21 at 16:51
  • @Andreas I cannot rely on the the order of messages. A packet could be sent to the server and the first received back may not be the reply. – Richard Sep 07 '21 at 16:53
  • @HereticMonkey I can see the light! I have used the technique outlined at [Link](https://codeburst.io/promises-for-the-web-worker-9311b7831733) to surround the websocket i/o requests and replies with promises, and I can handle those initiated by the server in a more anonymous way. Thank you both. – Richard Sep 08 '21 at 10:05

0 Answers0