简体   繁体   中英

Node js Eventemitter not fireing

I have the Problem, that my Eventemitter is not fireing. I already googled the Problem and it seems like my order of the code is wrong.. or at least thats my guess. Anyways, no matter how I put the Order of creating the client.js class, it's not triggering.

FYI creating a electron app that is communicating with a server over TCP/IP. When the clients connection status is changing, the "connection"-event should trigger to tell the frontend to change. Other events like "error" are working.

I dont know if this is enough code for you, just tell me if you need more.

client.js

class Client extends events.EventEmitter {
  constructor(HOST, PORT){
    // setting up the client..
    super();
    this.host = HOST;
    this.port = PORT;
    // and so on...
    this.client = new net.Socket();
    // othere eventlisteres...
    this.client.on("connect", this.connectEventHandler);
  }
  connectEventHandler() {
    try {
      this.status = "connected"; // I can check the status from main
      this.tryReconnecting = false; // other logic of the client class
      this.emit("connection"); // this is the event (not working)
      console.log("connection status should be triggered"); // this console.log is working
    } catch (error) {
      console.log(error);
      this.emit("error", error);
    }
  }
}

main.js

// creating the client
const client = new Client(process.env.SERVER, process.env.PORT);

// when the app is ready, client is told to connect to the server
app.whenReady().then(() => {
  createWindow();
  app.on("activate", function () {
    if (BrowserWindow.getAllWindows().length === 0) createWindow();
  });
  client.connect();
});

// the error event is working perfectly, event when I call it in the connectEventHandler
client.on("error", (message) => {
  console.log("error event triggered");
  dialog.showMessageBoxSync(mainWindow, {
    title: "Fehler",
    message: message,
  });
});

// this part is not working
client.on("connection", () => {
  console.log("new client status: " + client.status);
  mainWindow.webContents.send("client-status", client.status);
});

Even when I call the error event instead of the connection event, the error is displayed in my app:

// code from client.js
connectEventHandler() {
  try {
    this.status = "connected"; // I can check the status from main
    this.tryReconnecting = false; // other logic of the client class

    // this part is getting ignored
    this.emit("connection");

    // this part is executed
    this.emit("error", "this is a test error");

  } catch (error) {
    console.log(error);
    this.emit("error", error);
  }
}

the Response from the last bit of code (connectEventHandler):

“connectEventHandler”中来自前端的响应

What is going on here?

Thanks!

The Problem was the ConnectEventHandler, after putting the logic of this function into the constructor of the client, every event is getting triggered properly.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM