<div dir="ltr">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!)<div>
<br></div><div><a href="http://learnyousomeerlang.com/dialyzer">http://learnyousomeerlang.com/dialyzer</a> and search for "-callback"<br></div><div><br></div><div><br></div></div><div class="gmail_extra"><br><br>
<div class="gmail_quote">On Mon, Mar 10, 2014 at 3:31 PM, Mike Oxford <span dir="ltr"><<a href="mailto:moxford@gmail.com" target="_blank">moxford@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<div dir="ltr">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.<div>
<br></div><div>It looks like ws_handler:websocket_init/3 starts a 1 second timer.</div><div>When that timer fires then you're seeing the 1 second handler, which is ws_handler.</div><div>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</div>
<div><br></div><div>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.)</div>
<div><br></div><div>HTH.</div><div><br></div><div>-mox</div><div><br></div><div><br></div><div><br></div></div><div class="HOEnZb"><div class="h5"><div class="gmail_extra"><br><br><div class="gmail_quote">On Mon, Mar 10, 2014 at 2:32 PM, t x <span dir="ltr"><<a href="mailto:txrev319@gmail.com" target="_blank">txrev319@gmail.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Hi,<br>
<br>
I'm trying to understand what Erlang is doing with the following<br>
piece of code:<br>
<br>
<a href="https://github.com/extend/cowboy/blob/master/examples/websocket/src/ws_handler.erl" target="_blank">https://github.com/extend/cowboy/blob/master/examples/websocket/src/ws_handler.erl</a><br>
<br>
Please note:<br>
<br>
* this code is running fine (I don't need help trouble shooting the code)<br>
<br>
* the behavior I get matches my intuition of what is happening<br>
<br>
My actual problem is _why_ does everything happen the way it is.<br>
<br>
<br>
### Behavior I get:<br>
<br>
Upon opening a websocket and connecting, I get back:<br>
<br>
Hello!<br>
[ 1 second delay ]<br>
How' you doing'?<br>
[ 1 second delay ]<br>
How' you doing'?<br>
...<br>
<br>
<br>
This behavior is expected because<br>
<br>
* websocket_init is called, which causes a "Hello!" message to be<br>
sent to self (handled by websocket_info)<br>
<br>
* every time websocket_info is called, it:<br>
* registers 1-second call back for "How'you doing'?"<br>
* and sends out the msg it received<br>
<br>
<br>
## Question: What is happening behind the scenes with:<br>
<br>
-behavior(cowboy_websocket_handler) which causes all this to happen?<br>
<br>
<br>
<br>
## What I've tried:<br>
<br>
So I googled and got:<br>
<br>
<a href="https://github.com/extend/cowboy/blob/master/manual/cowboy_websocket_handler.md" target="_blank">https://github.com/extend/cowboy/blob/master/manual/cowboy_websocket_handler.md</a><br>
<br>
And this is a great human-level understanding.<br>
<br>
<br>
However, I want to see the code that makes it happen.<br>
<br>
<br>
Then, I look at:<br>
<br>
<a href="https://github.com/extend/cowboy/blob/master/src/cowboy_websocket_handler.erl" target="_blank">https://github.com/extend/cowboy/blob/master/src/cowboy_websocket_handler.erl</a><br>
<br>
<br>
<br>
And now, I am confused.<br>
<br>
<br>
What I see is a bunch of "-callback" lines.<br>
<br>
<br>
<br>
What I expect to see is something like:<br>
<br>
spawn(func, ...)<br>
<br>
func ( ... ) -><br>
some loop or some sort for handling messages<br>
<br>
<br>
In particular, my confusion is: it's clear that<br>
behavior(cowboy_websocket_handler) causes some process to be spawned<br>
somewhere. Where is this happening?<br>
<br>
<br>
<br>
Thanks!<br>
_______________________________________________<br>
erlang-questions mailing list<br>
<a href="mailto:erlang-questions@erlang.org" target="_blank">erlang-questions@erlang.org</a><br>
<a href="http://erlang.org/mailman/listinfo/erlang-questions" target="_blank">http://erlang.org/mailman/listinfo/erlang-questions</a><br>
</blockquote></div><br></div>
</div></div></blockquote></div><br></div>