English 中文(简体)
How to enable active sockets in a Mochiweb application?
原标题:

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!

问题回答

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.





相关问题
How big can Erlang DETS be and what to do if its too small?

All I need is a large persistent lookup table in Erlang and dets seems like just the thing though I need a definative answer to: just how big the total size of the binaries in the table can be. how ...

passing events from erlang to Clojure

I m looking for a way to pass events back and forth between Clojure and erlang. has someone done this before? how should I encode the (immutable) messages in a flaxable general way? Should IPC be ...

How to send a push notification using Erlang?

I m trying to send a push notification to APNs using Erlang. This is the code I came up with so far: -module(apnstest2). -export([connect/0]). connect() -> application:start(ssl), ssl:...

How do I build a DNS Query record in Erlang?

I am building a native Bonjour / Zeroconf library and need to build DNS query records to broadcast off to the other machines. I have tried looking thru the Erlang source code but as I am relatively ...

AccessViolation when calling unmanaged dll

When calling an unmanaged Dll from a c# application I get an AccessViolationException. The strange thing is that the exported function has no arguments, so the problem is not in the Marshalling of ...

How to enable active sockets in a Mochiweb application?

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-...

How to convert numbers to words in Erlang?

I found this interesting question about converting numbers into "words": Code Golf: Number to Words I would really like to see how you would implement this efficiently in Erlang.

热门标签