-
-
Notifications
You must be signed in to change notification settings - Fork 242
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Handle disconnection by Modbus/TCP gateway (#347)
* WC-2988: index.js: Make ModbusRTU an EventEmitter. `node-red-modbus-contrib` monitors for disconnection events by hooking the `close` event from the port by accessing the `_port` property of the connection object. This seems like a bad thing to do, but there are numerous cases where a user of this library would need to receive such events. So, better plan: make the client itself an `EventEmitter` so it can proxy such events. * WC-2988: index.js: Expose 'close' event. On successful connect, bind the `close` event of the parent connection class to the port's `close` event so that when it fires on the port, it fires the event (once) on the connection object. No more events should be fired until the library user calls `open` again. * WC-2988: index.js: Prevent memory leak when calling `.open`. If a port gets closed, and we call `.open`, a new copy of the `data` handler is created and added to the port's `data` handler list, which will race the first and cause double-handling. Thus, we need to remove the old listener first before we add a new one. HOWEVER, since it's a new copy, we don't have the reference to the old one to hand to `removeListener`, so a better plan is needed: 1. move the logic out to its own private function. 2. we need a consistent reference, so use the `.bind` function once in our constructor, so we can re-use that same reference when calling `removeListener` 3. pass our premade reference to `_port.on` This will save memory and prevent an ugly double-up situation. * WC-2988: ports: Emit `close` if connection lost on TCP sockets. If a TCP socket gets closed on us, emit a `close` signal so that Modbus library users can initiate a re-connect if needed.
- Loading branch information
1 parent
5c06f21
commit 1d593c0
Showing
4 changed files
with
147 additions
and
120 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters