无法使用Deno中的Oak WebSocket中间件从服务器发送消息回客户端。

huangapple go评论91阅读模式
英文:

Unable to send messages back to the client from the server using Oak websocket middleware in Deno

问题

当使用下面的代码时,客户端永远不会收到echo2消息。第一条消息echo1,在等待之前被客户端接收到。如果我移除await,客户端会接收到两条消息。

我知道await在某种程度上中断了WebSocket。我漏掉了什么?

服务器端代码:

  1. import {
  2. Application,
  3. Router
  4. } from "https://deno.land/x/oak@v12.1.0/mod.ts";
  5. const app = new Application();
  6. const router = new Router();
  7. async function pause(ms) {
  8. return new Promise((resolve) => setTimeout(resolve, ms));
  9. }
  10. router.get("/ws", (ctx) => {
  11. console.log("ws connected");
  12. if (!ctx.isUpgradable) {
  13. ctx.throw(501);
  14. }
  15. const ws = ctx.upgrade();
  16. ws.onopen = () => {
  17. ws.send(JSON.stringify({ intent: "init", id: "test" }));
  18. };
  19. ws.addEventListener("message", async (ev) => {
  20. const d = JSON.parse(ev.data);
  21. console.log("got message", d);
  22. switch (d.intent) {
  23. case "echo": {
  24. ws.send(JSON.stringify({ intent: "echo1", data: { empty: true } }));
  25. await pause(1000);
  26. ws.send(JSON.stringify({ intent: "echo2", data: { empty: true } }));
  27. break;
  28. }
  29. }
  30. });
  31. });

编辑:我使用了一个简单的WS客户端Chrome扩展来测试WebSocket,而不是编写显式代码。

英文:

When using the below code, the echo2 message is never received by the client. The first message, echo1 -- which is prior to the await, is received by the client. If I remove the await, both messages are received by the client.

I know the await is somehow interrupting the websocket. What am I missing?

Server Code:

  1. import {
  2. Application,
  3. Router
  4. } from "https://deno.land/x/oak@v12.1.0/mod.ts";
  5. const app = new Application();
  6. const router = new Router();
  7. async function pause(ms) {
  8. return new Promise((resolve) => setTimeout(resolve, ms));
  9. }
  10. router.get("/ws", (ctx) => {
  11. console.log("ws connected");
  12. if (!ctx.isUpgradable) {
  13. ctx.throw(501);
  14. }
  15. const ws = ctx.upgrade();
  16. ws.onopen = () => {
  17. ws.send(JSON.stringify({ intent: "init", id: "test" }));
  18. };
  19. ws.addEventListener("message", async (ev) => {
  20. const d = JSON.parse(ev.data);
  21. console.log("got message", d);
  22. switch (d.intent) {
  23. case "echo": {
  24. ws.send(JSON.stringify({ intent: "echo1", data: { empty: true } }));
  25. await pause(1000);
  26. ws.send(JSON.stringify({ intent: "echo2", data: { empty: true } }));
  27. break;
  28. }
  29. }
  30. });
  31. });

EDIT: I was using a simple WS client chrome extension to test the websocket, not writing explicit code.

答案1

得分: 0

这个问题在我从 deno 版本 1.32.x 升级到 1.34.3 后自行解决了。

英文:

This issue resolved itself when I upgraded from deno 1.32.x to 1.34.3.

huangapple
  • 本文由 发表于 2023年7月3日 08:17:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/76601237.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定