- FXServer version 5848
- Connecting to an external (same system) data source through websockets (socket.io)
- When server side of socket.io (running on NestJS) breaks connection EventEmitter.prototype.off errors out when it’s not needing to error
- Server side bug (artifacts)
- Create a socket.io server, and a FiveM resource that connects to it.
On line 590 of citizen/scripting/v8/eventemitter2.js
it will error out when socket.io disconnects, the prototype seems to assume that all types are ‘removeListener’, while the one I’m triggering is ‘offline’.
When I manually adjust the code to (see code block), the error fixes itself and expected behavior returns.
EventEmitter.prototype.off = function(type, listener) {
- if (typeof listener !== 'function') {
+ if (type === 'removeListener' && typeof listener !== 'function') {
throw new Error('removeListener only takes instances of Function');
}
...
type
seems to always be a string
and based on the code within the prototype removeListener
is the reason the prototype is wanting to throw an error.
If there’s a public repo somewhere I am willing to make a PR, else, this should be the fix based on reading just this method in isolation.
Edit: Other solution is to just return void if type is ‘offline’
EventEmitter.prototype.off = function(type, listener) {
+ if(type === 'offline') {
+ return;
+ }
if (typeof listener !== 'function') {
throw new Error('removeListener only takes instances of Function');
}
Edit 2:
While both solutions work, neither are a permanent solution. On repeated server side (NestJS) disconnects and reconnects, some of the socket.io events (only on ‘connect’ during limited testing) gets additional events added; causing it to fire several times (n+1)