如何在 Mochiweb 应用程序中启用活动套接字?

发布于 2024-08-10 13:21:42 字数 639 浏览 1 评论 0原文

有谁知道如何在 Mochiweb 应用程序中启用主动套接字而不是被动套接字。具体来说,我正在尝试适应 http ://www.metabrew.com/article/a-million-user-comet-application-with-mochiweb-part-2 这样当客户端断开连接时,它会立即“注销”。

我尝试过设置:

start(Options) ->
    {DocRoot, Options1} = get_option(docroot, Options),
    Loop = fun (Req) ->
        Socket = Req:get(socket),
        inet:setopts(Socket, [{active, once}]),
        ?MODULE:loop(Req, DocRoot)
    end,

但这似乎不起作用。在收到新消息后,我仍然只能在 receive 中收到更新。

想法?谢谢!

Does anyone know how to enable active instead of passive sockets in a Mochiweb application. Specifically, I am trying to adapt http://www.metabrew.com/article/a-million-user-comet-application-with-mochiweb-part-2 so that when a client disconnects, it will immediately "logout".

I have tried setting:

start(Options) ->
    {DocRoot, Options1} = get_option(docroot, Options),
    Loop = fun (Req) ->
        Socket = Req:get(socket),
        inet:setopts(Socket, [{active, once}]),
        ?MODULE:loop(Req, DocRoot)
    end,

but that seems to not be working. I still only get updates in my receive after I am sent a new message.

Thoughts? Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

迷雾森÷林ヴ 2024-08-17 13:21:42

我为我的 Erlang comet 应用程序解决了这个问题,我在 这篇博文。基本上,您不希望套接字始终处于活动模式;您只希望在阅读客户端的请求之后和返回响应之前使其处于活动模式。

这是一个示例请求处理程序:

comet(Req) ->
    Body = Req:recv_body(),
    io:format("~nBody: ~p~n", [Body]),
    Socket = Req:get(socket),
    inet:setopts(Socket, [{active, once}]),
    Response = connection:handle_json(Body),
    inet:setopts(Socket, [{active, false}]),
    io:format("~nSending Response: ~s~n", [Response]),
    Req:ok({"application/json", [], Response}).

io:format 调用只是为了我的利益而进行的控制台日志记录。重要的部分是,在从请求读取正文之后、调用保存请求并返回数据的函数之前,我在套接字上设置了 {active, Once}。我还关闭了活动模式;套接字可以在某些 HTTP 模式下重用。

I solved this for my Erlang comet app, parts of which I show in this blog post. Basically, you don't want the socket to be in active mode all the time; you just want it in active mode after you've read the client's request and before you return a response.

Here's a sample request handler:

comet(Req) ->
    Body = Req:recv_body(),
    io:format("~nBody: ~p~n", [Body]),
    Socket = Req:get(socket),
    inet:setopts(Socket, [{active, once}]),
    Response = connection:handle_json(Body),
    inet:setopts(Socket, [{active, false}]),
    io:format("~nSending Response: ~s~n", [Response]),
    Req:ok({"application/json", [], Response}).

The io:format call is just console logging for my benefit. The important part is that I set {active, once} on the socket after reading the body from the request and just before calling the function which holds the request and returns data. I also turn active mode back off; the socket may be reused in certain HTTP modes.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文