Hacktoberfest 2026:维护者为十月标记出来的 issue,仍然开放、适合新手。 浏览 Hacktoberfest issue

clarify how bidirectional communication should work

未关闭
#30 6 条评论 1 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 5 天内回复

还没有人认领这个 Issue。

  • #247 来自 @ting-hong-shieh —— 已关闭,未合并

评估

难度
3/5
预计耗时
1-2 天
新手友好度
45/100
Issue 类型
文档
描述清晰度
基本清楚
活跃度
停滞
技术栈
typescript
领域
documentation

调研方向

从本 issue 中通过 newWebSocketRpcSession、RpcTarget、callback 函数和 timer 示例展示的双向通信行为开始。查看链接的示例 repository,以了解所演示的流程,然后通过可运行的示例记录通知式 callback 以及 async/timer 的易错点。完成的标准是,新贡献者能够理解如何使用双向通信,以及应该预期什么样的时序行为。

由索引模型根据 Issue 内容生成。

描述

documentation

Heya, neat library. Almost what I was asking for just a day ago 😆

I'm having a hard time understanding how to achieve bidirectional communication. It's almost worded like you can just push data back to the client, but I think that's not the right kind of thinking. So I dug deeper I ended up using the functionality that allows you to pass functions by reference- it seems like this is kind of what's intended?

Where it gets really confusing when you add things like timers into the equation. Without marking the method as async and awaiting a promise directly, it doesn't behave as you'd expect (from a "nieve" point of view, that is.)

I have an example repository you can use to explore this in more depth

Example client:

using wsApi = newWebSocketRpcSession<MyWs>("ws://localhost:8000");

const d = (sm: string) => console.log("got something: " + sm)

const wsResult = await wsApi.test(d);

console.log(wsResult);

Example websocket "client":

class WsServer extends RpcTarget {
    async test(fn: (data: string) => void) {
        fn("testing");

        setTimeout(1000, () => {
            fn("hello from timeout");
        });

        return true;
    }
}

It'll call the first function, then return true. That makes a lot of sense! I would expect this to be a "gotcha" here and not a bug of the protocol (how would you know timers are involved?)

Now replace the setTimeout with an import from node:timers/promises:

class WsServer extends RpcTarget {
    async test(fn: (data: string) => void) {
        fn("testing");

        const msg = await setTimeout(1000, "da");
        fn(msg);

        return true;
    }
}

We'll get what we want:

got something: testing
got something: da
true

The ask here is if we can get some examples of bidirectional communication and the "gotchas" those comes with. I can see how functions can be used to achieve the "notification" style API for a client and this is something I'm looking to explore :)

主要语言
TypeScript
星标
4k
派生
150
平均合并
3 天 12 小时
30 天内合并 PR
2

环境准备

  • 没有 Dockerfile 或 Docker Compose 文件
  • 没有 Pull Request 模板
  • 阅读贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

cloudflare/capnweb 的其他 Issue

查看 cloudflare/capnweb 的全部 Issue

相似的 Issue

更多 TypeScript Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。