RanvierMUD / websocket-networking

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Server crashes when you unexpectedly drop an established websocket connection.

ratacat opened this issue · comments

`events.js:167
throw er; // Unhandled 'error' event
^

Error: read ECONNRESET
at TCP.onStreamRead (internal/stream_base_commons.js:139:27)
Emitted 'error' event at:
at WebSocket.finalize (/Users/jared/Dev/ranviermud/bundles/ranvier-websocket/node_modules/ws/lib/WebSocket.js:182:41)
at Socket.emit (events.js:182:13)
at emitErrorNT (internal/streams/destroy.js:82:8)
at emitErrorAndCloseNT (internal/streams/destroy.js:50:3)
at process.internalTickCallback (internal/process/next_tick.js:72:19)`

I seem to be able to replicate it pretty easily by simply connecting to the server with Neuro (which I'm running in a browser) and then closing the browser tab after I've already logged in to a character.

commented

Would using socket.io be a good fix for that, or is that excessive? I ask only because AFAIK socket.io already has this sort of error handling and much more. Or would it be better to create a socket-io-networking bundle?

A separate socket io bundle would be quite cool.

A working solution I found is related to this thread and this comment in the ws repo.

Upon every new inbound connection, attach an error listener to the websocket that is created:

      // This creates a super basic "echo" websocket server
      wss.on('connection', function connection(ws) {

        // ------ Add error handler
        ws.on('error', (err) => {
          // Ignore network errors like `ECONNRESET`, `EPIPE`, etc.
          if (err.errno) return;
          Logger.error(err);
          throw err;
        });
        // ---------
        
        // create our adapter
        const stream = new WebsocketStream();
        // and attach the raw websocket
        stream.attach(ws);

        // Register all of the input events (login, etc.)
        state.InputEventManager.attach(stream);

        stream.write("Connecting...\n");
        Logger.log("User connected via websocket...");

        // @see: bundles/ranvier-events/events/login.js
        stream.emit('intro', stream);
      });
      Logger.log(`Websocket server started on port: ${wss.options.port}...`);
    },
commented

I like this as a good-enough solution, then if people want to do different handling for specific errors they can easily add their own listeners to the socket IIRC.

I upgraded ws to latest and made a yarn install in the bundle and it worked fine