engine.io-client icon indicating copy to clipboard operation
engine.io-client copied to clipboard

[Browser] Doesn't switch to polling when WebSocket is undefined.

Open hassansin opened this issue 2 years ago • 0 comments

I have set my transports to use WebSocket first and then fallback to polling e.g. transports: ["websocket", "polling"].

To test if the fallback transport really works, I used TamperMonkey browser extension to disable Websocket by setting WebSocket = void 0.

Instead of switching to polling, I keep getting connect_error with the message timeout from websocket transport.

Tried to investigate and it looks like transport switching only works if createTransport method throws here:

https://github.com/socketio/engine.io-client/blob/fa479164251dd1f283dd163143b546582161339a/lib/socket.ts#L467-L474

But the method always returns a transport wrapper class, even when WebSocket is not defined.

To Reproduce

Engine.IO server version: 6.5.2

Server

const engine = require("engine.io");
const server = engine.listen(3000, {
  transports: ["polling", "websocket"]
});

server.on("connection", (socket) => {
  console.log("connection");

  socket.on("message", (data) => {
    console.log("data", data);
  });

  socket.on("close", () => {
    console.log("close");
  });
});

Engine.IO client version: 6.5.2

Client

const socket = require("engine.io-client")("ws://localhost:3000", {
transports: ["websocket", "polling"]
});

socket.on("open", () => {
  console.log("open");

  socket.on("message", (data) => {
    console.log("data", data);
  });

  socket.on("close", () => {
    console.log("close");
  });
});

Expected behavior

Should be able to fallback to polling transport when WebSocket transport is disabled on the browser.

Platform:

  • Device: Chrome 119.0.6045
  • OS: Windows

Additional context Add any other context about the problem here.

hassansin avatar Nov 16 '23 11:11 hassansin