emscripten-core / emscripten

Emscripten: An LLVM-to-WebAssembly Compiler
Other
25.67k stars 3.29k forks source link

EmscriptenWebSocketCloseEvent->code and wasClean are incorrect and messed #22499

Closed yanxia97 closed 3 weeks ago

yanxia97 commented 4 weeks ago

Please include the following in your bug report:

Version of emscripten/emsdk: emcc -v emcc (Emscripten gcc/clang-like replacement + linker emulating GNU ld) 3.1.57 (1df9c1977b49926c1efca672c31414da45c0c7bb) clang version 19.0.0git (https:/github.com/llvm/llvm-project ccdebbae4d77d3efc236af92c22941de5d437e01) Target: wasm32-unknown-emscripten Thread model: posix InstalledDir: /Users/yanxia/Desktop/emsdk/upstream/bin Build config: +assertions

I try to get the code and reason in the close callback. Here is my code example.

EM_BOOL EmscriptenConnection::OnWebSocketClose(int eventType, const EmscriptenWebSocketCloseEvent *websocketEvent,
                                               void *userData) {
  auto connection = static_cast<EmscriptenConnection *>(userData);

  unsigned short code = websocketEvent->code;
  auto reason = websocketEvent->reason;

  connection->connecting_ = false;
  connection->connected_ = false;

  if (connection->on_close_callback_) {
    connection->on_close_callback_(connection, code, reason);
  }

  return EM_TRUE;
}

The problem is that the code the server sent is not the code I recieved. And I find in the line 220 of file library_websocket.js from the main branch right now, there is a typo.

      {{{ makeSetValue('eventPtr', C_STRUCTS.EmscriptenWebSocketCloseEvent.wasClean, 'e.code', 'i16') }}},

issue2024-09-04 15 25 35

As a result, EmscriptenWebSocketCloseEvent->code is incorrect and EmscriptenWebSocketCloseEvent->wasClean somehow works as the code

sbc100 commented 4 weeks ago

Oops, thanks for the report. Clearly we lack testing in this area.