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

deno std websocket server doesn`t push messages to client when using a wss connection and listening for ws events #8945

Closed
starkpsmichael opened this issue Dec 31, 2020 · 1 comment
Labels

Comments

@starkpsmichael
Copy link

This is really strange, i have tried to test it down.

I observed, see also the comments in the test file, that when you try to push messages via websocket.send using a normal ws connection, all works fine, also when you observe the websocket events for a ping, or a string, all works.

But when you use a wss connection, the messages will not be pushed.
The problem seems to be the observing of the websocket events, if you remove/deactive it - all works, and the messages will be pushed to the client.
Also, if u send a manual event to the server via a client, the not pushed messages, will then be pushed - seems the websocket server needs a trigger to actually push it.
When u log the websocket connection you can see, that the messages are hanging in the message queue. :(

Expected Behaviour

Websocket messages will be pushed to client, when using a wss connection.

Actual Behaviour

No messages will be pushed.

Steps to Reproduce

  1. Create a file named mod.ts with the following content:
 // ! Problem: When u use https message 2 and 3 will not be pushed, when the for loop is activated.

  const useHttps = true; // change me
  const observeWsEvents = true; // the problem seems to be here, change it to false when u use https and it works!

  async function handleWs(sock: WebSocket) {
    console.info("socket connected!");

    await sock.send("message 1");

    setTimeout(async () => {
      await sock.send("message 2");
      const cevent = new CustomEvent("cevent");
      globalThis.dispatchEvent(cevent);
    }, 1000 * 5);
    globalThis.addEventListener("cevent", async (event) => {
      await sock.send("message 3");
    });

    if (observeWsEvents) {
      for await (const event of sock) {
        if (typeof event === "string") {
          console.log(`ws:Text ${event}`);
          await sock.send(event);
        }
      }
    }
  }

  const port = 5000;
  const httpServer = useHttps
    ? serveTLS({
      port,
      certFile: "./cert/localhost.crt",
      keyFile: "./cert/localhost.key",
    })
    : serve({ port });

  console.info(`${useHttps ? "https" : "http"} server is running on :${port}`);

  for await (const req of httpServer) {
    try {
      const ws = await acceptWebSocket({
        conn: req.conn,
        bufReader: req.r,
        bufWriter: req.w,
        headers: req.headers,
      });
      console.info(`websocket server is running on :${port}`);
      await handleWs(ws);
    } catch (e) {
      console.error(`failed to accept websocket: ${e}`);
    }
  }
  1. Run it with deno run --unstable --allow-read --allow-net --allow-env --lock-write --lock lock.json mod.ts
  2. Inspect the websocket with something like: WebSocket Test Client

Using

  • deno 1.6.3 (release, x86_64-unknown-linux-gnu)
  • v8 8.8.294
  • typescript 4.1.3
@stale
Copy link

stale bot commented Mar 1, 2021

This issue has been automatically marked as stale because it has not had recent activity. It will be closed in 7 days if no further activity occurs. Thank you for your contributions.

@stale stale bot added the stale label Mar 1, 2021
@stale stale bot closed this as completed Mar 8, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant