servvs
servvs

Reputation: 74

How can you read modified class properties from another classes callback in javascript?

I have 2 classes with websockets. They should be used to send information from one to the other with the program as the intermediary. The WSS listens and can only have a single connection. I have a client connection with a different server. In my main function I instantiate each of these objects and assign forwardMessage(event) as a callback (onmessage) to the listener. It should essentially forward the contents but the problem is even after a client has connected to the serverWSS object, the callback always reports activeWS as null. I am not sure how I can get a reference of this between the two in this way.

class WSS {
    constructor(){
        const WebSocket = require('ws');
        this.wss = new WebSocket.Server({ port: 3001});
        this.activeWs = null;

        this.wss.on('connection', function connection(ws) {
            console.log("Client Connected");
            if (this.activeWs != null) {
                this.activeWs.close(1000, "Server initiated close");
                console.log("Closed old session")
            }
            this.activeWs = ws;
        })

        this.sendCommand = (data) => {
            console.log(this.activeWs)
            if (this.activeWs == null) {
                return;
            }
            console.log(data);
        }
    }
}

class WS {
    constructor(clientId, onmessage) {
        this.clientId = clientId;
        const WebSocket = require('ws');
        this.ws = new WebSocket('localhost:8080');
        this.ws.onmessage = (event) => { onmessage(event)};
    }
}

serverWSS = new WSS();
listener = new WS("000", forwardMessage)

function forwardMessage(event) {
    serverWSS.sendCommand(event.data);
}

Upvotes: 0

Views: 41

Answers (1)

user29455873
user29455873

Reputation:

The context of the listener connection function is different than the WSS class therefore the line...

this.activeWs = ws;

...initialises a different activeWs variable than the line...

this.activeWs = null;

A fix could be to change the WSS class constructor's code to define a context variable initialised with this...

var context = this;

...and use the context variable in the code snippets of the connection listener function and in the sendCommand lambda function, that by the way has access to a different this that the context of WSS constructor since it is a lambda function and not a function...

class WSS {
    constructor(){
        const WebSocket = require('ws');
        this.wss = new WebSocket.Server({ port: 3001});
        this.activeWs = null;

        var context = this;

        this.wss.on('connection', function connection(ws) {
            console.log("Client Connected");
            if (context.activeWs != null) {
                context.activeWs.close(1000, "Server initiated close");
                console.log("Closed old session")
            }
            context.activeWs = ws;
        })

        this.sendCommand = (data) => {
            console.log(context.activeWs)
            if (context.activeWs == null) {
                return;
            }
            console.log(data);
        }
    }
}

Upvotes: 0

Related Questions