如何在 Mochiweb 应用程序中启用活动套接字?
有谁知道如何在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我为我的 Erlang comet 应用程序解决了这个问题,我在 这篇博文。基本上,您不希望套接字始终处于活动模式;您只希望在阅读客户端的请求之后和返回响应之前使其处于活动模式。
这是一个示例请求处理程序:
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:
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.