[erlang-questions] otp / behavior / cowboy_websocket_handler
Mike Oxford
moxford@REDACTED
Mon Mar 10 23:37:56 CET 2014
The -callback is a dialyzer (testing tool) type spec. It's just an
annotation so that some behaviour metadata gets added for the analysis.
The code would run exactly the same if you took out the -callback from
each line (though Dialyzer might then complain!)
http://learnyousomeerlang.com/dialyzer and search for "-callback"
On Mon, Mar 10, 2014 at 3:31 PM, Mike Oxford <moxford@REDACTED> wrote:
> Erlang behaviours are, I believe and for lack of a better term, interface
> definitions. It says what is required to be implemented to be a valid
> behaviour. Once it's compiled and verified I do not believe that it's ever
> used again. It does not actually "do" anything since it's just a
> specification.
>
> It looks like ws_handler:websocket_init/3 starts a 1 second timer.
> When that timer fires then you're seeing the 1 second handler, which is
> ws_handler.
> The "loop" you're expecting to see is in websocket_app:start/2 where you
> register the callbacks (such as ws_handler) for cowboy:start_http/3
>
> So cowboy:start/3 takes in the ws_handler definition, inits it (and that
> init starts the 1s timer.) Every time it fires (eg, every 1 seconds) the
> cowboy system will hit the ws_handler callback (which, since it's
> a cowboy_websocket_handler behaviour, is known to have a websocket_handle/3
> function.)
>
> HTH.
>
> -mox
>
>
>
>
>
> On Mon, Mar 10, 2014 at 2:32 PM, t x <txrev319@REDACTED> wrote:
>
>> Hi,
>>
>> I'm trying to understand what Erlang is doing with the following
>> piece of code:
>>
>>
>> https://github.com/extend/cowboy/blob/master/examples/websocket/src/ws_handler.erl
>>
>> Please note:
>>
>> * this code is running fine (I don't need help trouble shooting the
>> code)
>>
>> * the behavior I get matches my intuition of what is happening
>>
>> My actual problem is _why_ does everything happen the way it is.
>>
>>
>> ### Behavior I get:
>>
>> Upon opening a websocket and connecting, I get back:
>>
>> Hello!
>> [ 1 second delay ]
>> How' you doing'?
>> [ 1 second delay ]
>> How' you doing'?
>> ...
>>
>>
>> This behavior is expected because
>>
>> * websocket_init is called, which causes a "Hello!" message to be
>> sent to self (handled by websocket_info)
>>
>> * every time websocket_info is called, it:
>> * registers 1-second call back for "How'you doing'?"
>> * and sends out the msg it received
>>
>>
>> ## Question: What is happening behind the scenes with:
>>
>> -behavior(cowboy_websocket_handler) which causes all this to happen?
>>
>>
>>
>> ## What I've tried:
>>
>> So I googled and got:
>>
>>
>> https://github.com/extend/cowboy/blob/master/manual/cowboy_websocket_handler.md
>>
>> And this is a great human-level understanding.
>>
>>
>> However, I want to see the code that makes it happen.
>>
>>
>> Then, I look at:
>>
>>
>> https://github.com/extend/cowboy/blob/master/src/cowboy_websocket_handler.erl
>>
>>
>>
>> And now, I am confused.
>>
>>
>> What I see is a bunch of "-callback" lines.
>>
>>
>>
>> What I expect to see is something like:
>>
>> spawn(func, ...)
>>
>> func ( ... ) ->
>> some loop or some sort for handling messages
>>
>>
>> In particular, my confusion is: it's clear that
>> behavior(cowboy_websocket_handler) causes some process to be spawned
>> somewhere. Where is this happening?
>>
>>
>>
>> Thanks!
>> _______________________________________________
>> erlang-questions mailing list
>> erlang-questions@REDACTED
>> http://erlang.org/mailman/listinfo/erlang-questions
>>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20140310/2e539372/attachment.htm>
More information about the erlang-questions
mailing list