我有套接字连接,我正在尝试从不同的类订阅这个 onMessage 函数,以便我可以获取该特定类中的消息,但我无法在下面的代码中的processTicks函数中调用test()片段。请帮忙。谢谢
class SocketClient {
this._ws.onmessage = (msg) => {
for (var i = this._handlers.length - 1; i >= 0; i--) {
this._handlers[i](msg);
}
};
setHandler(callback) {
this._handlers.push(callback)
}
}
class SomeOtherClass {
test() {
}
processTicks(ticks) {
try {
this.test() //this is not working
// how do i get the correct this here,
// so that i can call some function inside this class
} catch (err) {
console.log(err)
}
}
registerListner() {
socketClient.setHandler(this.processTicks);
}
}