Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

When data is empty, WebSocket will not fire a message event. #3202

Closed
tsctx opened this issue May 5, 2024 · 1 comment · Fixed by #3205
Closed

When data is empty, WebSocket will not fire a message event. #3202

tsctx opened this issue May 5, 2024 · 1 comment · Fixed by #3205
Labels
bug Something isn't working

Comments

@tsctx
Copy link
Member

tsctx commented May 5, 2024

import { WebSocket } from "./index.js";

import { WebSocketServer } from "ws";

const server = new WebSocketServer({ port: 5001 });

server.on("connection", (socket) => {
  socket.on("message", (_data, _isBinary) => {
    socket.send(Buffer.allocUnsafe(0));    // socket.send(""); // or this
  });
});

await new Promise((resolve, _reject) => {
  server.on("listening", resolve);
});

const ws = new WebSocket(`http://localhost:${server.address().port}`);

ws.addEventListener("open", () => {
  ws.send("Hi");
});

ws.addEventListener("message", () => {
  console.log("ok");
  ws.close();
  server.close();
});

ws.addEventListener("error", (err) => {
  process.nextTick(() => {
    throw err;
  });
});
@tsctx tsctx added the bug Something isn't working label May 5, 2024
@tsctx
Copy link
Member Author

tsctx commented May 5, 2024

@KhafraDev

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant