[erlang-questions] otp / behavior / cowboy_websocket_handler

Loïc Hoguin essen@REDACTED
Tue Mar 11 08:37:25 CET 2014


The actual code is in cowboy_websocket. (Hence init/3 returning it wants 
to upgrade to cowboy_websocket).

The guide now has a proper chapter for Websocket (on github) that 
explains that part of the code.

On 03/10/2014 10:32 PM, t x 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
>

-- 
Loïc Hoguin
http://ninenines.eu



More information about the erlang-questions mailing list